Codename K
Codename K

Reputation: 744

Word VSTO (VB.NET) - Get the image location, size, etc,?

I am creating an add-in using VB.NET VSTO for MS-Word. In this program I need to retrieve the details such as Location (Left, Top) and Size (Height, Weight) of all the images on the document. I also want to retrieve the page number where the image is located. I use the following code,

        Dim i As Integer
        For i = 1 To Globals.ThisAddIn.Application.ActiveDocument.InlineShapes.Count
            If Globals.ThisAddIn.Application.ActiveDocument.InlineShapes(i).Type = Word.WdInlineShapeType.wdInlineShapePicture Then
                strHeight = Globals.ThisAddIn.Application.ActiveDocument.InlineShapes(i).ScaleHeight()
                strWidth = Globals.ThisAddIn.Application.ActiveDocument.InlineShapes(i).ScaleWidth()
            End If
        Next i

However, this can only retrieve the Size (Height, Weight). How to get the Location (Left, Top) and page number of the image?

Upvotes: 1

Views: 1155

Answers (2)

Cindy Meister
Cindy Meister

Reputation: 25663

You can also get the top and left position of the image (although it might not do you any good, depending on why you want it). The get_Information method also has wdHorizontalPositionRelativeToPage and wdVerticalPositionRelativeToPage

Upvotes: 1

veevan
veevan

Reputation: 204

By its very nature, an InlineShape doesn't have a positionable top and left. It's, well, inline, which means it exists in the text layer of the document and the location floats depending on the text and/or other content before it. If the item is on page 2, and you insert 25 lines of text or another picture before InlineShape(i), said shape will float down to page 3 (or 4 or whatever.)

The height and width ARE accessible, simply by using .Height and .Width. ScaleHeight and ScaleWidth are properties that reflect the size of the object in the document relative to the original size of the object. You probably want to store the height and width as strings, though, since the property returns a single (numeric) value. For height and width:

    Dim i As Integer
    Dim shp as InlineShape
    For i = 1 To Globals.ThisAddIn.Application.ActiveDocument.InlineShapes.Count
        shp = Globals.ThisAddIn.Application.ActiveDocument.InlineShapes(i)
        If shp.Type = Word.WdInlineShapeType.wdInlineShapePicture Then
            strHeight = shp.Height.ToString()
            strWidth = shp.Width.ToString()
        End If
    Next i

To get the page number, you have to reference the range of the InlineShape.

    shp.Range.get_Information(Word.WdInformation.wdActiveEndPageNumber)

Upvotes: 2

Related Questions