Macro to resize and center align pictures, charts, canvas in MS Worddocument

U

ucanalways

Hey people,

I am novice to word VBA programming.

I have a word document and it contains, pictures, charts and canvas
(powerpoint slides).
Currently, I use the following code to center align the pictures and
charts.

Sub test()
Dim dc As Document
Dim ilShp As InlineShape

Set dc = ActiveDocument

For Each ilShp In dc.InlineShapes
If Not ilShp.Type = wdInlineShapeOLEControlObject Then
ilShp.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
End If
Next

End Sub

I trying to write macros to:
1. Center align a canvas
2. Strecth/resize a picture to fit the width of the page while
mainitaing the aspect ratio.

Please let me know the macro for those.

I would really appreciate any help. Thank You.
 
U

ucanalways

Hey people,

I am novice to word VBA programming.

I have a word document and it contains, pictures, charts and canvas
(powerpoint slides).
Currently, I use the following code to center align the pictures and
charts.

Sub test()
Dim dc As Document
Dim ilShp As InlineShape

Set dc = ActiveDocument

For Each ilShp In dc.InlineShapes
If Not ilShp.Type = wdInlineShapeOLEControlObject Then
ilShp.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
End If
Next

End Sub

I trying to write macros to:
1. Center align a canvas
2. Strecth/resize a picture to fit the width of the page while
mainitaing the aspect ratio.

Please let me know the macro for those.

I would really appreciate any help. Thank You.

Guys, can anyone please help me to solve my macro problem? Thanks
 
F

fumei via OfficeKB.com

Regarding the centering, the best way would be to make a style for your
images. This is a good idea generally. All my images use an image paragraph
style.

So say you have a style - MyImageCenter - and it is centered, or whatever.

Sub test()
Dim dc As Document
Dim ilShp As InlineShape

Set dc = ActiveDocument

For Each ilShp In dc.InlineShapes
If Not ilShp.Type = wdInlineShapeOLEControlObject Then
ilShp.Range.Style = "MyImageCenter"
End If
Next
End Sub

This applies the cenetring style. Done.

As for the resizing...this can get tricky, but can be done.

The EASIEST way, and if the images are all the same size to start with, is to
manually do the resizing and note the Scale percentage change. So say you
resized it, and the Scale change is 150, that it, it is 150% bigger than the
original. Now you could use:

Sub test()
Dim dc As Document
Dim ilShp As InlineShape

Set dc = ActiveDocument

For Each ilShp In dc.InlineShapes
If Not ilShp.Type = wdInlineShapeOLEControlObject Then
With ilShp
.LockAspectRatio = msoTrue
.ScaleWidth = 150
End With
ilShp.Range.Style = "MyImageCenter"
End If
Next
End Sub

Still using the style to do the centering, but scaling each up 150%.
 

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