首页 > 解决方案 > 使用 word-interop 将图像添加到 word

问题描述

我正在尝试使用超链接插入图像以下是我的代码

            Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
            WordApp.Documents.Add();

            WordApp.Visible = true;
            Microsoft.Office.Interop.Word.Document doc = WordApp.ActiveDocument;
            Microsoft.Office.Interop.Word.Range drange = doc.Range();
            Microsoft.Office.Interop.Word.InlineShape picture = 
            drange.InlineShapes.AddPicture("c:\\logo.png", Type.Missing, Type.Missing, Type.Missing);

            // noew add the hyperlink to object of inlineshape
            drange.Hyperlinks.Add(picture, "http:\\www.google.com", Type.Missing, Type.Missing, Type.Missing, Type.Missing);

但是当我运行该项目时,我收到一个错误 在此处输入图像描述 ,有人知道为什么会发生这种情况,或者我该如何解决它

标签: c#vstoword-interop

解决方案


Microsoft.Office.Interop.Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;                            
Microsoft.Office.Interop.Word.Application objApplication = Globals.ThisAddIn.Application;
Microsoft.Office.Interop.Word.Selection objSelection = objApplication.Selection;
Microsoft.Office.Interop.Word.Paragraphs p = objSelection.Paragraphs;
Microsoft.Office.Interop.Word.Range objRange = objSelection.Range;

Microsoft.Office.Interop.Word.InlineShape ils = objRange.InlineShapes.AddPicture(@"C:\..\image.PNG");
float scaledWidth = ils.Width;
float scaledHeight = ils.Height;

推荐阅读