2017-08-31 125 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");