萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 辦公室軟體 >  >> Word 教學 >> Word 使用技巧

Word VBA 圖片格式控制全攻略:旋轉、內嵌轉換、尺寸設定、像素取得、跨應用程式貼上

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)。

模組五:從 Excel 複製圖表區域貼上至 Word

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 會改變錨點與版面流向,批次操作前請備份。
  • 跨應用程式自動化 (Excel→Word) 需參考對應類型庫或使用晚期繫結 CreateObject 避免版本相依。

掌握這五大模組,即可程式化處理 Word 文件中 90% 以上的圖片格式需求。

copyright © 萬盛學電腦網 all rights reserved