Forum Discussion

recumbent80's avatar
recumbent80
Copper Contributor
Sep 18, 2024

Format a Picture in MS Word

I am trying to write a simple macro to select an image, inserted it into Word, and then format it. I've cobbled together some code which sizes it properly but does not set the Wrap Text to "tight" correctly. Can anyone help me fix this issue?

 

Sub FormatImage()
' FormatImage 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 = msoFalse
.Height = InchesToPoints(1.25)
.Width = InchesToPoints(0.85)
End With

If Selection.ShapeRange.Count = 0 Then
If Selection.InlineShapes.Count = 1 Then
Selection.InlineShapes(1).ConvertToShape
Else
MsgBox "Select a picture first.", , "Oops!"
End If
End If

With Selection.ShapeRange(1)
With .WrapFormat
.Type = wdWrapTight
End With
End With

End Sub

No RepliesBe the first to reply