Word VBA/マクロ 備忘録

作業効率化のため作成したマクロをバックアップ代わりにアップしていきます。

【Word VBA/マクロ】文書内全てのオートシェイプのフォント変更

サンプルコードでできること

文書内の全てのオートシェイプのフォントを変更します。

コードの説明

  jFont = "MS ゴシック"
  eFont = "Arial"

変数に日本語フォントと英数字フォントを格納します。

  ActiveWindow.View.Type = wdPrintView

図を表示するため印刷レイアウト表示にしています。
(コードなしでもフォントは変更されます。)

    For Each shp In ActiveDocument.Shapes
        If shp.TextFrame.HasText Then
           '省略
        End If

文書内の図をひとつずつ確認して、テキストを持つ場合のみ、フォント変更を行います。


グループ化を含めたのオートシェイプが変更対象です。
行内の図やスマートアートを含める場合、次の記事を参考にしてください。
rapoppo.hatenadiary.jp

Private Sub AllShapeArial()
'文書内のオートシェイプすべてのフォントをMSゴシック+Arialにします
  Dim shp As Shape, gShp As Shape
  Dim jFont As String, eFont As String
  jFont = "MS ゴシック"
  eFont = "Arial"
  
  ActiveWindow.View.Type = wdPrintView
    For Each shp In ActiveDocument.Shapes
    With shp.TextFrame
        If .HasText Then
            With .TextRange.Font
                .NameFarEast = jFont
                .Name = eFont
            End With
        End If
    End With
    
    'グループ化されたオートシェイプ内のフォント変更
    If shp.Type = msoGroup Then
        For Each gShp In shp.GroupItems
            With gShp.TextFrame
                If .HasText Then
                    With .TextRange.Font
                        .NameFarEast = jFont
                        .Name = eFont
                    End With
                End If
            End With
        Next
    End If
  Next shp
End Sub