2016-09-27 131 views
0

这是我遇到的问题。我需要在单个Word文档中旋转Word.Shapes,但我的脚本只会旋转第一个,而我无法弄清楚原因。VBA - 旋转Word.Shapes在Word文档中

这里的字文档如何来是(打开一个PDF每页都会有一个形状):

Set wrdDoc = wrdAppMain.Documents.Open(FileName:=sToSaveAs, Visible:=False) 

这里是如何循环设计:

For Each wrdShape In wrdDoc.Shapes 

    If CheckFormat(wrdShape) = False Then 
     FitToPage = False 
     GoTo ExitScript 
    End If 

Next wrdShape 

而现在这部分代理:

Private Function CheckFormat(oShapeToCheck As Word.Shape) As Boolean 

    On Error GoTo Failed 

    Dim siAspectRatio As Single 
    Dim iRotation As Integer 

    '---- Seitenverhältnis und Rotation berechnen ---- 
    If oShapeToCheck.Height > 0 And oShapeToCheck.Width > 0 Then 
     siAspectRatio = oShapeToCheck.Height/oShapeToCheck.Width 
     iRotation = oShapeToCheck.Rotation 
    Else 
     ErrorCode = " (PDF)" 
     GoTo Failed 
    End If 

    '---- Kontrolle ob Bild im Querformat vorliegt ---- 
    If siAspectRatio < 1 Then 

    '---- Kontrolle ob rotiert oder natives Querformat ---- 
    Select Case iRotation 
     Case 0 
      oShapeToCheck.IncrementRotation 90 
     Case 180 
      oShapeToCheck.IncrementRotation 270 
     Case 90 
      oShapeToCheck.IncrementRotation 0 
     Case 270 
      oShapeToCheck.IncrementRotation 180 
    End Select 

所以,这里是问题所在。尽管我第一个符合标准的Word.Shape将被旋转,但其他任何人都不会。此外,如果我将Word文档的可见性设置为TRUE,则在脚本执行旋转之前通过全屏调整Word文档,每次都会旋转任何Word.Shape。

我试着搞乱了。激活之类的,但似乎没有任何工作。希望你能帮助我!

谢谢!

Markus

+1

你能展示剩下的代码吗?要为多个对象运行此操作,您必须在循环中执行此操作,但我们无法看到您是如何设置它的,因此缺少重要的信息。 – Dave

+0

嗨,戴夫!信息已更新。对不起,以前没有! –

回答

0

所以我找到了一种方法来完成这项工作。我不是逐个旋转每个Word.Shape,而是通过它们的索引(或其中的复数形式)将它们全部收集到一个ShapeRange中,并将它们一次全部旋转。

Select Case iRotation 
     Case 0 
      If bIsDimensioned = False Then 
       ReDim Preserve RotationArray(0 To 0) As Variant 
       RotationArray(0) = iShapeIndex 
       bIsDimensioned = True 
      Else 
       ReDim Preserve RotationArray(0 To UBound(RotationArray) + 1) As Variant 
       RotationArray(UBound(RotationArray)) = iShapeIndex 
      End If 
End Select 

而且ShapeRange后是完全填充:

If bIsDimensioned = True Then 
    Set RotationShapeRange = wrdDoc.Shapes.Range(RotationArray) 
    RotationShapeRange.IncrementRotation 90 
    RotationShapeRange.WrapFormat.Type = wdWrapTight 
    RotationShapeRange.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage 
    RotationShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionPage 
    RotationShapeRange.Left = wdShapeCenter 
    RotationShapeRange.Top = wdShapeCenter 
End If 

这应该是它!