2017-08-31 127 views
-2

我在pictureBox中有图片。我想将它插入到docx中。我怎么能这样做?插入图片在Microsoft Office Word中.docx

我已经使用follwing代码来这样做,但它不工作。我如何可以在图像上的docx“图片”中替换?

ReplaceWordStub("{picture}", pictureBox.Image, wordDocument); 

任何帮助将不胜感激。

+1

的可能的复制[查找特定的文本,并在MS Word图片文件替换(https://stackoverflow.com/questions/16456792/find-specific-text-and-replace-with-image-file -in-MS字) – mjwills

回答

0

我认为这是一个不好的代码,但它的工作。

using Word = Microsoft.Office.Interop.Word; 



var wordApp = new Word.Application(); 
      wordApp.Visible = false; 
     try 
     { 

      var wordDocument = wordApp.Documents.Open(TemplateFileName); 
      ReplaceWordStub("{ILL}", dataGridView3.CurrentRow.Cells[3].Value.ToString(), wordDocument); 
      pictureBoxTgt.Image.Save(@"C:\Template\1.jpg", ImageFormat.Jpeg); 
      string fileName = @"C:\Template\1.jpg"; 
      Object oMissed = wordDocument.Paragraphs[1].Range; 
      Object oLinkToFile = false; 
      Object oSaveWithDocument = true; 
      wordDocument.InlineShapes.AddPicture(fileName, ref oLinkToFile, ref oSaveWithDocument, ref oMissed); 
      wordDocument.SaveAs(@"C:\Result\" + name + ".doc"); 
      wordDocument.Close(); 

     } 
     catch 
     { 
      MessageBox.Show("Error!"); 
     } 
     finally 
     { 
      wordApp.Quit(); 
     } 
     File.Delete(@"C:\Template\1.jpg");