简体   繁体   中英

Align (distribute) images horizontally in Word with VBA macro

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!

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM