iRomul
iRomul

Reputation: 351

Add image to Word document at specified position

My task is place image from external app at specified position in document. For example - i have Rich Text content control, and my image must appear on this content control. This is example of code:

Dim cc As ContentControl
Set cc = ActiveDocument.ContentControls(1)
ActiveDocument.Shapes.AddPicture filename:=filename, LinkToFile:=False, SaveWithDocument:=True, Anchor:=cc.Range

Inserted image located at the same height, but vertical is located on the left. According to documentation this behavior is correct, but i need to set it right above my ContentControl object. Is it possible to do it? Maybe i can calculate length of paragraph in pixel and set Left parameter in Shapes.AddPicture method?

Upvotes: 1

Views: 4875

Answers (1)

Dirk Vollmar
Dirk Vollmar

Reputation: 176269

The AddPicture function returns a shape object which you can configure as desired:

Dim cc As ContentControl
Dim shp As shape
Set cc = ActiveDocument.ContentControls(1)

Set shp = ActiveDocument.Shapes.AddPicture(filename:=filename, LinkToFile:=False, SaveWithDocument:=True, Anchor:=cc.Range)
shp.Left = 20
shp.Top = -100

You can also set the RelativeHorizontalPosition and RelativeVerticalPosition properties of the shape object to specify how the left and top values are to be interpreted.

Upvotes: 3

Related Questions