Word Automation重置编号方案

问题描述

我是使用C#编程的新手,并且正在使用Microsoft.Office.Interop.Word扩展名。我的程序需要几个Word文档,并将它们插入单个文档中。我在重置文档Picture showing my issue

中的编号方案时遇到麻烦

这是我用来插入文档的代码:

        word.Selection.InsertNewPage();
        Word.Paragraph para2;
        para2 = doc.Content.Paragraphs.Add(ref missing);
        para2.Range.Text = "Vertrag";
        para2.Range.Font.Bold = 1;
        para2.Range.Font.Size = 24;
        para2.Range.Font.Name = "Arial";
        para2.Format.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
        para2.Range.InsertParagraphAfter();
        //Resetting the Font to normal
        word.Selection.GoTo(ref gotoPage,ref gotoLast,ref missing,ref missing);
        word.Selection.ClearFormatting();

        //ADDING THE FOURTH PAGE
        word.Selection.InsertNewPage();
        word.Selection.GoTo(ref gotoPage,ref missing);
        doc.Bookmarks.get_Item(ref endOfDoc).Range.InsertFile(filePathLeist,ref oFalse,ref oFalse);
        word.Selection.GoTo(ref gotoPage,ref missing);
        //word.Selection.ClearFormatting();


        //ADDING THE FIFTH PAGE
        word.Selection.GoTo(ref gotoPage,ref missing);
        word.Selection.InsertNewPage();
        string filePathAGB = filePathCommon + "\\effexx_WSIV_allgemein_AGB_2020_V1_0.docx";
        doc.Bookmarks.get_Item(ref endOfDoc).Range.InsertFile(filePathAGB,ref oFalse);

我试图清除两个文档之间的格式,但这没有用 有人可以告诉我我在做错什么,如何解决这个问题?

解决方法

在评论中这样做可能会更好,但我也是新来的人,还不允许评论。

以下页面虽然很旧,但应该会有所帮助:Methods for Restarting Numbering Using vba by Margaret Aldis,Word MVP

看着您的代码,对我来说看起来不错,但是Word中的自动编号被弄乱了。这已经超过二十五年了。 Word Numbering Explained by John McGhie,MVP有两种牢不可破的可靠方法。第一种是将一种编号方案附加到段落样式,如Shauna Kelly的教程“ How to create numbered headings or outline numbering in Word”中所述,每个级别都有一个编号方案。如果您在文档中的编号使用相同的样式,则应无缝集成。

第二种方法已经存在超过25年了,它使用序列字段。该文件效果很好,但在每个文档中都必须以相同的方式进行设置。

通过代码来修复多个文档中的文档编号以使用相同的方法超出了我的能力范围。我希望,任何尝试它的人都需要知道当前的编号方式。 Margaret Aldis建议“您可以创建列表模板,并使用VBA宏将每个级别链接到适当的样式。”如果尝试这样做,我将使用这些样式创建一个模板,然后使用OrganizerCopy method将样式转移到文档中,并通过将适当的样式应用于文档中各个编号的段落来对文档进行编号。但是,这可能会使这些段落的任何格式混乱。 Using the Organizer in Word。可以从我的网站下载example of a template that can do this

为将来使此操作变得容易,您希望在Word中有一个文档模板,该模板用作必须合并的新文档的基础。该模板应设置一个编号方案,并应在每个文档中使用。我会推荐Shauna的方法。要使用它,您的用户应使用适当的样式而不是“主页”选项卡上的按钮来应用编号。

请注意,在讨论Word时,术语“模板”是术语或艺术或专业术语。不要以普通英语阅读它的通常含义。 Templates in Microsoft Word

如果使用序列字段,则可以将字段存储在自动图文集或某种Building Block中。我的page on numbering中对此进行了说明。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...