Microsoft.Offfice.Interop.Word,替换所有文档中的匹配但不替换页眉/页脚C#

时间:2016-08-25 18:37:12

标签: c# .net ms-word office-interop

我试图"查找和替换"使用.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被替换为值,但<Today_Date标记位于页脚中,则不会替换...

任何想法是什么问题?

1 个答案:

答案 0 :(得分:-1)

您必须使用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.
}