使用Word.Interop创建多级项目符号列表

Flo*_*ers 5 c# vsto ms-word ms-office

我需要创建一个多级子弹列表Microsoft.Office.Interop.Word,我目前正在努力解决其(可怕的)API(再次).

我刚刚用编程语言C#在Microsoft Office Word 2010的VSTO文档级项目中创建了以下示例(不是动态的,仅用于演示目的):

Word.Paragraph paragraph = null;
Word.Range range = this.Content;
paragraph = range.Paragraphs.Add();
paragraph.Range.Text = "Item 1";
paragraph.Range.ListFormat.ApplyBulletDefault(Word.WdDefaultListBehavior.wdWord10ListBehavior);
// ATTENTION: We have to outdent the paragraph AFTER its list format has been set, otherwise this has no effect.
// Without this, the the indent of "Item 2" differs from the indent of "Item 1".
paragraph.Outdent();

paragraph.Range.InsertParagraphAfter();

paragraph = range.Paragraphs.Add();
paragraph.Range.Text = "Item 1.1";
// ATTENTION: We have to indent the paragraph AFTER its text has been set, otherwise this has no effect.
paragraph.Indent();
paragraph.Range.InsertParagraphAfter();

paragraph = range.Paragraphs.Add();
paragraph.Range.Text = "Item 1.2";
paragraph.Range.InsertParagraphAfter();

paragraph = range.Paragraphs.Add();
paragraph.Range.Text = "Item 2";
paragraph.Outdent();
Run Code Online (Sandbox Code Playgroud)

代码完全符合我的要求(经过大量的尝试和错误!),但在我看来这很糟糕.格式必须在非常特定的点应用,我必须手动缩进并突出创建的段落.

所以我的问题是:是否有更好的方法来创建一个多级子弹列表Word.Interop,例如通过我尚未发现的简写方法?

我的目标是从XML数据创建一个多级列表(更具体的CustomXMLNode对象)

Stack Overflow上存在与子弹列表相关的另外两个问题,但两者都没有帮助我(上面的源代码是对第二个问题的一个答案):

编辑(2013-08-08):

我刚刚将一些东西混合在一起,输出两个数组作为一个带有两个级别的子弹列表(带有子项的数组用于每个根项目,以保持简单).通过引入递归,可以创建具有无限级别的子弹列表(理论上).但问题仍然存在,代码很乱......

string[] rootItems = new string[]
{
    "Root Item A", "Root Item B", "Root Item C"
};

string[] subItems = new string[]
{
    "Subitem A", "Subitem B"
};

Word.Paragraph paragraph = null;
Word.Range range = this.Content;
bool appliedListFormat = false;
bool indented = false;

for (int i = 0; i < rootItems.Length; ++i)
{
    paragraph = range.Paragraphs.Add();
    paragraph.Range.Text = rootItems[i];

    if (!appliedListFormat)
    {
        paragraph.Range.ListFormat.ApplyBulletDefault(Word.WdDefaultListBehavior.wdWord10ListBehavior);
        appliedListFormat = true;
    }

    paragraph.Outdent();
    paragraph.Range.InsertParagraphAfter();

    for (int j = 0; j < subItems.Length; ++j)
    {
        paragraph = range.Paragraphs.Add();
        paragraph.Range.Text = subItems[j];

        if (!indented)
        {
            paragraph.Indent();
            indented = true;
        }

        paragraph.Range.InsertParagraphAfter();
    }

    indented = false;
}

// Delete the last paragraph, since otherwise the list ends with an empty sub-item.
paragraph.Range.Delete();
Run Code Online (Sandbox Code Playgroud)

编辑(2013-08-12):

上个星期五,我以为我已经实现了我想要的目标,但今天早上我注意到,只有当插入点位于文档的末尾时,我的解决方案才有效.我创建了以下简单示例来演示(错误的)行为.结束我的问题:我只能在文档的末尾创建多级项目符号列表.一旦我更改当前选择(例如,更改为文档的开头),列表就会被销毁.据我所知,这与对象的(自动或非自动)扩展有关Range.到目前为止我已经尝试了很多(我几乎失去了它),但这对我来说都是货物崇拜.我想这样做的唯一的事情是插入一个元素又一个(是不可能创建内容控制里面的一段,这样,该段的文字之后是内容控制?) ,并在任何给到RangeDocument.我将在CustomXMLPart今天晚上用我的实际装订课在GitHub上创建一个Gist .最终有人可以帮我解决这个麻烦的问题.

