Macro in Word to insert and format a picture?

caddcop

New member
Local time
Today, 15:42
Joined
Oct 25, 2011
Messages
3
We have a large number of pictures to insert into word docs and we also need to change their format once inserted. This is a frequent task for a particular client, so this will become a real timesaver.
I tried to record the process, but after the image is inserted, it is not selected. To get it to work, I had to break it into two macros and you have to select the image in between running the two macros.
Does anyone know how I can tell word to select the just inserted image?
The image is inserted from a file as though you selected the Insert Picture From File menu item.
Thanks.
 
Here is what I ended up with. The bulk of the code was from recording the macro as I stepped through the process. The key to linking the newly inserted picture was the InLineShape object which I used newPicture as the name of my InLineShape.
The macro starts by attempting to delete any selected object - in our case, that was usually a picture used on the last project as the process was started by making a new file from a copy of the last project's file. Then, it offers the Browse to file dialog box to select a picture and after it inserts the picture, it formats the newly inserted picture, adjusting its size and the borders.

Sub InsertFormatedPicture()
'
' InsertPicture Macro
'
Dim fileSelected As Variant
Dim fileOpenDialog As FileDialog
Dim newPicture As InlineShape

Set fileOpenDialog = Application.FileDialog(msoFileDialogOpen)

Selection.Delete
'Unit:=wdCharacter, Count:=1
With fileOpenDialog
.AllowMultiSelect = False
.Filters.Add "Images", "*.gif; *.jpg; *.jpeg", 1
If .Show = -1 Then
fileSelected = .SelectedItems(1)
Else
Exit Sub
End If
End With
Set newPicture = Selection.InlineShapes.AddPicture(FileName:=fileSelected, LinkToFile:=False, SaveWithDocument:=True)
With newPicture
.LockAspectRatio = msoTrue
.Height = 272.9
.Width = 363.6
With .Borders(wdBorderLeft)
.LineStyle = wdLineStyleThinThickSmallGap
.LineWidth = wdLineWidth150pt
.Color = wdColorRed
End With
With .Borders(wdBorderRight)
.LineStyle = wdLineStyleThickThinSmallGap
.LineWidth = wdLineWidth150pt
.Color = wdColorRed
End With
With .Borders(wdBorderTop)
.LineStyle = wdLineStyleThinThickSmallGap
.LineWidth = wdLineWidth150pt
.Color = wdColorRed
End With
With .Borders(wdBorderBottom)
.LineStyle = wdLineStyleThickThinSmallGap
.LineWidth = wdLineWidth150pt
.Color = wdColorRed
End With
.Borders.Shadow = False
End With
End Sub

I hope this proves useful.
 

Users who are viewing this thread

Back
Top Bottom