Jay*_*Jay 12 ms-word office-interop c#-4.0
如何替换页眉/页脚中的"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 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);
}
Run Code Online (Sandbox Code Playgroud)
更新: 看起来有帮助我的东西,但似乎没有工作.任何想法如何在导出之前强制文档对象使用下面的内容.
field.ShowCodes = true;
Run Code Online (Sandbox Code Playgroud)
Jay*_*Jay 14
最后,在浏览了关于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]");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14804 次 |
| 最近记录: |