none
wordファイルのフッターに存在する表に章番号入りのページ番号を設定できない RRS feed

  • 質問

  • Word初心者です。

    たくさんのwordファイルのフッターに存在する表(2,4)にページ番号(章番号を含む)を

    設定したく、VBAを組みましたが、問題があります。

         

    For Each sec In myDoc.Sections
    Set myDoc = Documents.Open
    with sec.Footers(wdHeaderFooterPrimary)
    .PageNumbers.HeadingLevelForChapter = 3
    .ChapterPageSeparator = wdSeparatorHyphen
    .Range.Tables(1).Cell(2, 4).Formula Formula:="PAGE \* Arabic "
    end with
    Next sec

    ページ番号は表示されますが、章番号が含まれません。

    リボンから確認しても設定されていません。

    別の方法として、.Range.Tables(1).Cell(2, 4).Formula Formula:="PAGE \* Arabic "の代わりに

    以下を試しましたが、(pageには、ページ番号設定があります。)

    Application.Templates( _
                                   "C:\Users\WZ87878\AppData\Roaming\Microsoft\Templates\Normal.dotm"). _
                                   BuildingBlockEntries("page").Insert Where:=sec.Footers(wdHeaderFooterPrimary).Range.Tables(1).Cell(2, 4).Range, RichText:=True

    Insert メソッドは失敗しました。BuildingBlockオブジェクト

    とエラーメッセージが表示されてしまいます。

    お手数をおかけしますが、アドバイスいただけますと嬉しいです。

    2017年9月12日 13:20

回答

  • こんにちは。

    下記Forループは恐らく記述ミスだと思いますが、それは置いておきまして、

    > For Each sec In myDoc.Sections
    > Set myDoc = Documents.Open

    下記のようにページ番号を設定した後「MERGEFORMAT」スイッチを付けてフィールドを挿入してはいかがでしょうか?

    Public Sub Sample()
      Dim sec As Word.Section
      Dim f As Word.HeaderFooter
      
      For Each sec In ActiveDocument.Sections
        For Each f In sec.Footers
          If f.Range.Tables.Count > 0 Then
            'ページ番号設定
            With f.PageNumbers
              .NumberStyle = wdPageNumberStyleArabic
              .IncludeChapterNumber = True '章番号を含む
              .ChapterPageSeparator = wdSeparatorHyphen
            End With
            
            With f.Range.Tables.Item(1)
              .Cell(2, 4).Formula Formula:="PAGE   \* MERGEFORMAT"
            End With
          End If
        Next
      Next
    End Sub


    2017年9月13日 1:34

すべての返信

  • こんにちは。

    下記Forループは恐らく記述ミスだと思いますが、それは置いておきまして、

    > For Each sec In myDoc.Sections
    > Set myDoc = Documents.Open

    下記のようにページ番号を設定した後「MERGEFORMAT」スイッチを付けてフィールドを挿入してはいかがでしょうか?

    Public Sub Sample()
      Dim sec As Word.Section
      Dim f As Word.HeaderFooter
      
      For Each sec In ActiveDocument.Sections
        For Each f In sec.Footers
          If f.Range.Tables.Count > 0 Then
            'ページ番号設定
            With f.PageNumbers
              .NumberStyle = wdPageNumberStyleArabic
              .IncludeChapterNumber = True '章番号を含む
              .ChapterPageSeparator = wdSeparatorHyphen
            End With
            
            With f.Range.Tables.Item(1)
              .Cell(2, 4).Formula Formula:="PAGE   \* MERGEFORMAT"
            End With
          End If
        Next
      Next
    End Sub


    2017年9月13日 1:34
  • ありがとうございます。

    解決しました!

    相当煮詰まっていたので、大変助かりました。

    2017年9月13日 10:45