InlineShape.Width在首次运行时无效

时间:2013-01-14 18:49:06

标签: image width word-vba

我正在尝试使用VBA调整Word文档中的图片大小。代码工作正常,但只是第二次运行。如果你打断它然后单步执行它也可以工作。有人可以评论我错过的东西吗?

Documents.Open FileName:=vDirectory & "\" & vFile

Dim objPic As InlineShape
For Each objPic In ActiveDocument.InlineShapes

If objPic.Width > CentimetersToPoints(16.51) Then
    With objPic
        .Width = CentimetersToPoints(16.51)
    End With
End If

Next objPic

1 个答案:

答案 0 :(得分:0)

我从访问中运行word宏时遇到了完全相同的问题。 (Word中也出现错误)

我终于改变了我的代码:

Dim objWord as object
Dim pic as InlineShape
Set objWord = CreateObject("Word.Application")    
With objWord.ActiveDocument
    For Each pic In .InlineShapes
        If pic.Width > 450 Then
            pic.Width = 450
        End If
    Next
End With

要:

Dim objWord as object
Dim pic as InlineShape
Set objWord = CreateObject("Word.Application")    
With objWord.ActiveDocument
    For Each pic In .InlineShapes
        If pic.Width > 450 Then
            pic.Width = 450
        End If
    Next
    For Each pic In .InlineShapes
        If pic.Width > 450 Then
            pic.Width = 450
        End If
    Next
End With

工作正常。看起来很蠢!