需將 100 張照片分別插入 100 張投影片?手動操作需數小時。利用 PowerPoint VBA 巨集,幾行代碼自動化批次插入,從準備到完成僅需幾分鐘。
1.jpg, 2.jpg, ..., 100.jpg 置於已知資料夾(如 C:\Pictures\)。Sub BatchInsertPictures()
Dim i As Long
Dim picPath As String
Dim slideCount As Long
' 設定圖片資料夾路徑 (請修改為實際路徑,結尾加 \)
Const PIC_FOLDER As String = "C:\Pictures\"
' 確保有足夠投影片 (若不足自動新增)
slideCount = ActivePresentation.Slides.Count
If slideCount < 100 Then
Dim j As Long
For j = slideCount + 1 To 100
ActivePresentation.Slides.Add j, ppLayoutBlank
Next j
End If
' 逐頁插入圖片做為背景填滿
For i = 1 To 100
On Error Resume Next ' 忽略找不到檔案錯誤
picPath = PIC_FOLDER & i & ".jpg"
If Dir(picPath) <> "" Then
With ActivePresentation.Slides(i)
.FollowMasterBackground = msoFalse
.Background.Fill.UserPicture picPath
End With
End If
On Error GoTo 0
Next i
MsgBox "批次插入完成!共處理 " & i - 1 & " 張投影片。"
End Sub

巨集執行對話框
.FollowMasterBackground = msoFalse:取消繼承母版背景,允許單獨設定。.Background.Fill.UserPicture:將圖片設為投影片背景填滿(自動置中縮放裁切)。ActivePresentation.Slides(i).Shapes.AddPicture _
FileName:=picPath, _
LinkToFile:=msoFalse, _
SaveWithDocument:=msoTrue, _
Left:=0, Top:=0, _
Width:=ActivePresentation.PageSetup.SlideWidth, _
Height:=ActivePresentation.PageSetup.SlideHeight
Dir(PIC_FOLDER & i & ".*") 取得實際檔名。VBA 巨集是 PPT 自動化的核心武器,掌握「批次插圖」範本,未來任何大量重複操作(插表、插圖、改版、匯出)皆可舉一反三,效率提升百倍!