Word VBA - format a picture

Joined
Nov 27, 2023
Messages
3
Reaction score
0
Hi,

Apologies if this is a basic question but I'm having difficult getting some VBA code to work ... I'm somewhat familiar with Excel VBA but not Word VBA, hence my lack of understanding of the required syntax. (I'm using Word 365 btw)

Basically, I have a document with a number of images that are too small on the page and I would like to select the image and run a macro to resize to 19cm width (retaining the aspect ratio) and set it as 'middle center with square text wrapping' ['position' tab of 'picture format' icon, center selection].

I have been getting near it (including unsuccessfully asking chat GPT!) but I cant seem to get it to work ... I'm regularly hanging up Word completely.

One of the Codings suggested was:

Sub ResizeAndCenterSelectedObject()
With Selection.ShapeRange
.LockAspectRatio = msoTrue
.Height = 200
.Width = 200
.Align msoAlignCenters, True
.Align msoAlignMiddles, True
End With
End Sub

I kind of got to the same area myself but this code just crashes Word (no idea why). Once I get a bit more time I will take a deep dive into Word VBA but for now, does anyone have the code to do this ... I feel it w=should be pretty short but I just don't have enough knowledge of the syntax at the moment to pull it off.

Any help appreciated
 
Joined
Nov 27, 2023
Messages
3
Reaction score
0
I found some other code which seems to get the size correct (and retains aspect ratio) but the centering doesn't seem to work (although I can manually center it with 'middle center with square text wrapping'):

Sub ResizeCenterAndWrapTextSquare()
Dim pic As InlineShape
Dim targetSize As Single
Dim pageWidth As Single
Dim pageHeight As Single
Dim leftPosition As Single
Dim topPosition As Single

' Set the target size for the picture (square)
targetSize = 535 ' Set your desired size

' Set the reference to the picture (change the index accordingly)
Set pic = ActiveDocument.InlineShapes(1) ' Change the index as needed

' Get the page width and height
pageWidth = ActiveDocument.PageSetup.pageWidth
pageHeight = ActiveDocument.PageSetup.pageHeight

' Calculate the left position to center the picture horizontally
leftPosition = -100

' Calculate the top position to center the picture vertically
topPosition = (pageHeight - targetSize) / 2

' Resize and position the picture
With pic
.LockAspectRatio = msoTrue
.Width = targetSize


End With
End Sub
 
Joined
Nov 27, 2023
Messages
3
Reaction score
0
Ok, this works - posted here in case any other poor soul has the same question :eek:... unfortunately I had so many tabs open and was cutting and pasting so many bits of code together I lost what the original source was for this:

Sub AlignToCentre()
'
' AlignToCentre
Dim shp As Word.shape
If Selection.InlineShapes.Count = 1 Then
Set shp = Selection.InlineShapes(1).ConvertToShape
shp.WrapFormat.Type = wdWrapSquare
shp.Select
With Selection.ShapeRange
.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
.Left = wdShapeCenter
.RelativeVerticalPosition = wdRelativeVerticalPositionPage
.Top = InchesToPoints(1)
End With
ElseIf Selection.ShapeRange.Count = 1 Then
With Selection.ShapeRange
.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
.Left = wdShapeCenter
.RelativeVerticalPosition = wdRelativeVerticalPositionPage
.Top = InchesToPoints(1)
End With
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top