调整图片大小

时间:2013-02-14 11:40:02

标签: image word-vba

我正在使用VBA代码来减少文档中的图像以适合可打印的页面宽度:

Dim myPageWidthWithoutMargins As Single
myPageWidthWithoutMargins = ActiveDocument.PageSetup.PageWidth - ActiveDocument.PageSetup.LeftMargin - ActiveDocument.PageSetup.RightMargin

Dim myShape As InlineShape
For Each myShape In ActiveDocument.InlineShapes
   myShape.LockAspectRatio = msoTrue
   If myShape.Width > myPageWidthWithoutMargins Then
      myShape.Width = myPageWidthWithoutMargins
   End If
Next

它不能作为另一个子的一部分工作,但它确实作为一个单独的宏工作,我在完成所有其他子操作后手动启动。为什么以及如何解决这个问题?

===============

它是大型子程序的一部分,可将HTML文件转换为相应的Word文档。以下是处理图像之前使用的代码:

Dim myImgStyle As Word.Style
Set myImgStyle = ActiveDocument.Styles.Add("CenteredImageStyle", wdStyleTypeParagraph)
With myImgStyle
    .BaseStyle = ActiveDocument.Styles(cNormalStyleName)
    .ParagraphFormat.Alignment = wdAlignParagraphCenter
End With

Dim myShape As InlineShape
For Each myShape In ActiveDocument.InlineShapes
    If Not myShape.LinkFormat Is Nothing Then
       myShape.LinkFormat.SavePictureWithDocument = True
       myShape.LinkFormat.BreakLink
       myShape.Range.Select
       Dim myParaRange As Range
       Set myParaRange = Selection.Paragraphs.First.Range
       If Selection.Start = myParaRange.Start And Selection.End = myParaRange.End - 1 Then
           Selection.Style = myImgStyle
       End If
    End If
Next

0 个答案:

没有答案