private void buttonTestStatic_Click(object sender, RibbonControlEventArgs e)
{
    Word.Range range = Globals.ThisDocument.Application.Selection.Range;
    Word.ListGallery listGallery = Globals.ThisDocument.Application.ListGalleries[Word.WdListGalleryType.wdBulletGallery];
    Word.Paragraph paragraph = null;
    Word.ListFormat listFormat = null;

    // TODO At the end of the document, the ranges are automatically expanded and inbetween not?

    paragraph = range.Paragraphs.Add();
    listFormat = paragraph.Range.ListFormat;
    paragraph.Range.Text = "Root Item A";
    this.ApplyListTemplate(listGallery, listFormat, 1);
    paragraph.Range.InsertParagraphAfter();

    paragraph = paragraph.Range.Paragraphs.Add();
    listFormat = paragraph.Range.ListFormat;
    paragraph.Range.Text = "Child Item A.1";
    this.ApplyListTemplate(listGallery, listFormat, 2);
    paragraph.Range.InsertParagraphAfter();

    paragraph = paragraph.Range.Paragraphs.Add();
    listFormat = paragraph.Range.ListFormat;
    paragraph.Range.Text = "Child Item A.2";
    this.ApplyListTemplate(listGallery, listFormat, 2);
    paragraph.Range.InsertParagraphAfter();

    paragraph = paragraph.Range.Paragraphs.Add();
    listFormat = paragraph.Range.ListFormat;
    paragraph.Range.Text = "Root Item B";
    this.ApplyListTemplate(listGallery, listFormat, 1);
    paragraph.Range.InsertParagraphAfter();
}

private void ApplyListTemplate(Word.ListGallery listGallery, Word.ListFormat listFormat, int level = 1)
{
    listFormat.ApplyListTemplateWithLevel(
        listGallery.ListTemplates[level],
        ContinuePreviousList: true,
        ApplyTo: Word.WdListApplyTo.wdListApplyToSelection,
        DefaultListBehavior: Word.WdDefaultListBehavior.wdWord10ListBehavior,
        ApplyLevel: level);
}
Run Code Online (Sandbox Code Playgroud)

编辑(2013-08-12):我在这里设置了一个GitHub存储库,它演示了我Word.Range对象的问题.OnClickButton文件中的方法Ribbon.cs调用我的自定义映射器类.那里的评论描述了这个问题.我知道我的问题与参数Word.Range对象引用有关,但我尝试的所有其他解决方案(例如修改类内部的范围)都失败了.到目前为止,我已经实现的最佳解决方案是将Document.Content范围指定为MapToCustomControlsIn方法的参数.这会将格式良好的多级项目符号列表(包含绑定到内容控件的自定义XML部分)插入到文档末尾.我想要的是将该列表插入到文档中的自定义位置(例如,当前选择通过Word.Selection.Range).

小智 1

Florian Wolters 的例子差不多就到了,但是当我尝试时,第一个子项目编号总是不正确。

有人建议使用宏和 VBA 脚本然后转换为 C# 给了我灵感。

下面是在我这边测试过的示例代码。希望能帮助到你。

using Microsoft.Office.Interop.Word;
using System.Reflection;

namespace OfficeUtility
{
    public class NumberListGenerate
    {
        public void GenerateList()
        {
            Application app = null;
            Document doc = null;
            string filePath = "c:\\output.docx";
            string pdfPath = "c:\\export.pdf";

            try
            {
                app = new Application();
                app.Visible = false;    // Open Microsoft Office in background
                doc = app.Documents.Open(filePath, Missing.Value, false);

                Range range = doc.Range();
                string search = "$list";
 
                // Find in document to generate list
                while (range.Find.Execute(search))
                {
                    ListGallery listGallery = 
                        app.ListGalleries[WdListGalleryType.wdNumberGallery];

                    // Select found location
                    range.Select();

                    // Apply multi level list
                    app.Selection.Range.ListFormat.ApplyListTemplateWithLevel(
                        listGallery.ListTemplates[1],
                        ContinuePreviousList: false,
                        ApplyTo: WdListApplyTo.wdListApplyToWholeList, 
                        DefaultListBehavior: WdDefaultListBehavior.wdWord10ListBehavior);

                    // First level
                    app.Selection.TypeText("Root Item A");  // Set text to key in
                    app.Selection.TypeParagraph();  // Simulate typing in MS Word

                    // Go to 2nd level
                    app.Selection.Range.ListFormat.ListIndent();
                    app.Selection.TypeText("Child Item A.1");
                    app.Selection.TypeParagraph();
                    app.Selection.TypeText("Child Item A.2");
                    app.Selection.TypeParagraph();

                    // Back to 1st level
                    app.Selection.Range.ListFormat.ListOutdent(); 
                    app.Selection.TypeText("Root Item B");
                    app.Selection.TypeParagraph();

                    // Go to 2nd level
                    app.Selection.Range.ListFormat.ListIndent();
                    app.Selection.TypeText("Child Item B.1");
                    app.Selection.TypeParagraph();
                    app.Selection.TypeText("Child Item B.2");
                    app.Selection.TypeParagraph();

                    // Delete empty item generated by app.Selection.TypeParagraph();
                    app.Selection.TypeBackspace();
                }

                // Save document
                doc.Save();

                // Export to pdf 
                doc.ExportAsFixedFormat(pdfPath, WdExportFormat.wdExportFormatPDF);                   
            }
            catch (System.Exception ex)
            {
                LogError(ex);
            }
            finally
            {
                if (doc != null)
                {
                    // Need to close the document to prevent deadlock
                    doc.Close(false);
                    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(doc);
                }

                if (app != null)
                {
                    app.Quit();
                    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(app);
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)