[英]How to find and replace text in header of landscape page of word document [c# word interop]?
我有一个模板 Word 文档,我正在使用一个函数将特定文本替换为我想要的文本。 它适用于整个文档(主体和标题),除了最后一页上的标题是横向页面。
void FindAndReplace2(Microsoft.Office.Interop.Word.Document document, string placeHolder, string newText)
{
object missingObject = null;
object item = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;
object whichItem = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToFirst;
object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
object forward = true;
object matchAllWord = true;
object matchCase = false;
object originalText = placeHolder;
object replaceText = newText;
document.GoTo(ref item, ref whichItem, ref missingObject, ref missingObject);
foreach (Microsoft.Office.Interop.Word.Range rng in document.StoryRanges)
{
rng.Find.Execute(ref originalText, ref matchCase,
ref matchAllWord, ref missingObject, ref missingObject, ref missingObject, ref forward,
ref missingObject, ref missingObject, ref replaceText, ref replaceAll, ref missingObject,
ref missingObject, ref missingObject, ref missingObject);
}
}
我不知道为什么这个功能可以在除横向页面之外的所有内容上工作。 我不知道如何从这里开始。
主要示例文件
object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */
// Start Word and create a new document.
Word.Application oWord;
Word.Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
//Template file
object oTemplate = @"FILEPATH";
oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing, ref oMissing, ref oMissing);
string SiteName="J8865";
FindAndReplace2(oDoc, "Sample", SiteName);
注意:此文件只是我的主文件的摘录。
我以一种非常不同的方式解决了这个问题。 我通过链接纵向页面和横向页面上的标题来编辑我的模板文档。 这是通过选择标题到设计选项卡>>导航>>下一个>>链接到上一个来完成的。 您的格式可能会搞砸。 使用“插入对齐选项卡”来格式化您的标题。 有用的链接。 https://cybertext.wordpress.com/2014/07/25/word-auto-aligning-headerfooter-info-in-portrait-and-landscape-pages/ 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.