【问题标题】:Not able to insert image after the current one in word with microsoft.office.interop.word无法使用 microsoft.office.interop.word 在 word 中的当前图像之后插入图像
【发布时间】:2015-05-29 21:41:20
【问题描述】:

我正在尝试打开一个现有的 word 文件并将图像插入其中(图像名称以及传递给函数的路径)。

代码逻辑工作正常,但挑战是每次插入新图像文件/放置在最后一个文件的顶部。意味着最新图像显示在 word 文件的第一页上,其余的都是相应的。 但我的目标是最古老的将在顶部。或者您可以说第一个将成为第一个宫殿,然后是第二个,第三个.....

这是我的代码示例。

using WordC = Microsoft.Office.Interop.Word;

public void insertImage(string docFileName, string imgFilename)
{
    WordC.Application wordApp = new WordC.Application();
    //  create Word document object
    WordC.Document aDoc = null;
    object readOnly = false;
     object isVisible = false;
     wordApp.Visible = false;
    //  wordApp.DisplayAlerts = false;
    aDoc = wordApp.Documents.Open(docFileName, Type.Missing, ref readOnly, 
        Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
        Type.Missing, Type.Missing, Type.Missing, ref isVisible, Type.Missing, 
        Type.Missing, Type.Missing, Type.Missing);
     aDoc.Activate();
    // WordC.Document doc = WordApp.Documents.Open(docFileName);
    // now add the picture in active document reference
    aDoc.InlineShapes.AddPicture(imgFilename, Type.Missing, Type.Missing, Type.Missing);

    aDoc.Save();
    aDoc.Close(Type.Missing, Type.Missing, Type.Missing);
    wordApp.Quit(Type.Missing, Type.Missing, Type.Missing);
}

【问题讨论】:

    标签: c# selenium-webdriver ms-word office-interop


    【解决方案1】:

    我已经修改了下面的代码。我正在传递范围对象,它现在解决了我的问题。如果有的话,仍在寻找更好的解决方案。

    object o_CollapseEnd = WordC.WdCollapseDirection.wdCollapseEnd;
                     WordC.Range imgrng = aDoc.Content;
                     imgrng.Collapse(ref o_CollapseEnd);
                     imgrng.InlineShapes.AddPicture(imgFilename, Type.Missing, Type.Missing,imgrng);
    

    【讨论】:

      【解决方案2】:

      我使用了这个word to PDF 代码,它将您在word 文档中的所有图像转换为PDF 文档。

      也许你会觉得它有用!!!

      【讨论】:

      • 它与我的问题无关。 :)
      猜你喜欢
      • 2012-04-22
      • 2019-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-12
      • 2018-06-12
      • 2017-06-28
      • 1970-01-01
      相关资源
      最近更新 更多