我将完全在VBA中生成word文档,并希望为目录提供罗马数字页码,为文档的其余部分提供数字页码。我的目录涵盖了多个页面,并且页面大小是可变的。
如何仅对可变页面范围的目录获得罗马数字页面编号?
任何帮助将不胜感激。
答案 0 :(得分:0)
如果您不知道从哪里开始,请在Word中尝试以下方法:
答案 1 :(得分:0)
在该区域我也很喜欢Word,因此这里有一些代码来显示一个可能的示例。该代码清除了当前文档的内容(因此,请不要在现有文档中运行它!),然后生成一些标题,后跟一个目录,然后通过分节符将其拆分。分节符允许使用不同的页码格式(第一部分为罗马数字,第二部分为阿拉伯数字)。将for循环最多更改为100将演示多个ToC页面。可能会指出正确的方向。干杯。
Option Explicit
Public Sub PageNumbers()
Dim myRange As Range
Dim Counter As Long
Dim myTOC As TableOfContents
' Delete word document content
ActiveDocument.StoryRanges(wdMainTextStory).Delete
' Add in some headings for testing
Set myRange = ActiveDocument.Range(0, 0)
For Counter = 1 To 10
myRange.InsertAfter "Heading " & Counter
myRange.Style = WdBuiltinStyle.wdStyleHeading1
myRange.InsertParagraphAfter
Next
' Add in a page number
With ActiveDocument.Sections(1)
.Footers(wdHeaderFooterPrimary).PageNumbers.Add _
PageNumberAlignment:=wdAlignPageNumberLeft, _
FirstPage:=True
End With
' Add in a section break at the start of the document
Set myRange = ActiveDocument.Range(0, 0)
myRange.InsertBreak Type:=wdSectionBreakNextPage
myRange.InsertParagraphAfter
' Insert a table of contents (into the first section)
Set myRange = ActiveDocument.Range(0, 0)
Set myTOC = ActiveDocument.TablesOfContents.Add(myRange, True, 1, 3, False)
' Format the page number of the first section to have roman numerals
With ActiveDocument.Sections.Item(1).Footers.Item(1).PageNumbers
.NumberStyle = wdPageNumberStyleLowercaseRoman
.HeadingLevelForChapter = 0
.IncludeChapterNumber = False
.ChapterPageSeparator = wdSeparatorHyphen
.RestartNumberingAtSection = False
.StartingNumber = 0
End With
' Format the page number of the second section to have arabic numerals
With ActiveDocument.Sections.Item(2).Footers.Item(1).PageNumbers
.NumberStyle = wdPageNumberStyleArabic
.HeadingLevelForChapter = 0
.IncludeChapterNumber = False
.ChapterPageSeparator = wdSeparatorHyphen
.RestartNumberingAtSection = True
.StartingNumber = 1
End With
End Sub
输出: