开发者

Visual Basic Macro in Word to Resize/Center/Delete All Images

开发者 https://www.devze.com 2022-12-14 22:44 出处:网络
I found a VBA macro online that resizes all the images in a Word document: Sub ResizeAllImages() \'\'# make all images (both inline and floating)

I found a VBA macro online that resizes all the images in a Word document:

Sub ResizeAllImages()
''# make all images (both inline and floating)
''# 11 cm wide while preserving aspect ratio

Dim oShp As Shape
Dim oILShp As InlineShape

For Each oShp In ActiveDocument.Shapes
    With oShp
        .Height = AspectHt(.Width, .Height, _
        CentimetersToPoints(11))
        .Width = CentimetersToPoints(11)
    End With
Next

For Each oILShp In ActiveDocument.InlineShapes
    With oILShp
        .Height = AspectHt(.Width, .Height, _
        CentimetersToPoints(11))
        .Width = CentimetersToPoints(11)
    End With
Next
End Sub

I couldn't find the name of a method that I could use to center-align all images. Does anyone know what I need to add, and where I would have to 开发者_高级运维add it?

Lastly, I'd like to delete images that I find to be too small. How would I do... If width of shape is smaller than 5, and height of shape is smaller than 5, delete the shape.

For easier reading of large amounts of online text, I sometimes like to paste everything in word, and then rearrange it. I replace every period-whitespace, with a period-manual line, which gives me a new line for each sentence.. I read better when it's like that. Since I'm pasting everything, the graphics comes too, so I'd like to be able to control the size of all the images, and get rid of any unnecessary images.


I think you cannot center-align images. You can center-align paragraphs. Perhaps something like this will help you:

For Each oILShp In ActiveDocument.InlineShapes
    oILShp.Select
    Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Next

For deletion, just call Delete for each Shape object satisfying your conditions.


http://yuriy-okhmat.blogspot.co.uk/2011/07/how-to-resize-all-images-in-word.html

Has the full code including definition of AspectHt


Code to delete small pictures:

Sub DeleteSmallPictures()
Dim iShp As InlineShape

    For Each iShp In ActiveDocument.InlineShapes
        With iShp
            If .Width < CentimetersToPoints(5) Then
                iShp.Delete
            End If
        End With
    Next iShp
End Sub
0

精彩评论

暂无评论...
验证码 换一张
取 消