使用Interop在Word中的标题和页脚中替换字段 [英] Replace Field in Header&Footer in Word Using Interop

查看:183
本文介绍了使用Interop在Word中的标题和页脚中替换字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



例如:Word文件与文件名日期。代替文件路径 - [FilePath]而不是C://Documents/Location/Filename.doc,[Date] instead 18/07/2013。



任何具有范围的文本。

  foreach(wordDocument.Sections中的Microsoft.Office.Interop.Word.Section部分)
{
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);

只有我现在看到的方式是处理所有可能的日期格式和替换,


$ b

$ b

不给我确切的字段信息说[DATE]。当我迭代通过故事范围类型信息我得到wdstorytype这是关于部分信息,nt关于字段信息。

  foreach(wordDocument.StoryRanges中的Microsoft.Office.Interop.Word.Range tmpRange)
{
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缺少,ref replaceAll,
ref missing,ref missing,ref missing,ref missing);
}

更新:帮助我在这里,但似乎并不工作。任何想法如何可以强制文档对象使用下面的导出前。

  field.ShowCodes = true; 


解决方案

最后经过关于introp.word ,获得解决方案

  //遍历所有节
foreach(Microsoft.Office.Interop.Word.Section节in wordDocument.Sections)
{
wordDocument.TrackRevisions = false; //禁用对字段替换操作的跟踪
//获取所有标题
Microsoft.Office.Interop.Word。 HeadersFooters headers = section.Headers;
//所有类型的headerfooter循环节enum WdHeaderFooterIndex。 wdHeaderFooterEvenPages / wdHeaderFooterFirstPage / wdHeaderFooterPrimary;
foreach(头文件中的Microsoft.Office.Interop.Word.HeaderFooter头)
{
字段fields = header.Range.Fields;
foreach(字段中的字段)
{
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]);

}
}
}
//获取所有页脚
Microsoft.Office.Interop.Word.HeadersFooters footers = section.Footers;
//所有类型的headerfooter循环节enum WdHeaderFooterIndex。 wdHeaderFooterEvenPages / wdHeaderFooterFirstPage / wdHeaderFooterPrimary;
foreach(页脚中的Microsoft.Office.Interop.Word.HeaderFooter页脚)
{
字段fields = footer.Range.Fields;
foreach(字段中的字段)
{
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]);

}
}
}
}


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/Filename.doc ,[Date] instead 18/07/2013.

I can replace any text with range.

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]"); 
}

This works fine for The Filename, however for Date, it is not possible to guess the format to replace. This is all because I'm not able to catch the exact field info to replace.

The below code also I can't use

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

Only way that I see as of now is handle all possible date formats and replace,but this doesn't seems like a good approach to me.

Update as per the comment given using Storyrange.

Doesn't give me the exact Field information saying [DATE].When I iterate through story range the type info I'm getting wdstorytype which is about section information, nt about the field information.

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

Update: Looks something that helps me here, but doesn't seems to be working. Any idea how can i force the document object use the below before export.

field.ShowCodes = true;

解决方案

Finally after going through the poor documentation about introp.word, got the solution

                // 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]");

                             }
                         }
                     }
                }

这篇关于使用Interop在Word中的标题和页脚中替换字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