Word VBA/マクロ 備忘録

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

【Word VBA/マクロ】基本構文(段落、図、表処理ほか)

よく使用する構文をまとめておきます。
よく使用するコードはこちら
検索の基本構文はこちら

Sub 段落処理の基本構文()
'文書内の段落を1つずつ処理します。
    Dim para As Paragraph
    For Each para In ActiveDocument.Paragraphs
        '処理内容を書く
    Next
End Sub
Sub 選択段落の文字を表示()
'選択段落を1つずつ処理します。
    Dim par As Paragraph
    For Each par In Selection.Paragraphs
        '処理内容を書く
    Next
End Sub
Sub 全ての文書処理の基本構文()
    Dim doc As Document
    For Each doc In Documents
        '処理内容を書く
    Next
End Sub
Sub 表処理の基本構文()
    Dim tbl As Table    
    For Each tbl In ActiveDocument.Tables
        '各表に対する処理を書く
    Next
End Sub
Sub 行列セル処理の基本構文()
    Dim tbl As Table, myRow As row
    Dim myColumn As Column, myCell As cell
    
    Set tbl = Selection.Tables(1)
    
    For Each myRow In tbl.Rows
        '各行に対する処理を書く
    Next
    
    For Each myColumn In tbl.Columns
        '各列に対する処理を書く
    Next
    
    For Each myCell In Selection.Cells
        '各セルに対する処理を書く
    Next
End Sub
Sub 図処理の基本構文()
    Dim sp As Shape
    For Each sp In ActiveDocument.Shapes
        '処理内容を書く
    Next
End Sub
Sub グループ化図形処理の基本構文()
    Dim shp As Shape, gpshp As Shape    
     For Each shp In ActiveDocument.Shapes
      If shp.Type = msoGroup Then
        For Each gpshp In shp.GroupItems
            '処理内容を書く
        Next
      End If
    Next shp
End Sub
Sub ページ内の図を処理する基本構文()
    Dim shp As Shape
    For Each shp In ActiveDocument.Bookmarks("\Page"). _
        Range.ShapeRange
        '処理内容を書く
    Next
Sub 行内の図の基本構文()
    Dim ilSp As InlineShape
    For Each ilSp In ActiveDocument.InlineShapes
        '処理内容を書く
    Next
End Sub
Sub セクション処理の基本構文()
    Dim sec As Section
    For Each sec In ActiveDocument.Sections
        '処理の内容を書く
    Next
End Sub
Sub ヘッダーフッター処理の基本構文()
    Dim hdr As HeaderFooter, ftr As HeaderFooter
    Dim sec As Section
    
    For Each sec In ActiveDocument.Sections
        For Each hdr In sec.Headers
            'ヘッダーに対する処理内容を書く
        Next
        
        For Each ftr In sec.Footers
            ftr.Range.Font.Hidden = False
            'フッターに対する処理内容を書く
        Next
    Next
End Sub
Sub ヘッダーフッターの図形処理の基本構文()
    Dim shp As Shape
    Dim sec As Section, hd_ft As HeaderFooter
    
    For Each sec In ActiveDocument.Sections
        For Each hd_ft In sec.Headers
            For Each shp In hd_ft.Shapes
                'ヘッダー図形の処理内容を書く
            Next
        Next

        For Each hd_ft In sec.Footers
            For Each shp In hd_ft.Shapes
                'フッター図形の処理内容を書く
            Next
        Next
    Next
End Sub
Sub 脚注処理の基本構文()
    Dim ftNote As Footnote       
    For Each ftNote In ActiveDocument.Footnotes
        '処理内容を書く
    Next
End Sub
Sub 文末脚注処理の基本構文()
    Dim endNote As endNote
       
    For Each endNote In ActiveDocument.Endnotes
        '処理内容を書く
    Next
End Sub
Sub ページ毎に処理する基本構文()
    Dim i As Long
    Dim doc As Document
    Set doc = ActiveDocument
    
    ActiveWindow.View = wdPrintView
    doc.Range(0, 0).Select

    For i = 1 To Selection.Information(wdNumberOfPagesInDocument)
        Set rng = doc.Bookmarks("\Page").Range
            '処理内容を書く
        Selection.GoToNext What:=wdGoToPage
    Next
End Sub
Sub ブックマーク処理の基本構文()
    Dim bk As Bookmark    
    For Each bk In ActiveDocument.Bookmarks
        '処理内容を書く
    Next
End Sub
Sub コメント処理の基本構文()
    Dim cnt As Comment
    For Each cnt In ActiveDocument.Comments
        '処理内容を書く
    Next
End Sub
Sub 一字ずつ処理する基本構文()
'選択段落のテキストを1字ずつ処理します。
    Dim chr As Range
    For Each chr In Selection.Paragraphs(1).Range.Characters
        '処理内容を書く
    Next
End Sub
Sub ドキュメントプロパティを処理する基本構文()
    Dim docProp As DocumentProperty
    For Each docProp In ActiveDocument.BuiltInDocumentProperties
        '処理内容を書く
    Next
End Sub
Sub 行内のスマートアート処理の基本構文()
    Dim shp As Shape, inlShp As InlineShape
    Dim satNode As SmartArtNode
    For Each inlShp In ActiveDocument.InlineShapes
        If inlShp.Type = wdInlineShapeSmartArt Then
            For Each satNode In inlShp.SmartArt.AllNodes
                '処理の内容を書く
            Next
        End If
    Next
End Sub
Sub 指定フォルダ内のファイル処理の基本構文()
    Dim Path As String
    Dim fs As Scripting.FileSystemObject
    Dim baseFolder As Scripting.Folder
    Dim mySubFile As Scripting.File, mySubFiles As Scripting.FILES
    Dim dc As Document
    Dim hidFile As Boolean
    Dim dlg As FileDialog
    
    On Error Resume Next
    Set dlg = Application.FileDialog(msoFileDialogFolderPicker)
    
    ' キャンセルボタンクリック時にマクロを終了
    If dlg.Show = False Then Exit Sub
    ' フォルダーのフルパスを変数に格納
    Path = dlg.SelectedItems(1)    
    
    Set fs = New Scripting.FileSystemObject
    Set baseFolder = fs.GetFolder(Path)
    Set mySubFiles = baseFolder.FILES
    
    For Each mySubFile In mySubFiles
        hidFile = False
        '隠しファイルの判定
        If mySubFile.Attributes = 2 Or _
            mySubFile.Attributes = 34 Then
            hidFile = True
        End If
        
        If hidFile = False Then
            If fs.GetExtensionName(mySubFile.Path) = "doc" Or _
                fs.GetExtensionName(mySubFile.Path) = "docx" Then
                Documents.Open mySubFile.Path, ReadOnly:=True
                Set dc = ActiveDocument
                                                
                '処理内容を書く

                dc.Close SaveChanges:=False
            End If
        End If
    Next
End Sub


検索の基本構文は次のページへ移動しました。
rapoppo.hatenadiary.jp