Creating a multi-level bullet list with Word.Interop

半世苍凉 提交于 2019-12-03 08:52:54

Florian Wolters example almost there, but the first child item numbering always not correct when I tried.

Someone gave me inspiration by suggesting using Macro and VBA script then convert to C#.

Below is the sample code tested works at my side. Hope it helps.

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);
                }
            }
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!