2015-10-17 46 views
0

背景簡報VBA宏調整大小選擇爲最大的對象

我添加一個附加在做了以下事情中的所有對象: 對於選擇的所有PowerPoint對象(實施例4米的矩形),在添加 - 將調整所有對象的高度和寬度,以匹配選擇中最大對象的高度和寬度。

我試着寫一個VBA宏來複制這個插件,但什麼也沒有發生(適應於以下問題找到的代碼:Powerpoint VBA Macro to copy object's size and location and paste to another object):

Sub test() 
    Dim w As Double 
    Dim h As Double 
    Dim obj As Shape 

    w = 0 
    h = 0 

    For i = 1 To ActiveWindow.Selection.ShapeRange.Count 
     Set obj = ActiveWindow.Selection.ShapeRange(i) 
     If obj.Width > w Then 
      w = obj.Width 
     Else 
      obj.Width = w 
     End If 

     If obj.Height > h Then 
      h = obj.Height 
     Else 
      obj.Height = h 
     End If 
    Next 
End Sub 

問題

任何想法上如何使這段代碼有效?

回答

0

一些調查研究後,這裏是工作的代碼(不知道這是一個非常高效的,因爲我是新來的VBA):

Sub resizeAll() 
    Dim w As Double 
    Dim h As Double 
    Dim obj As Shape 

    w = 0 
    h = 0 

    ' Loop through all objects selected to assign the biggest width and height to w and h 
    For i = 1 To ActiveWindow.Selection.ShapeRange.Count 
     Set obj = ActiveWindow.Selection.ShapeRange(i) 
     If obj.Width > w Then 
      w = obj.Width 
     End If 

     If obj.Height > h Then 
      h = obj.Height 
     End If 
    Next 

    ' Loop through all objects selected to resize them if their height or width is smaller than h/w 
    For i = 1 To ActiveWindow.Selection.ShapeRange.Count 
     Set obj = ActiveWindow.Selection.ShapeRange(i) 
     If obj.Width < w Then 
      obj.Width = w 
     End If 

     If obj.Height < h Then 
      obj.Height = h 
     End If 
    Next 
End Sub 
+0

有可能,你可以適用於一些小的調整在理論上*使這更有效率。實際上,儘管如此,你不太可能看到差異。 –

+0

感謝您的反饋! (並且我使用你的加載項解釋將這個宏添加到我的ppt中)。 – remif