【问题标题】:how to insert numbering that is linked to styles ms word c#如何插入与样式 ms word c# 链接的编号
【发布时间】:2014-09-08 08:40:58
【问题描述】:

我正在创建一个使用单词自动化的应用程序。我正在使用 Visual Studio 2010、Microsoft Word 2010 并使用互操作。

我的word文档中有以下3种样式。

如您所见,我已经对样式进行了格式化,以便它们在编号时达到自己的水平。 我希望应用程序做的是打开一个带有编号项目符号的现有文档(应用程序可以这样做),然后添加更多(见图)。

到目前为止,我已经尝试过了,但它似乎没有遵循我手动放入样式的格式。

    public void insertTextHeading(Word.Document document, string bookmark, string text, int fontSize, string headingType, int listNumber, int bulletLevel)
    {
        var start = document.Bookmarks[bookmark].Start;
        var end = document.Bookmarks[bookmark].End;
        Word.Range range = document.Range(start, end);

        //The text design
        range.Font.Name = "Verdana";
        range.Font.Size = fontSize;
        range.set_Style(headingType);

        object n = listNumber;
        Word.ListTemplate template =
            document.Application.ListGalleries[Word.WdListGalleryType.wdNumberGallery].ListTemplates.get_Item(ref n);

        object bContinuePrevList = true;
        object applyTo = Word.WdListApplyTo.wdListApplyToSelection;
        object defBehavior = Word.WdDefaultListBehavior.wdWord10ListBehavior;
        object missing = System.Reflection.Missing.Value;

        range.ListFormat.ApplyListTemplateWithLevel(
        template, ref bContinuePrevList,
        ref applyTo, ref defBehavior, ref missing);
        range.Text = text;
    }

【问题讨论】:

    标签: c# ms-word interop


    【解决方案1】:

    有时你自己的问题的答案是你是个白痴。 几个小时后,我决定调试我拥有的每一行代码,结果发现是我手动添加到 word 模板的样式创建了编号本身。所以我尝试通过代码添加编号,而我所要做的就是告诉要插入的文本是我想要的样式。

    【讨论】:

    • 我试图通过修改样式,然后应用这些样式来达到相同的结果,但遇到了问题。如果你有一些时间,你能分享你的代码是如何工作的吗? stackoverflow.com/questions/37353021/…
    猜你喜欢
    • 2017-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多