2016-09-27 104 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 

這應該是它!