VBA code to centre pictures in WORD.

S.H.A.D.O.

Well-known Member
Joined
Sep 6, 2005
Messages
1,915
Good afternoon,

I have the following code which works well at resizing all the pictures to a set size.
I can't however work out how to centre all the pictures at the same time though!

Code:
Sub Resize_Images1()
Dim i As Long
    With Application
        .ScreenUpdating = False: .DisplayAlerts = False
    End With
    With ActiveDocument
        For i = 1 To .InlineShapes.Count
            With .InlineShapes(i)
                .ScaleHeight = 40
                .ScaleWidth = 40
            End With
        Next i
    End With
    With Application
        .DisplayAlerts = True:  .ScreenUpdating = True
    End With
End Sub

Any help will be greatly appreciated.

Thanks in advance.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Not to worry, I have managed to create a workaround.
It appears that you can't centre a picture using VBA so you have to centre the paragraph (picture).
What I did was to create a further Sub and then called that from the Sub above.
 
Upvote 0
Brilliant, thanks Macropod, it is very much apprciated.

Adding that single line to my existing code is far better than the solution I came up with which is this:

Code:
Sub Resize_Images()
Dim i As Long
    With Application
        .ScreenUpdating = False: .DisplayAlerts = False
    End With
    With ActiveDocument
        For i = 1 To .InlineShapes.Count
            With .InlineShapes(i)
                .ScaleHeight = 35
                .ScaleWidth = 35
            End With
        Next i
    End With
    Call Centre_Images
    With Application
        .DisplayAlerts = True: .ScreenUpdating = True
    End With
End Sub

Sub Centre_Images()
Dim j As InlineShape
    With Application
        .ScreenUpdating = False: .DisplayAlerts = False
    End With
    For Each j In ActiveDocument.InlineShapes
        If j.Type = wdInlineShapePicture Then
            j.Select
            Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
        End If
    Next j
    With Application
        .DisplayAlerts = True: .ScreenUpdating = True
    End With
End Sub

My code now is obviously this (I was almost there!):

Code:
Sub Resize_Images()
Dim i As Long
    With Application
        .ScreenUpdating = False: .DisplayAlerts = False
    End With
    With ActiveDocument
        For i = 1 To .InlineShapes.Count
            With .InlineShapes(i)
                .ScaleHeight = 35
                .ScaleWidth = 35
                .Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
            End With
        Next i
    End With
    With Application
        .DisplayAlerts = True: .ScreenUpdating = True
    End With
End Sub

Thanks again :beerchug: .
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,212
Members
452,618
Latest member
Tam84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top