Word VBA 提供豐富的 InlineShape(內嵌圖片)與 Shape(浮動圖片)物件模型,可程式化控制圖片旋轉、版式轉換、尺寸、像素資訊擷取,甚至跨應用程式(如 Excel→Word)貼上圖片。本文整理五大實用代碼模組。
Sub RotateSelectedPicture()
Dim isInline As Boolean
Dim angle As Variant
' 1. 若為內嵌圖片,先轉為浮動 Shape 才能旋轉
If Selection.Type = wdSelectionInlineShape Then
isInline = True
Selection.InlineShapes(1).ConvertToShape
End If
' 2. 輸入旋轉角度(正=順時針,負=逆時針)
angle = InputBox("請輸入旋轉角度 (度):" & vbCrLf & "正數=順時針,負數=逆時針", "圖片旋轉", 30)
If Not IsNumeric(angle) Then Exit Sub
' 3. 執行旋轉
On Error Resume Next
Selection.ShapeRange.IncrementRotation CDbl(angle)
On Error GoTo 0
' 4. 若原為內嵌,可選擇轉回(視需求)
' If isInline Then Selection.ShapeRange(1).ConvertToInlineShape
MsgBox "旋轉完成:" & angle & " 度", vbInformation
End Sub
Sub ConvertAllFloatingToInline()
Dim shp As Shape
Dim doc As Document
Set doc = Documents("MyDoc.doc") ' 或 ActiveDocument
Application.ScreenUpdating = False
For Each shp In doc.Shapes
If shp.Type = msoPicture Then
shp.ConvertToInlineShape
End If
Next shp
Application.ScreenUpdating = True
MsgBox "轉換完成", vbInformation
End Sub
差異補充:內嵌圖片 (InlineShape) 如字元般隨文字流動,無法自由定位;浮動圖片 (Shape) 可絕對/相對定位、文字環繞、旋轉、加特效。
Sub ResizeAllInlinePictures()
Dim iShp As InlineShape
Dim cmWidth As Double, cmHeight As Double
cmWidth = 10 ' 目標寬度 10 cm
cmHeight = 10 ' 目標高度 10 cm
Application.ScreenUpdating = False
For Each iShp In ActiveDocument.InlineShapes
' 1 cm = 28.3464567 pt (Word 點數單位)
iShp.Width = cmWidth * 28.3464567
iShp.Height = cmHeight * 28.3464567
' 若需保持比例,僅設定寬度,高度設為 -1 (wdUndefined)
' iShp.Height = -1
Next iShp
Application.ScreenUpdating = True
MsgBox "所有內嵌圖片已設定為 " & cmWidth & "×" & cmHeight & " cm", vbInformation
End Sub
Sub GetInlinePicturePixels()
On Error Resume Next
With Selection.Range.Find
.ClearFormatting
.Execute FindText:="^g", MatchWildcards:=False, Wrap:=wdFindStop ' 尋找下一張圖片
End With
If Selection.InlineShapes.Count > 0 Then
With Selection.InlineShapes(1)
MsgBox "圖片像素尺寸:" & .Width & " × " & .Height & " 點" & vbCrLf & _
"約 " & Round(.Width / 28.3464567, 1) & " × " & Round(.Height / 28.3464567, 1) & " cm", _
vbInformation, "圖片資訊"
End With
Else
MsgBox "游標處無內嵌圖片", vbExclamation
End If
End Sub
補充:Word 內部單位為「點」, 1 pt = 1/72 inch。螢幕像素需依 DPI 換算(96 DPI 下 1 pt ≈ 1.33 px)。
Sub CopyExcelRangeToWord()
Dim xlApp As Object, xlWb As Object, xlRng As Object
Dim wdApp As Object, wdDoc As Object
' 1. 取得/建立 Excel 應用程式
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If xlApp Is Nothing Then Set xlApp = CreateObject("Excel.Application")
On Error GoTo 0
' 2. 開啟/指定活頁簿與範圍
Set xlWb = xlApp.Workbooks.Open("C:\Data\Report.xlsx")
Set xlRng = xlWb.Worksheets("Sheet1").Range("B3:K11") ' 圖表/表格區域
' 3. 複製為圖片
xlRng.CopyPicture Appearance:=1, Format:=-4147 ' xlScreen, xlPicture
' 4. 建立/啟動 Word 並貼上
Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True
Set wdDoc = wdApp.Documents.Add
wdApp.Selection.Paste
' 5. 清理
Set wdDoc = Nothing
Set wdApp = Nothing
xlWb.Close False
Set xlWb = Nothing
' xlApp.Quit ' 視需求決定是否關閉 Excel
MsgBox "Excel 區域已以圖片貼上至 Word", vbInformation
End Sub
| 集合/物件 | 代表 | 關鍵屬性/方法 |
|---|---|---|
Document.InlineShapes | 所有內嵌圖片 | .Width, .Height, .ConvertToShape, .ScaleWidth |
Document.Shapes | 所有浮動圖片/繪圖物件 | .Rotation, .IncrementRotation, .ConvertToInlineShape, .WrapFormat |
ShapeRange | 多重選取形狀集合 | 同 Shape,支援批次操作 |
InlineShape.Range | 圖片所在的 Range | .Select, .Copy, .Delete |
Application.ScreenUpdating = False。ConvertToShape/ConvertToInlineShape 會改變錨點與版面流向,批次操作前請備份。CreateObject 避免版本相依。掌握這五大模組,即可程式化處理 Word 文件中 90% 以上的圖片格式需求。