如何替换页眉/页脚中的"FIELD"?
例如:带有文件名和日期的Word doc文件.代替文件路径 - [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]");
}
Run Code Online (Sandbox Code Playgroud)
这适用于文件名,但是对于Date,无法猜测要替换的格式.这都是因为我无法捕获要替换的确切字段信息.
以下代码也是我无法使用的
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);
Run Code Online (Sandbox Code Playgroud)
我现在看到的唯一方法是处理所有可能的日期格式并替换,但这对我来说似乎不是一个好方法.
根据使用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 …Run Code Online (Sandbox Code Playgroud)