C# 已建立文本的页码
在word文档(.docx、.doc)中查找和替换文本有多个示例,但是如何查找所查找文本所在的页面 谢谢,,C# 已建立文本的页码,c#,.net,vb.net,office-interop,C#,.net,Vb.net,Office Interop,在word文档(.docx、.doc)中查找和替换文本有多个示例,但是如何查找所查找文本所在的页面 谢谢,, Mohsen您可以对Find()的结果使用信息(Word.WdInformation.wdActiveEndAdjustedPageNumber)方法,并将其转换为int,以获取找到文本的页码 发布你的代码,我会更新它以返回页码 下面是一个例子: currentPageNumber = word.ActiveDocument.ActiveWindow.Selection.Range.In
Mohsen您可以对
Find()
的结果使用信息(Word.WdInformation.wdActiveEndAdjustedPageNumber)
方法,并将其转换为int
,以获取找到文本的页码
发布你的代码,我会更新它以返回页码
下面是一个例子:
currentPageNumber = word.ActiveDocument.ActiveWindow.Selection.Range.Information(
Microsoft.Office.Interop.Word.WdInformation.wdActiveEndAdjustedPageNumber)
这是获取页码的方法。根据您的需要,您可能需要使用与页码相关的两个枚举器中的一个。我在此处找到了此链接,可能会对您有所帮助:谢谢。但我没有信用卡。:-)@莫森:你可以通过谷歌找到这个页面,然后它是免费的。使用标题作为关键字。
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);
}
}