📄 Word 圖片管理全攻略:一鍵選取、刪除與批次匯出教學
當 Word 文件中有大量圖片時,逐一刪除或整理會浪費許多時間。 本文將介紹三種不同層次的方法,讓你能輕鬆選取、刪除或匯出所有圖片,並進一步實現自動化管理。
1️⃣ 手動方式:選取與刪除圖片
- 方法一:點選任一圖片後,按 Ctrl + A 可同時選取所有內容(含圖片與文字)。
- 方法二:使用「尋找與取代」功能:
- 按 Ctrl + H 開啟「尋找與取代」。
- 點選「更多」 → 「特殊格式」 → 「圖形」。
- 在「尋找內容」中出現
^g,即可逐一尋找圖片所在。
這些方式適合少量圖片的情境,但若需批次操作,建議使用 VBA 巨集。
2️⃣ VBA 巨集方式:自動選取與刪除所有圖片
以下巨集可自動選取文件中所有圖片(含嵌入與浮動),也能批次刪除。
Sub SelectAllPictures()
Dim shp As Shape
Dim inShp As InlineShape
Selection.HomeKey wdStory
' 選取所有嵌入圖片
For Each inShp In ActiveDocument.InlineShapes
inShp.Select
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Next inShp
' 選取所有浮動圖片
For Each shp In ActiveDocument.Shapes
shp.Select Replace:=False
Next shp
MsgBox "✅ 所有圖片已被選取完成。"
End Sub
🗑️ 批次刪除所有圖片
Sub DeleteAllPictures()
Dim shp As Shape
Dim inShp As InlineShape
For Each inShp In ActiveDocument.InlineShapes
inShp.Delete
Next inShp
For Each shp In ActiveDocument.Shapes
shp.Delete
Next shp
MsgBox "🗑️ 所有圖片已刪除完成!"
End Sub
📁 匯出所有圖片
Sub ExportAllImages()
Dim shp As InlineShape, i As Long
Dim path As String
path = Environ("USERPROFILE") & "\Desktop\WordImages\"
On Error Resume Next: MkDir path: On Error GoTo 0
For Each shp In ActiveDocument.InlineShapes
shp.Select
Selection.CopyAsPicture
Selection.Paste
Selection.InlineShapes(1).Select
Selection.ShapeRange.SaveAsPicture path & "Image_" & i & ".png"
Selection.Delete
i = i + 1
Next shp
MsgBox "📦 所有圖片已匯出至:" & path
End Sub
💡 執行方式:
- 按 Alt + F11 開啟 VBA 編輯器。
- 選擇「插入 → 模組」,貼上上述程式碼。
- 回到 Word,按 Alt + F8 執行巨集。
3️⃣ 進階整合應用
- 可在匯出後,使用 PowerShell 或 Python 自動壓縮圖片。
- 壓縮後再利用
Selection.InlineShapes.AddPicture重新插入圖片。
Selection.InlineShapes.AddPicture _
FileName:="C:\Images\img1.png", _
LinkToFile:=False, SaveWithDocument:=True
若搭配壓縮工具(如 TinyPNG API 或 Python Pillow),可將大型報告檔案縮小至原本的 30%。
🧭 行動清單
✅ 開啟 Word 文件並按 Alt + F11 ✅ 貼上「SelectAllPictures」巨集 ✅ 執行並確認所有圖片被選取 ✅ 使用「DeleteAllPictures」清除圖片 ✅ 若需備份,執行「ExportAllImages」匯出
📘 結語
透過 VBA 巨集即可在幾秒內完成 Word 文件的圖片選取、刪除與匯出,大幅減少人工操作時間。 若搭配自動壓縮與再插入流程,更能形成完整的文件優化機制。
🔗 延伸閱讀
— WWFandy・Office 自動化筆記
沒有留言: