ᴘᴀɴᴀʏɪᴏᴛɪs
ᴘᴀɴᴀʏɪᴏᴛɪs

Reputation: 7529

Insert resize and relocate image using a microsoft word macro

I'm trying to write a very basic macro in VB for Microsoft Word, but I don't have the required knowledge.

I simply need to do two things:

  1. Insert a picture from file
  2. Relocate it to the top right corner and resize it

I can do the first task via the record new macro feature but I am unable to select move a picture while in recording mode so I need some VB code for this.

I already have this, so how do I move/resize the image?

    Selection.InlineShapes.AddPicture FileName:= _
    "C:\Users\***\Pictures\**.jpg" _
    , LinkToFile:=False, SaveWithDocument:=True

Upvotes: 4

Views: 56499

Answers (1)

Denys Wessels
Denys Wessels

Reputation: 17049

The AddPicture function has a number of parameters which include width and height which you can use to resize the image to the desired size.

Please see an example below:

Sub InsertImage()

    Dim imagePath As String
    imagePath = "C:\\picture.jpg"

    ActiveDocument.Shapes.AddPicture FileName:=imagePath, _
    LinkToFile:=False, _
    SaveWithDocument:=True, _
    Left:=-5, _
    Top:=5, _
    Anchor:=Selection.Range, _
    Width:=20, _
    Height:=20

End Sub

Additionally, have a look at this msdn article for an explanation of the AddPicture() function as well as the list of available parameters which you can pass to it.

Upvotes: 8

Related Questions