Replace Field in Header&Footer in Word Using Interop

前端 未结 3 1948
抹茶落季
抹茶落季 2020-12-31 08:56

How to replace a \"FIELD\" in the header/footer?

Ex: Word doc file with File Name & Date. in place of file path - [FilePath] instead C://Documents/Location/Filen

3条回答
  •  遥遥无期
    2020-12-31 09:34

    object replaceAll = MSWord.WdReplace.wdReplaceAll;
    foreach (Microsoft.Office.Interop.Word.Section section in oDoc.Sections)
    {
        Microsoft.Office.Interop.Word.Range footerRange =  section.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
        footerRange.Find.Text = "Some Text";
        footerRange.Find.Replacement.Text = "Replace Text";
        footerRange.Find.Execute(ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceAll, ref missing, ref missing, ref missing, ref missing);
    }
    

    oDoc is a "MSWord.Document" object which has current doc i.e.

    oDoc = oMSWord.Documents.Open(ref "DocPath", 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);
    

    Then apply loop on "Sections" of current oDoc object. Based on "Sections" you will get range of Footer. Then you will able to find and replace the text in Footer.

提交回复
热议问题