跳到主要內容

效率提升術:使用VBA自動調整 PowerPoint 圖片和均分對齊

效率提升術:使用VBA自動調整 PowerPoint 圖片和均分對齊


使用 VBA 自動調整 PowerPoint 中圖片的對齊和分配

如果你經常使用 PowerPoint 製作報告,你應該知道在投影片上對齊和平均分配圖片是多麼耗時。手動調整多個圖片的大小和位置可能非常繁瑣,點選圖片,調整高度,調整位置,平均分配,如果發現大小調整不好,圖片還會重疊...。小編因為這個寫個一下 VBA,你可以使用 Visual Basic for Applications(VBA)自動執行這個煩人的工作任務。


在這篇文章中,我們將探討一個 VBA code,它可以自動調整 PowerPoint 中所選圖片的大小、位置和對齊。

Code 如下:

'水平調整, 將選取的圖片自動調整大小, 對齊, 均分'
Sub HAdjustImage()

Dim objShape As Shape
Dim lLeft As Single
Dim lRight As Single
Dim lWidth As Single
Dim lSpacing As Single
Dim lCount As Long
Dim lTotalWidth As Single
Dim lMinLeft As Single
Dim lMinLeftShape As Shape ' Leftmost picture
Dim lMaxRightShape As Shape ' Rightmost picture

'Find the left and right positions of the selected shapes
For Each objShape In ActiveWindow.Selection.ShapeRange
    If objShape.Type = msoPicture Then
        If objShape.Left < lMinLeft Or lMinLeft = 0 Then lMinLeft = objShape.Left
        If objShape.Left < lLeft Or lLeft = 0 Then
            lLeft = objShape.Left
            Set lMinLeftShape = objShape
        End If
        If objShape.Left + objShape.Width > lRight Then
            lRight = objShape.Left + objShape.Width
            Set lMaxRightShape = objShape
        End If
    End If
Next objShape

'Calculate the total width of the selected shapes and the spacing between them
lWidth = lRight - lLeft
lCount = ActiveWindow.Selection.ShapeRange.Count
lSpacing = 0.3 / 2.54 * 72 * (lCount - 1)

'Adjust the width of the selected shapes to avoid overlapping
For Each objShape In ActiveWindow.Selection.ShapeRange
    If objShape.Type = msoPicture Then
            objShape.Width = (lWidth - lSpacing) / lCount
    End If
Next objShape

' Adjust the left edge of the leftmost shape
lMinLeftShape.Left = lLeft
lMaxRightShape.Left = lRight - lMaxRightShape.Width


Dim selShapes As ShapeRange
Set selShapes = ActiveWindow.Selection.ShapeRange
    
'Align the shapes horizontally relative to the slide
selShapes.Align msoAlignTops, msoTrue

'Distribute the shapes horizontally
If lCount > 2 Then
    selShapes.Distribute msoDistributeHorizontally, msoDistributeByCenter
End If

selShapes.Group

End Sub

以下是 code 的功能:

找到選擇中最左和最右的圖片,計算選定圖片的總寬度和它們之間的間距。
調整圖片寬度以避免重疊。
將最左邊和最右邊的圖片的距離固定,你可以在一開始就用最左跟最右的圖片決定你放圖的寬。
將圖片水平均分並將它們群組在一起,最後只要拖曳到你要的位置即可。


code如何運作

讓我們細看一下 HAdjustImage 的幾個步驟。


找到最左和最右的圖片

第一步是找到選擇中最左和最右的圖片。code 確認每個選定的形狀,並檢查它是否為圖片。如果是,則將其左位置與當前的最小和最大左位置進行比較,以找到最左和最右的圖片。


計算總寬度和間距

找到最左和最右的圖片後,code 通過從最右圖片的右位置減去最左圖片的左位置來計算所選圖片的總寬度。code 設定圖片間距為 0.3cm,計算出每張圖片的寬度要多少才不會重疊。


調整圖片大小

接下來,code 調整每個圖片的寬度。


對齊圖片

因為圖片大小調整後,左右總寬度會超出或小於我們原本的設計。
我們需要重新將左右張圖片位置移動到原始位置。


均分圖片

最後,Code 將使用 msoDistributeHorizontally 選項將圖片水平均分。這確保了圖片間距均勻。然後,使用 Group 方法將圖片群組在一起,以便更容易地將它們移動或編輯。


結論

自動調整圖片對齊和均分可以在使用 PowerPoint 報告時為你節省大量時間。使用 VBA,你還可以根據自己的需求調整 code,例如調整垂直對齊或更改圖片之間的間距。


你可能會想知道如果想要調整圖片間的間距,該如何修改 HAdjustImage 巨集?

如果你想要調整圖片之間的間距,可以修改 lSpacing = 0.3 / 2.54 * 72 * (lCount - 1) 這行。0.3 cm 就是圖片間的間距。例如,假設你希望將圖片之間的間距設置為 0.5 cm,則可以將固定值更改為 0.5。


留言

這個網誌中的熱門文章

Reddit 超強文章:使用 ChatGPT 的 Custom Instruction 提升回答品質

Forefront Chat:自由切換 GPT-3.5 和 GPT-4 聊天,現在免費!

提高投資決策效率:利用 ChatGPT 分析資產負債表

ChatGPT 可以顯示美觀數學公式:Tex All the Tings Chrome Extension

Glarity Chrome 擴充套件:使用 ChatGPT 生成 Youtube/bilibili, Google/Bing, Page Summary, Comment Summary 摘要

ChatGPT 的 GPTs 筆記 (4):GPTs Konwledge 知識庫的限制與風險

當未來和過去交錯:AI 翻譯古老楔形文字 Cuneiform

TRIZGPT:解決問題的小顧問

ChatGPT Sidebar: 全方位 AI 助手 (Chrome Extension)

Grammarly 推出 GrammarlyGo:創新的生成式 AI 寫作助手