Word VSTO - Filling a shape does not work in Word 2010 and Word 2013?

心已入冬 提交于 2019-12-20 05:46:09

问题


I use the following VB.NET (VSTO) code to add a shape in MS-Word,

Dim app As Word.Application = Globals.ThisAddIn.Application
Dim doc As Word.Document = app.ActiveDocument
Dim left As Single = CSng(Convert.ToDouble(app.Selection.Information(Word.WdInformation.wdHorizontalPositionRelativeToPage)))
Dim top As Single = CSng(Convert.ToDouble(app.Selection.Information(Word.WdInformation.wdVerticalPositionRelativeToPage)))
Dim shape As Word.Shape = doc.Shapes.AddShape(1, left, top, 225.1F, 224.5F)

shape.Fill.BackColor.RGB = ColorTranslator.ToOle(Color.Transparent)
shape.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoFalse
shape.Fill.Transparency = 0.0F
shape.Line.Transparency = 0.0F
shape.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse
shape.Fill.UserPicture("C:\Newfolder\App1.jpg")

What this code does is, it adds a rectangle shape at cursor point, makes it transparent (both background and line) and adds (fills) an image.

This works fine in Word 2007. But on Word 2010 and Word 2013 there is an issue. It adds the rectangle shape at cursor point and makes it transparent. But it does not fill the image.

shape.Fill.UserPicture("C:\Newfolder\App1.jpg")

The above line of code does not work in Word 2010 and Word 2013. Other parts works fine. How to modify the code to fill the image in the rectangle shape in Word 2010 and 2013?


回答1:


Instead of shape.Fill.UserPicture("C:\Newfolder\App1.jpg") try

    Word.Range range = shape1.TextFrame.TextRange;
    range.InlineShapes.AddPicture(@"C:\Newfolder\App1.jpg", false, true, Type.Missing);


来源:https://stackoverflow.com/questions/31589137/word-vsto-filling-a-shape-does-not-work-in-word-2010-and-word-2013

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!