英文:
Use VBA to select predefined multlevel list style in Word
问题
以下是翻译好的部分:
我正在尝试确定在键入文本之前选择我的预定义多级列表的正确语法,而不需要每次都重新定义VBA。
我仍在学习VBA,并了解录制宏的限制。显然,当我尝试应用我的预定义多级列表时,录制器设置了列表的所有属性。
我所尝试做的只是选择我的预定义多级列表,标题为“我的列表”,输入“文本”,然后进入下一行,再次输入“文本”。我的VBA代码超过100行,因为它定义了列表的每个级别,但如果我已经在使用保存的列表,我相信这是不必要的。
这不是我的确切多级列表,而是内置的一个。
英文:
I am trying to determine the right syntax for selecting my predefined multilevel list before typing text without the VBA redefining every time.
I am still learning VBA and know the limits of recording a macro. Apparently when I try to apply my predefined multilevel list, the recorder sets all of the attributes of the list.
All I am trying to do is select my predefined multilevel list titled "MyList", type "Text", go to next line, type "Text" again. My VBA is over 100 lines because it defines each level of list which I am sure is unnecessary if I am already using a saved list.
This is not my exact multilevel list but one of the built-in ones.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
Sub Macro3()
'
' Macro3 Macro
'
'
With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(1)
.NumberFormat = "%1)"
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleArabic
.NumberPosition = InchesToPoints(0)
.Alignment = wdListLevelAlignLeft
.TextPosition = InchesToPoints(0.25)
.TabPosition = wdUndefined
.ResetOnHigher = 0
.StartAt = 1
With .Font
.Bold = wdUndefined
.Italic = wdUndefined
.StrikeThrough = wdUndefined
.Subscript = wdUndefined
.Superscript = wdUndefined
.Shadow = wdUndefined
.Outline = wdUndefined
.Emboss = wdUndefined
.Engrave = wdUndefined
.AllCaps = wdUndefined
.Hidden = wdUndefined
.Underline = wdUndefined
.Color = wdUndefined
.Size = wdUndefined
.Animation = wdUndefined
.DoubleStrikeThrough = wdUndefined
.Name = ""
End With
.LinkedStyle = ""
End With
With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(2)
.NumberFormat = "%2)"
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleLowercaseLetter
.NumberPosition = InchesToPoints(0.25)
.Alignment = wdListLevelAlignLeft
.TextPosition = InchesToPoints(0.5)
.TabPosition = wdUndefined
.ResetOnHigher = 1
.StartAt = 1
With .Font
.Bold = wdUndefined
.Italic = wdUndefined
.StrikeThrough = wdUndefined
.Subscript = wdUndefined
.Superscript = wdUndefined
.Shadow = wdUndefined
.Outline = wdUndefined
.Emboss = wdUndefined
.Engrave = wdUndefined
.AllCaps = wdUndefined
.Hidden = wdUndefined
.Underline = wdUndefined
.Color = wdUndefined
.Size = wdUndefined
.Animation = wdUndefined
.DoubleStrikeThrough = wdUndefined
.Name = ""
End With
.LinkedStyle = ""
End With
With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(3)
.NumberFormat = "%3)"
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleLowercaseRoman
.NumberPosition = InchesToPoints(0.5)
.Alignment = wdListLevelAlignLeft
.TextPosition = InchesToPoints(0.75)
.TabPosition = wdUndefined
.ResetOnHigher = 2
.StartAt = 1
With .Font
.Bold = wdUndefined
.Italic = wdUndefined
.StrikeThrough = wdUndefined
.Subscript = wdUndefined
.Superscript = wdUndefined
.Shadow = wdUndefined
.Outline = wdUndefined
.Emboss = wdUndefined
.Engrave = wdUndefined
.AllCaps = wdUndefined
.Hidden = wdUndefined
.Underline = wdUndefined
.Color = wdUndefined
.Size = wdUndefined
.Animation = wdUndefined
.DoubleStrikeThrough = wdUndefined
.Name = ""
End With
.LinkedStyle = ""
End With
With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(4)
.NumberFormat = "(%4)"
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleArabic
.NumberPosition = InchesToPoints(0.75)
.Alignment = wdListLevelAlignLeft
.TextPosition = InchesToPoints(1)
.TabPosition = wdUndefined
.ResetOnHigher = 3
.StartAt = 1
With .Font
.Bold = wdUndefined
.Italic = wdUndefined
.StrikeThrough = wdUndefined
.Subscript = wdUndefined
.Superscript = wdUndefined
.Shadow = wdUndefined
.Outline = wdUndefined
.Emboss = wdUndefined
.Engrave = wdUndefined
.AllCaps = wdUndefined
.Hidden = wdUndefined
.Underline = wdUndefined
.Color = wdUndefined
.Size = wdUndefined
.Animation = wdUndefined
.DoubleStrikeThrough = wdUndefined
.Name = ""
End With
.LinkedStyle = ""
End With
With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(5)
.NumberFormat = "(%5)"
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleLowercaseLetter
.NumberPosition = InchesToPoints(1)
.Alignment = wdListLevelAlignLeft
.TextPosition = InchesToPoints(1.25)
.TabPosition = wdUndefined
.ResetOnHigher = 4
.StartAt = 1
With .Font
.Bold = wdUndefined
.Italic = wdUndefined
.StrikeThrough = wdUndefined
.Subscript = wdUndefined
.Superscript = wdUndefined
.Shadow = wdUndefined
.Outline = wdUndefined
.Emboss = wdUndefined
.Engrave = wdUndefined
.AllCaps = wdUndefined
.Hidden = wdUndefined
.Underline = wdUndefined
.Color = wdUndefined
.Size = wdUndefined
.Animation = wdUndefined
.DoubleStrikeThrough = wdUndefined
.Name = ""
End With
.LinkedStyle = ""
End With
With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(6)
.NumberFormat = "(%6)"
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleLowercaseRoman
.NumberPosition = InchesToPoints(1.25)
.Alignment = wdListLevelAlignLeft
.TextPosition = InchesToPoints(1.5)
.TabPosition = wdUndefined
.ResetOnHigher = 5
.StartAt = 1
With .Font
.Bold = wdUndefined
.Italic = wdUndefined
.StrikeThrough = wdUndefined
.Subscript = wdUndefined
.Superscript = wdUndefined
.Shadow = wdUndefined
.Outline = wdUndefined
.Emboss = wdUndefined
.Engrave = wdUndefined
.AllCaps = wdUndefined
.Hidden = wdUndefined
.Underline = wdUndefined
.Color = wdUndefined
.Size = wdUndefined
.Animation = wdUndefined
.DoubleStrikeThrough = wdUndefined
.Name = ""
End With
.LinkedStyle = ""
End With
With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(7)
.NumberFormat = "%7."
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleArabic
.NumberPosition = InchesToPoints(1.5)
.Alignment = wdListLevelAlignLeft
.TextPosition = InchesToPoints(1.75)
.TabPosition = wdUndefined
.ResetOnHigher = 6
.StartAt = 1
With .Font
.Bold = wdUndefined
.Italic = wdUndefined
.StrikeThrough = wdUndefined
.Subscript = wdUndefined
.Superscript = wdUndefined
.Shadow = wdUndefined
.Outline = wdUndefined
.Emboss = wdUndefined
.Engrave = wdUndefined
.AllCaps = wdUndefined
.Hidden = wdUndefined
.Underline = wdUndefined
.Color = wdUndefined
.Size = wdUndefined
.Animation = wdUndefined
.DoubleStrikeThrough = wdUndefined
.Name = ""
End With
.LinkedStyle = ""
End With
With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(8)
.NumberFormat = "%8."
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleLowercaseLetter
.NumberPosition = InchesToPoints(1.75)
.Alignment = wdListLevelAlignLeft
.TextPosition = InchesToPoints(2)
.TabPosition = wdUndefined
.ResetOnHigher = 7
.StartAt = 1
With .Font
.Bold = wdUndefined
.Italic = wdUndefined
.StrikeThrough = wdUndefined
.Subscript = wdUndefined
.Superscript = wdUndefined
.Shadow = wdUndefined
.Outline = wdUndefined
.Emboss = wdUndefined
.Engrave = wdUndefined
.AllCaps = wdUndefined
.Hidden = wdUndefined
.Underline = wdUndefined
.Color = wdUndefined
.Size = wdUndefined
.Animation = wdUndefined
.DoubleStrikeThrough = wdUndefined
.Name = ""
End With
.LinkedStyle = ""
End With
With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(9)
.NumberFormat = "%9."
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleLowercaseRoman
.NumberPosition = InchesToPoints(2)
.Alignment = wdListLevelAlignLeft
.TextPosition = InchesToPoints(2.25)
.TabPosition = wdUndefined
.ResetOnHigher = 8
.StartAt = 1
With .Font
.Bold = wdUndefined
.Italic = wdUndefined
.StrikeThrough = wdUndefined
.Subscript = wdUndefined
.Superscript = wdUndefined
.Shadow = wdUndefined
.Outline = wdUndefined
.Emboss = wdUndefined
.Engrave = wdUndefined
.AllCaps = wdUndefined
.Hidden = wdUndefined
.Underline = wdUndefined
.Color = wdUndefined
.Size = wdUndefined
.Animation = wdUndefined
.DoubleStrikeThrough = wdUndefined
.Name = ""
End With
.LinkedStyle = ""
End With
ListGalleries(wdOutlineNumberGallery).ListTemplates(1).Name = ""
Selection.Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
ListGalleries(wdOutlineNumberGallery).ListTemplates(1), _
ContinuePreviousList:=False, ApplyTo:=wdListApplyToWholeList, _
DefaultListBehavior:=wdWord10ListBehavior
Selection.TypeText Text:="Test"
Selection.TypeParagraph
Selection.TypeText Text:="Test"
End Sub
<!-- end snippet -->
答案1
得分: 3
多级列表样式使用普通的段落样式,由整体列表样式协调。在使用时,不要将列表样式应用于文本。而是应用其中一种段落样式。
首先,最好学习如何在Word中正确构建多级列表样式。这将确保您的样式呈现出您期望的外观。以下是Shauna Kelly的方法,这种方法非常可靠:如何在Word 2007和Word 2010中创建带编号标题或大纲编号
如果您按照文章中所述设置了标题1,您应用样式的VBA代码将是:
Selection.Style = ActiveDocument.Styles("Heading 1")
您还可以使用键盘快捷键调用样式,因此您不一定需要使用VBA。
英文:
Multilevel list styles use ordinary paragraph styles that are co-ordinated by an overall list style. In use, you do not apply the list style to text. Instead you apply one of the paragraph styles.
To start, it's best to learn how to construct a multilevel list style correctly in Word. This will ensure that your styles result in the appearance you expect. Here is Shauna Kelly's method, this works reliably: How to create numbered headings or outline numbering in Word 2007 and Word 2010
If you set up Heading 1 as described in the article, your VBA to apply the style will be:
Selection.Style = ActiveDocument.Styles("Heading 1")
It's also possible to invoke styles with keyboard shortcuts, so you don't necessarily need to use VBA at all.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论