-1

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

            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);

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

4

2 回答 2

1
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;
于 2021-04-14T21:33:03.110 回答
0

似乎 Word 对象模型不接受内联形状作为Hyperlinks.Add方法的锚点。尝试改用范围对象。您也可以尝试使用宏记录器来发现需要什么对象作为锚点。

于 2021-04-14T20:18:54.197 回答