查找和替换word文档(.docx,.doc)中的文本有多个示例,但是如何查找已创建文本所在的页面?
谢谢你, 穆赫辛
答案 0 :(得分:1)
您可以对Information(Word.WdInformation.wdActiveEndAdjustedPageNumber)
的结果使用Find()
方法并将其投放到int
,以获取找到文本的PageNumber。
发布您的代码,我会更新它以返回页码。
以下是一个例子:
currentPageNumber = word.ActiveDocument.ActiveWindow.Selection.Range.Information(
Microsoft.Office.Interop.Word.WdInformation.wdActiveEndAdjustedPageNumber)
这是获取PageNumber的MSDN documentation on the enumerator used。根据您的需要,您可能希望使用与PageNumbers相关的2个枚举器中的1个。
答案 1 :(得分:0)
public void FindLoop()
{
Word.Application wordApp = new Word.Application();
wordApp.Visible = false;
object missing = System.Type.Missing;
object saveChanges = Word.WdSaveOptions.wdSaveChanges;
Word.Documents docs = wordApp.Documents;
docs.Open(filePathAndName, ReadOnly: true);
int intFound = 0;
Word.Range rng = wordApp.ActiveDocument.Content; // this = wordApp.ActiveDocument
rng.Find.ClearFormatting();
rng.Find.Forward = true;
rng.Find.Text = this.textBoxSearch.Text.Trim();
try
{
rng.Find.Execute(
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing);
while (rng.Find.Found)
{
intFound++;
rng.Find.Execute(
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing);
}
MessageBox.Show("Strings found: " + intFound.ToString());
wordApp.ActiveDocument.Close(saveChanges, ref missing, ref missing);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
wordApp.ActiveDocument.Close(saveChanges, ref missing, ref missing);
//wordApp.Application.Quit(ref missing, ref missing, ref missing);
}
}