(1) 在vs中添加引用对Microsoft.Office.Interop.Word.dll的引用。
(2) 源文件增加引用:
using System.IO;
using System.Reflection;
using MWord = Microsoft.Office.Interop.Word;
(3) 增加代码:
//doc路径 图片路径 文字
private void test(string docPath , string picPath , string words)
{
Microsoft.Office.Interop.Word.Application oWord;
Microsoft.Office.Interop.Word.Document oDoc;
object name = docPath;
object Range = System.Reflection.Missing.Value;
object bookmarks = “picture”;
object bookmarksdata = “data”;
oWord = new Microsoft.Office.Interop.Word.Application();
oWord.Visible = false;
object unknow = Type.Missing;
//打开文档
oDoc = oWord.Documents.Open(ref name, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow);
oDoc.Bookmarks.get_Item(ref bookmarks).Select();
//插入图片 并设置图片大小
MWord.InlineShape il = oWord.Selection.InlineShapes.AddPicture(picPath, ref unknow, ref unknow, ref Range);
il.Width = 30;
il.Height = 20;
oDoc.Bookmarks.get_Item(ref bookmarksdata).Select();
//插入图片 并设置图片大小
oWord.Selection.TypeText(words); // 插入文本
oDoc.Save();
}
(4) 对word的操作,在需要加载图片和文字的地方,点击插入,选择书签,图片的书签叫picture,日期的书签叫data。