Microsoft.Offfice.Interop.替换所有文档中的匹配项,但不包括页眉/页脚c#

本文关键字:不包括 页脚 替换 Interop Offfice 文档 Microsoft | 更新日期: 2023-09-27 18:03:56

我正在尝试用。doc模板中的代码"查找和替换"以替换我的应用程序中的一些值,它现在正在工作,但它不替换页眉/页脚,只是word文档的主体(这是伟大的),但我也需要替换页眉/页脚。

这就是我的代码:

FindAndReplace方法:

private void FindAndReplace(Microsoft.Office.Interop.Word.Application wordApp, object findText, object replacewithText)
{
    object matchCase = true;
    object matchWholeWord = true;
    object matchWildCards = false;
    object matchSoundLike = false;
    object nmacthAllForms = false;
    object forward = true;
    object format = false;
    object matchKashida = false;
    object matchDiactitics = false;
    object matchAleftHamza = false;
    object matchControl = false;
    object read_only = false;
    object visible = true;
    object replace = 2;
    object wrap = 1;
     wordApp.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord,
            ref matchWildCards, ref matchSoundLike, ref nmacthAllForms,
        ref forward, ref wrap, ref format, ref replacewithText, ref replace, ref matchKashida,
        ref matchDiactitics, ref matchAleftHamza, ref matchControl);           
}

这是我的方法,它取代了像<date>这样的标签,并被应用程序所需的值所取代:

` public void CreateWordDocument(object filename, object SaveAs, string [] InformationToReplace, int DocumentToSave)
    {
        object missing = Missing.Value;
        word.Application wordApp = new word.Application();
        word.Document aDoc = null;
        if (File.Exists((string)filename))
        {             
            object readOnly = false;
            object isVisible = false;
            wordApp.Visible = false;
            aDoc = wordApp.Documents.Open(ref filename, ref missing, ref readOnly,
                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);
            aDoc.Activate();
            if (DocumentToSave == 0)
                requirements(wordApp, InformationToReplace);
            else if (DocumentToSave == 1)
                TechnicalDesign(wordApp, InformationToReplace);

            //Update the generic Information
             this.FindAndReplace(wordApp, "<Today_Date>", System.DateTime.Now.ToString("yyyy-MM-dd"));
        }
 aDoc.SaveAs2(ref SaveAs, 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);
        aDoc.Close(ref missing, ref missing, ref missing);
}

在这种情况下,如果<Today_Date在word文档中被替换的值,但<Today_Date标签是在页脚,不被替换…

你知道是什么问题吗?

Microsoft.Offfice.Interop.替换所有文档中的匹配项,但不包括页眉/页脚c#

您必须使用Document.StoryRanges[WdStoryType]属性分别检查页眉和页脚。

您还必须单独检查文本框,尽管对于那些,您必须使用Range.ShapeRange从每个段落中获取文本框,检查其类型,并访问其TextFrame.TextRange(有一个WdTextFrameStory,但它只适用于文档中的第一个文本框,所以它不是很有帮助)。

foreach (Range range in doc.StoryRanges)
{
  if (range.StoryType == WdStoryType.wdEvenPagesHeaderStory)
  {
    // do work on header for even pages
  }
  else if (range.StoryType == WdStoryType.wdMainTextStory)
  {
    // do work on main content
    foreach (Paragraph para in range.Paragraphs)
    {
      foreach (Interop.Shape shape in para.Range.ShapeRange)
      {
        if (shape.Type == MsoShapeType.msoTextBox)
        {
          // do work on text box range
        }
      }
    }
  }
  // etc.
}