【问题标题】:Reading a word document (text and images) and Pasting to other word document阅读word文档(文本和图像)并粘贴到其他word文档
【发布时间】:2018-09-13 04:25:37
【问题描述】:

我有一个包含文本和图像的 Word 文档。当我阅读此文档并将内容写回另一个文档时,图像丢失。 我如何也可以复制图像?

例子:Word文档有

将内容写入其他文档后,输出为

代码示例:

object missing = System.Reflection.Missing.Value;
        object filename = "C:\\SampleInput.docx";
        Microsoft.Office.Interop.Word.ApplicationClass AC = new Microsoft.Office.Interop.Word.ApplicationClass();
        Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
        object readOnly = false;
        object isVisible = true;
        doc = AC.Documents.Open(ref filename, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref isVisible, ref missing, ref missing, ref missing);
        string inputText = doc.Content.Text;
        Microsoft.Office.Interop.Word.Application winword = new Microsoft.Office.Interop.Word.Application();
        winword.ShowAnimation = false;
        winword.Visible = false;
        Microsoft.Office.Interop.Word.Document document = winword.Documents.Add(ref missing, ref missing, ref missing, ref missing);

        document.Content.SetRange(0, 0);
        document.Content.Text = inputText;

        object outputPath = @"c:\FormattedOutput.docx";
        document.SaveAs2(ref outputPath);
        document.Close(ref missing, ref missing, ref missing);
        document = null;
        winword.Quit(ref missing, ref missing, ref missing);
        winword = null;

【问题讨论】:

  • 我同意 RIch 的建议。但是你的代码有问题。不要不要使用ACdoc的结构; not 实例化为 Application.Class 并且在 Word 文档的声明中not 使用 =new Word.Document。这些可能会在某些时候导致各种奇怪的问题。改用 winworddocument 的结构。请注意,您不需要两个文档的Word.Application 实例。启动一个实例并将其用于两个文档。

标签: c# ms-word ms-office


【解决方案1】:

您应该使用 Range.FormattedText 属性。换句话说,在源文档和目标文档打开并建立范围的情况下,命令看起来像这样......

DestinationRange.FormattedText = SourceRange.FormattedText;

您当前的代码仅获取纯文本并将其存储在字符串变量中。字符串变量不能保存图像对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-20
    • 1970-01-01
    相关资源
    最近更新 更多