将一个文档插入另一个文档 Microsoft.Office.Interop.Word

Inserting one document to another Microsoft.Office.Interop.Word

我正在处理 Word VSTO 加载项。我写了一个代码,将一个文档插入另一个文档。我有两种不同的方法。

1) Copy/Paste

var app = new Word.Application();
            var MyDoc = app.Documents.Add(@"C:\install\CSharp\Plank.dotm");
            MyDoc.ActiveWindow.Selection.WholeStory();
            MyDoc.ActiveWindow.Selection.CopyFormat();
            Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
            doc.Activate();
            doc.ActiveWindow.Selection.PasteFormat();

2) InsertFile()

var app = new Word.Application();

            var MyDoc = app.Documents.Add(@"C:\install\CSharp\Plank.dotm");
            Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
            doc.Activate();
                       //Taking margins
            float TopMargin = MyDoc.PageSetup.TopMargin;
            float RightMargin = MyDoc.PageSetup.RightMargin;
            float LeftMargin = MyDoc.PageSetup.LeftMargin;
            Globals.ThisAddIn.Application.Selection.InsertFile(@"C:\install\CSharp\Plank.dotm", Link: false, Attachment: false);

在第一种方法中,行 PasteFormat() 不起作用,告诉我文本属性未被复制。 (如果我只使用 Copy()Paste() 就可以了。)即使我手动粘贴我也能得到我想要的。

第二种方法可行,但它不采用文本格式。所以我得到了一些不同于原始格式和大小的文本。

问题是:如何保留原始字体格式?我试图在 Word 中手动插入一个文件。而且我也无法在那里获得原始文本格式。也许这是一种错误的方法?

我用 Copy() / Paste() 解决方案解决了这个问题。我需要这样复制:

MyDoc.ActiveWindow.Selection.Copy();
doc.ActiveWindow.Selection.PasteAndFormat(Word.WdRecoveryType.wdFormatOriginalFormatting);

然后它保留样式。但有时,如果文档有表格,它会将它们复制到下一页,这很奇怪。 InsertFile() 方法对我不起作用。

PasteFormat 只会粘贴格式,不会粘贴内容,应该在 CopyFormat 方法之后使用。 PasteAndFormat 方法应该用于粘贴带有或不带有 WdRecoveryType 常量定义的格式的内容。在这里你应该使用:

doc.ActiveWindow.Selection.PasteAndFormat(Word.WdRecoveryType.wdFormatOriginalFormatting);

有关更多信息,请阅读 PasteAndFormat method and WdRecoveryType constant

有关此行为的一些背景信息:默认情况下和设计 Word 将使用与 target 文档,以便合并进来的内容 "seamlessly"(从格式的角度来看)。如果传入内容应用了与目标文档中已经存在的样式名称相同的样式名称,则传入内容的格式将发生变化。

如答案所示,使用 Windows 剪贴板时带有参数 Word.WdRecoveryType.wdFormatOriginalFormatting 方法的 PasteAndFormat 将覆盖默认行为并保留原始格式。

然而,通过剪贴板工作并不总是理想的。还有另一种方法,即更改源文档中的样式名称,使其与目标文档中的样式名称不同。如果在源文档中 一致地 使用样式,则此方法将起作用。 (但是,手动应用的格式可能仍会丢失。)

以下代码片段说明了段落样式的方法。它根据文档中使用的样式创建新样式 - 除了名称外,新样式完全相同。然后执行 Find/Replace 以用新样式替换该样式的所有实例。现在可以将内容插入到另一个文档中并保持格式。

    Word.Document doc = wdApp.ActiveDocument;
    Word.Style sOld = null;
    Word.Style sCopy = null;
    Word.Find f = doc.Content.Find;

    //Select Normal text, otherwise Normal will take on character formatting of current selected text
    Word.Selection sel = wdApp.Selection;
    sel.Find.set_Style(Word.WdBuiltinStyle.wdStyleNormal);
    sel.Find.Execute("", Type.Missing,
      Type.Missing, Type.Missing, Type.Missing, Type.Missing, true, Word.WdFindWrap.wdFindStop,
      true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

    foreach (Word.Style s in doc.Styles)
    {
        if ((s.InUse) && (s.Type == Word.WdStyleType.wdStyleTypeParagraph) && 
            (!s.NameLocal.Contains(" Copy")))
        {
            sOld = s;
            sCopy = doc.Styles.Add(sOld.NameLocal + " Copy", sOld.Type);
            sCopy.set_BaseStyle(sOld);
            f.set_Style(sOld);
            f.Replacement.set_Style(sCopy);
            f.Execute("", Type.Missing, 
            Type.Missing, Type.Missing, Type.Missing, Type.Missing, true, Word.WdFindWrap.wdFindStop, 
            true, "", Word.WdReplace.wdReplaceAll, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
        }
    }