Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert resize and relocate image using a microsoft word macro

Tags:

ms-word

vba

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
like image 994
ᴘᴀɴᴀʏɪᴏᴛɪs Avatar asked Feb 21 '23 01:02

ᴘᴀɴᴀʏɪᴏᴛɪs


1 Answers

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.

like image 187
Denys Wessels Avatar answered Mar 06 '23 15:03

Denys Wessels