使用Interop替换Word中的页眉和页脚中的字段

时间:2013-07-18 04:38:01

标签: c#-4.0 ms-word office-interop

如何替换页眉/页脚中的“FIELD”?

Ex:带有文件名和文件名的Word文档文件日期。代替文件路径 - [FilePath]而不是C://Documents/Location/Filename.doc, [Date]而不是18/07/2013。

我可以用范围替换任何文本。

foreach (Microsoft.Office.Interop.Word.Section section in wordDocument.Sections)
{
   section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text.Replace(sourceDocPath, "[File Path]");

   section.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text.Replace(sourceDocPath, "[File Path]"); 
}

这适用于文件名,但是对于日期,无法猜测要替换的格式。这都是因为我无法捕捉到要替换的确切字段信息。

以下代码我也不能使用

wordApp.Selection.Find.Execute(ref textToReplace, ref typeMissing, 
        ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, 
        ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, 
        ref replaceTextWith, ref replaceAll, ref typeMissing, ref typeMissing, 
        ref typeMissing, ref typeMissing);

我现在看到的唯一方法是处理所有可能的日期格式并替换,但这对我来说似乎不是一个好方法。

根据使用Storyrange发表的评论进行更新。

没有给我确切的字段信息说[日期]。当我遍历故事范围时,我得到的类型信息是wdstorytype,这是关于部分信息,关于字段信息。

foreach (Microsoft.Office.Interop.Word.Range tmpRange in wordDocument.StoryRanges)
                    {
                        string strtype = tmpRange.StoryType.ToString();
                        tmpRange.Find.Text = "18/07/2013";
                        tmpRange.Find.Replacement.Text = "";
                        tmpRange.Find.Replacement.ParagraphFormat.Alignment =
                            Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphJustify;

                        tmpRange.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
                        object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;

                        tmpRange.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);
                    }

更新  看起来在这里帮助我的东西,但似乎没有工作。任何想法如何在导出之前强制文档对象使用下面的内容。

field.ShowCodes = true;

3 个答案:

答案 0 :(得分:11)

最后,在浏览了关于introp.word的糟糕文档之后,得到了解决方案

                // Loop through all sections
                foreach (Microsoft.Office.Interop.Word.Section section in wordDocument.Sections)
                {
                     wordDocument.TrackRevisions = false;//Disable Tracking for the Field replacement operation
                   //Get all Headers
                     Microsoft.Office.Interop.Word.HeadersFooters headers=section.Headers;
                    //Section headerfooter loop for all types enum WdHeaderFooterIndex. wdHeaderFooterEvenPages/wdHeaderFooterFirstPage/wdHeaderFooterPrimary;                          
                     foreach (Microsoft.Office.Interop.Word.HeaderFooter header in headers)
                     {
                        Fields fields= header.Range.Fields;
                        foreach (Field field in fields)
                        {
                            if (field.Type == WdFieldType.wdFieldDate)
                            {
                                field.Select();
                                field.Delete();
                                wordApplication.Selection.TypeText("[DATE]");
                            }
                            else if (field.Type == WdFieldType.wdFieldFileName)
                            {
                                field.Select();
                                field.Delete();
                                wordApplication.Selection.TypeText("[FILE NAME]");

                            }
                        }
                     }
                     //Get all Footers
                     Microsoft.Office.Interop.Word.HeadersFooters footers = section.Footers;
                     //Section headerfooter loop for all types enum WdHeaderFooterIndex. wdHeaderFooterEvenPages/wdHeaderFooterFirstPage/wdHeaderFooterPrimary; 
                     foreach (Microsoft.Office.Interop.Word.HeaderFooter footer in footers)
                     {
                         Fields fields = footer.Range.Fields;
                         foreach (Field field in fields)
                         {
                             if (field.Type == WdFieldType.wdFieldDate)
                             {
                                 field.Select();
                                 field.Delete();
                                 wordApplication.Selection.TypeText("[DATE]");
                             }
                             else if (field.Type == WdFieldType.wdFieldFileName)
                             {
                                 field.Select();
                                 field.Delete();
                                 wordApplication.Selection.TypeText("[FILE NAME]");

                             }
                         }
                     }
                }

答案 1 :(得分:2)

周杰伦,

也许有点迟了但无论如何......

无法评论,所以我回答。

根据当前接受的答案,有一天我可能对你(或其他人)有用。

  1. 要回答上一次更新中的问题,您可以使用类似的内容打开域代码,然后使用“查找”搜索字段内容:

    wordDocument.ActiveWindow.View.ShowFieldCodes = true;
    

    所以,你可以在搜索之前将其打开(除非已经开启),并在完成后将其恢复。

  2. 您提供给自己的解决方案适用于大多数情况,我曾经使用过类似的东西。但是,我碰到了一个包含2000个部分的文档。循环遍历这些部分将依次循环遍历相同的标题。在我的情况下,处理文档超时(给定可接受的处理时间)

  3. 使用StoryRanges的解决方案可能是更好的方法(与切换字段代码相结合)
    使用它的一些例子(通用搜索和替换):
    http://word.mvps.org/faqs/customization/ReplaceAnywhere.htm
    https://wls.wwco.com/blog/2010/07/03/find-and-replace-in-word-using-c-net/

  4. 要记住一件事:不要忘记在范围形状中搜索东西

  5. 我猜你想出了如何替换这个领域。无论如何,我实际上是将简单文本转换为字段。

    一旦Find.Execute命中了某个东西,就会选择范围而我

    theDoc.Fields.Add(range, WdFieldType.wdFieldDocVariable, "myDocVar");
    
  6. TL; DR:如果您的文档格式可预测且只有少量部分且文字不在形状内,那么请不要担心这一切。

答案 2 :(得分:0)

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是一个“MSWord.Document”对象,它有当前的文档,即

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);

然后在当前oDoc对象的“Sections”上应用循环。基于“章节”,您将获得页脚范围。然后,您将能够在页脚中找到并替换文本。