使用互操作服务在word文档的页脚中查找字符串 [英] Find a string in footer of word document using interop service
问题描述
           
          Word.HeaderFooter页脚;
          Word.Range footerRange;
$
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
object miss = System.Reflection.Missing.Value;
          object path = filePath;
          object readOnly = false;
Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(ref path,ref miss,ref readOnly,ref miss,ref miss,ref miss,ref miss,ref miss,ref miss,ref miss,ref miss, ref miss,ref miss,ref miss,ref miss,ref miss);
$
for(int i = 1; i< = docs.Sections.Count ; i ++)
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;试试
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; section = docs.Sections [i];
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(section!= null)
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {
$
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; footer = section.Footers [Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; footerRange = footer.Range;
}
}
Word.HeaderFooter footer;
Word.Range footerRange;
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
object miss = System.Reflection.Missing.Value;
object path = filePath;
object readOnly = false;
Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(ref path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
for (int i = 1; i <= docs.Sections.Count; i++)
{
try
{
section = docs.Sections[i];
if (section != null)
{
footer = section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
footerRange = footer.Range;
}
}
需要找到两个页脚名称在页脚中可用。
Need to find two string names are available in the footer.
检查字符串"业务"或"个人"或在页脚中,如果有任何字符串可用,则返回该字符串。
Check string "Business" or "Personal" in the footer , if any of the string is available return the string.
推荐答案
您好usapnew,
Hi usapnew,
>>如果任何字符串可用,则返回字符串。
>>if any of the string is available return the string.
将返回哪个字符串?
您可以使用
Indexof 比较页脚中的字符串和"Business"/"Personal"。
You could use Indexof to compare string in footer and "Business"/"Personal".
如果比较结果> 0,我们可以知道页脚包含字符串。
If compared result > 0, we could know the footer contains the string.
以下是示例。
if (footerRange.Text.IndexOf("Business") > 0) {
MessageBox.Show("Business");
}
else if (footerRange.Text.IndexOf("Personal") > 0) {
MessageBox.Show("Personal");
}
最好的问候,
Terry
这篇关于使用互操作服务在word文档的页脚中查找字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!