【WordVBA】Wordマクロで文章に、日付とページのヘッダーを付ける


こんな感じでヘッダーの左側に保存した日付、右側にページ(0/0)をつけたいんです。

Excelマクロだとヘッダーを細かく指定できるのですが、Wordマクロだとうまく指定できないです・・・

そうだ、テーブルを作ろう。

ヘッダーにテーブルを作って、その中に書き込んだらうまく行きそう

'ヘッダーを編集します
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader

Set mytable = ActiveDocument.Tables.Add(Range:=Selection.Range, _
        NumColumns:=3, NumRows:=1)
    With Selection.Tables(1)
        If .Style <> "表 (格子)" Then
            .Style = "表 (格子)"
        End If
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = False
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = False
        .ApplyStyleRowBands = True
        .ApplyStyleColumnBands = False
    End With
    Selection.WholeStory

    Selection.Tables(1).Rows.Alignment = wdAlignRowLeft

    'セルの幅を%に変える
    Selection.Tables(1).AllowAutoFit = True 
    Selection.Tables(1).Columns.PreferredWidthType = wdPreferredWidthPercent


    'テーブルのスタイル 印刷するときに枠が出ないようにNoneにしておきます。
    Selection.Borders(wdBorderTop).LineStyle = wdLineStyleNone
    Selection.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
    Selection.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
    Selection.Borders(wdBorderRight).LineStyle = wdLineStyleNone
    Selection.Borders(wdBorderHorizontal).LineStyle = wdLineStyleNone
    Selection.Borders(wdBorderVertical).LineStyle = wdLineStyleNone
    Selection.Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone

    'テーブルの左端に日付を挿入する 日付の記述が怪しいかもです
    Selection.Font.Size = 6
    Selection.Tables(1).Cell(1, 1).PreferredWidth = 33
    Selection.Font.ColorIndex = wdBlack
    Selection.Font.Name = "MS Pゴシック"
    Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    Selection.TypeText text:=Year(Date) +"年"+ Month(Date) +"月"+ Day(Date) +"日"+ " 保存"

    'テーブルの真ん中
    Selection.Tables(1).Cell(1, 2).Select    
    Selection.Tables(1).Cell(1, 2).PreferredWidth = 33
    Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft

    'テーブルの右
    Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
    Selection.MoveRight Unit:=wdCell
    Selection.Tables(1).Cell(1, 3).PreferredWidth = 33

    Selection.Font.ColorIndex = wdBlack
    Selection.Font.Size = 6
    Selection.Font.Name = "MS Pゴシック"
    Selection.ParagraphFormat.Alignment = wdAlignParagraphRight

    'ページ番号を挿入(0/0)
        With Selection
            .ParagraphFormat.Alignment = wdAlignParagraphRight
            .TypeText text:="("
            .Fields.Add Range:=Selection.Range, Type:=wdFieldPage
            .TypeText text:="/"
            .Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, text:="NUMPAGES", PreserveFormatting:=True
            .TypeText text:=")"
        End With

これを実行すると、こんなテーブルがヘッダーの中にできます。

いい感じのやつが出来ました。

もう一回保存するとテーブルが二行になるんだけど・・・

テーブルを作成する前に、今のヘッダーを削除する処理を加えます。

テーブルがあったら削除する
  Cnt = ActiveDocument.Tables.Count

  If Cnt > 0 Then
    WordBasic.RemoveHeader
  End If

保存のイベントに加えます

以上の処理を保存のイベントに加えると、保存するたびに日付が更新されてページがつきます。

このほかにも、最後に更新した人の名前やこの文章を閲覧できる役職、ファイル名を入れたりと、色々応用が効くと思います。

社内の書式を改善して行きたい人は、一度試してみてはいかがでしょうか。