Word Interop C#-将列表插入保存嵌套的列表

问题描述

我有一堆Word文档,其中一个是模板。在此模板文件中,我具有“ $ clause#”关键字来指示必须在何处粘贴现有子句#.docx文档的内容。此“ $ clause#”关键字可以在列表内,子句#.docx文件也可以在其中包含多级列表。问题是:如何替换$ clause#并保存列表顺序,将Clause#.docx文件内容插入template.docx文件。我几乎尝试了所有东西,IMO。 PasteAndFormat,尝试在书签之间粘贴内容并应用格式。我只是不知道如何使这个/ awesome /库工作。几乎所有关于它的事情都是违反直觉的,我将要放弃。

Template file image

Clause3 file contents image

Actual result image

foreach (string tokenFile in tokenFiles)
        {
            string tokenText = "$" + Path.GetFileNameWithoutExtension(tokenFile).ToLowerInvariant();
            Console.WriteLine(tokenFile);
            Document tokenDoc = app.Documents.Open(appDir + tokenFile);

            tokenDoc.Activate();
            tokenDoc.ActiveWindow.Selection.WholeStory();

            templateDoc.Activate();
            templateDoc.ActiveWindow.Selection.Find.Execute(tokenText);
            var templateFontSize = templateDoc.ActiveWindow.Selection.FormattedText.Font.Size;
            var templateSpacing = templateDoc.ActiveWindow.Selection.FormattedText.Font.Spacing;
            var templateFontName = templateDoc.ActiveWindow.Selection.FormattedText.Font.Name;
            var templateLinespacing = templateDoc.ActiveWindow.Selection.FormattedText.ParagraphFormat.Linespacing;

            tokenDoc.Activate();
            tokenDoc.ActiveWindow.Selection.copy();

            templateDoc.Activate();
            templateDoc.ActiveWindow.Selection.Collapse(WdCollapseDirection.wdCollapseEnd);

            templateDoc.Bookmarks.Add(tokenText.Replace("$","") + "BM1");
            templateDoc.Bookmarks.Add(tokenText.Replace("$","") + "BM2");
            int bmk1End = templateDoc.Bookmarks[bmcnt].Range.End;
            int bmk2Start = templateDoc.Bookmarks[bmcnt + 1].Range.Start;
            Range rng = templateDoc.Range(bmk1End,bmk2Start);
            rng.Paste();

            rng.Font.Size = templateFontSize;
            rng.Font.Spacing = templateSpacing;
            rng.Font.Name = templateFontName;
            rng.ParagraphFormat.Linespacing = templateLinespacing;
            bmcnt += 2; //It's just a counter

            tokenDoc.Close();
        }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)