トップ回答者
wordファイルのフッターに存在する表に章番号入りのページ番号を設定できない

質問
-
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:=TrueInsert メソッドは失敗しました。BuildingBlockオブジェクト
とエラーメッセージが表示されてしまいます。
お手数をおかけしますが、アドバイスいただけますと嬉しいです。
回答
-
こんにちは。
下記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
- 編集済み きぬあさMVP 2017年9月13日 1:35
- 回答の候補に設定 電腦神手吳子陵 2017年9月21日 9:19
- 回答としてマーク 栗下 望Microsoft employee, Moderator 2017年9月25日 0: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
- 編集済み きぬあさMVP 2017年9月13日 1:35
- 回答の候補に設定 電腦神手吳子陵 2017年9月21日 9:19
- 回答としてマーク 栗下 望Microsoft employee, Moderator 2017年9月25日 0:34