\n
I tried using code for Horizontal alignment from this link: https://www.excelcampus.com/vba/align-space-distribute-shapes/
\nBut I got the following result:\n\nMargins are weird and shapes are aligned infinitely instead of going into the next row...
My Code:
\n Dim lCnt As Long\n Dim dTop As Double\n Dim dLeft As Double\n Dim dWidth As Double\n Const dSPACE As Double = 8 'Set space between shapes in points\n \n lCnt = 1\n \n Dim image As Shape\n\nIf ActiveDocument.Shapes.Count > 0 Then\n For Each image In ActiveDocument.Shapes\n With image\n .WrapFormat.Type = wdWrapSquare\n .LockAspectRatio = msoTrue\n .Height = InchesToPoints(3)\n \n If lCnt > 1 Then\n .Top = dTop\n .Left = dLeft + dWidth + dSPACE\n End If\n dTop = .Top\n dLeft = .Left\n dWidth = .Width\n End With\n lCnt = lCnt + 1\n Next\n End If\nEnd Sub\n
\nThanks in advance!
\n","author":{"@type":"Person","name":"Vasilije Bursac"},"upvoteCount":0,"answerCount":2,"acceptedAnswer":{"@type":"Answer","text":"Inserting your images into a table with fixed cell dimensions won't achieve what you say you want, since the images clearly don't have the same aspect ratio. What you need to do is to convert them to inlineshapes so that Word can handle the line wrapping. For example:
\nSub Demo()\nApplication.ScreenUpdating = False\nDim iShp As InlineShape\nWith ActiveDocument\n Do While .Shapes.Count > 0\n .Shapes(1).ConvertToInlineShape\n Loop\n For Each iShp In .InlineShapes\n With iShp\n .LockAspectRatio = True\n .Height = InchesToPoints(3)\n If .Range.Characters.Last.Next <> " " Then .Range.InsertAfter " "\n End With\n Next\nEnd With\nApplication.ScreenUpdating = True\nEnd Sub\n
\nYou can adjust the vertical spacing between the images by changing the paragraph line spacing. Note too, that the horizontal alignment can be played around with by switching between left, centered and justified paragraph formats.
\n","author":{"@type":"Person","name":"macropod"},"upvoteCount":1}}}Reputation: 305
This is my first time writing macro in VBA. My goal is to write a VBA macro that will automatically align (distribute) all images in a Word document horizontally (next to each other) with a small margin on each side of every image. If there is not enough space to fit another image, I need it to go to the next row(just below previous images) and continue with the horizontal alignment of images.
I have searched a lot on the internet, but I haven't found a way to achieve this...
NOTE: My macro already contains code for making all images have the same height(while keeping the same aspect ratio), so I think dimensions shouldn't be a problem...
Here is a small example of what I want to achieve:
I tried using code for Horizontal alignment from this link: https://www.excelcampus.com/vba/align-space-distribute-shapes/
But I got the following result:
Margins are weird and shapes are aligned infinitely instead of going into the next row...
My Code:
Dim lCnt As Long
Dim dTop As Double
Dim dLeft As Double
Dim dWidth As Double
Const dSPACE As Double = 8 'Set space between shapes in points
lCnt = 1
Dim image As Shape
If ActiveDocument.Shapes.Count > 0 Then
For Each image In ActiveDocument.Shapes
With image
.WrapFormat.Type = wdWrapSquare
.LockAspectRatio = msoTrue
.Height = InchesToPoints(3)
If lCnt > 1 Then
.Top = dTop
.Left = dLeft + dWidth + dSPACE
End If
dTop = .Top
dLeft = .Left
dWidth = .Width
End With
lCnt = lCnt + 1
Next
End If
End Sub
Thanks in advance!
Upvotes: 0
Views: 1465
Reputation: 13515
Inserting your images into a table with fixed cell dimensions won't achieve what you say you want, since the images clearly don't have the same aspect ratio. What you need to do is to convert them to inlineshapes so that Word can handle the line wrapping. For example:
Sub Demo()
Application.ScreenUpdating = False
Dim iShp As InlineShape
With ActiveDocument
Do While .Shapes.Count > 0
.Shapes(1).ConvertToInlineShape
Loop
For Each iShp In .InlineShapes
With iShp
.LockAspectRatio = True
.Height = InchesToPoints(3)
If .Range.Characters.Last.Next <> " " Then .Range.InsertAfter " "
End With
Next
End With
Application.ScreenUpdating = True
End Sub
You can adjust the vertical spacing between the images by changing the paragraph line spacing. Note too, that the horizontal alignment can be played around with by switching between left, centered and justified paragraph formats.
Upvotes: 1
Reputation: 1713
Since you are new to VBA I wanted to share a bit of code if you were to pursue a Table approach. The code below creates a single-row table that is fixed in width and will not expand width-wise unless you alter the individual cells. For demo purposes only, I insert the same picture into each cell to demonstrate that the image resizes automatically based on cell width.
Sub TableOfPictures()
Dim doc As Word.Document, rng As Word.Range
Dim Tbl As Word.Table, C As Long
Set doc = ActiveDocument
Set rng = Selection.Range
Set Tbl = rng.Tables.Add(rng, 1, 2, Word.WdDefaultTableBehavior.wdWord8TableBehavior)
Tbl.rows(1).Cells(1).Width = InchesToPoints(2)
Tbl.rows(1).Cells(2).Width = InchesToPoints(4.5)
For C = 1 To 2
Tbl.rows(1).Cells(C).Range.InlineShapes.AddPicture ("Y:\Pictures\Mk45 Gun Proj_Blast.jpg")
Next
End Sub
Upvotes: 1