C# word自动化目录
我有一个文档,其中有一些文本和一个目录C# word自动化目录,c#,c#-4.0,word-automation,C#,C# 4.0,Word Automation,我有一个文档,其中有一些文本和一个目录 foreach (Word.TableOfContents toC in wordDoc.TablesOfContents) { toC.Update(); } 但是标题开头有相同的词,我想在目录中删除该词 我试过类似的东西 toC.Range.Text = toC.Range.Text.Replace("text to be removed", ""); 但现在看起来一团糟 对齐和间距被
foreach (Word.TableOfContents toC in wordDoc.TablesOfContents)
{
toC.Update();
}
但是标题开头有相同的词,我想在目录中删除该词
我试过类似的东西
toC.Range.Text = toC.Range.Text.Replace("text to be removed", "");
但现在看起来一团糟
对齐和间距被破坏了,有人知道如何在不影响目录布局的情况下更改文本吗
看来我走到了死胡同
如果有其他方法可以做到这一点,比如在模板中,如果我可以告诉目录在生成指定文本时用emoty字符串替换指定文本,是否有类似的情况 不确定这是否是直接编辑目录内容的最佳方式。 也许可以尝试这种方法
好吧,我是这样解决的
object findText = "text to be removed";
object replaceText = string.Empty;
object item = Word.WdGoToItem.wdGoToPage;
object whichItem = Word.WdGoToDirection.wdGoToFirst;
object replaceAll = Word.WdReplace.wdReplaceAll;
object forward = true;
object matchAllWord = true;
toC.Range.Document.GoTo(ref item, ref whichItem, ref missing, ref missing);
toC.Range.Find.Execute(ref findText, ref missing, ref matchAllWord,
ref missing, ref missing, ref missing, ref forward,
ref missing, ref missing, ref replaceText, ref replaceAll,
ref missing, ref missing, ref missing, ref missing);
但是你不应该更新目录,或者你需要手动替换文本这是一个很好的解决方案,但在我的应用程序中,我希望它们都是相同的样式,找到了一种方法来替换文本,而不会弄乱目录