在microsoft office word .docx中插入图片

Insert picture in microsoft office word .docx

我在 pictureBox 中有图片。我想将它插入到 docx 中。我该怎么做?

我已经使用了以下代码来执行此操作,但它不起作用。我如何替换图像上的 docx "picture"?

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

任何帮助将不胜感激。

我认为这是一个错误的代码,但它可以工作。

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.jpg", ImageFormat.Jpeg);
            string fileName = @"C:\Template.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.jpg");