使用VBA选择Word中预定义的多级列表样式。

huangapple go评论72阅读模式
英文:

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()
&#39;
&#39; Macro3 Macro
&#39;
&#39;
With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(1)
.NumberFormat = &quot;%1)&quot;
.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 = &quot;&quot;
End With
.LinkedStyle = &quot;&quot;
End With
With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(2)
.NumberFormat = &quot;%2)&quot;
.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 = &quot;&quot;
End With
.LinkedStyle = &quot;&quot;
End With
With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(3)
.NumberFormat = &quot;%3)&quot;
.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 = &quot;&quot;
End With
.LinkedStyle = &quot;&quot;
End With
With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(4)
.NumberFormat = &quot;(%4)&quot;
.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 = &quot;&quot;
End With
.LinkedStyle = &quot;&quot;
End With
With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(5)
.NumberFormat = &quot;(%5)&quot;
.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 = &quot;&quot;
End With
.LinkedStyle = &quot;&quot;
End With
With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(6)
.NumberFormat = &quot;(%6)&quot;
.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 = &quot;&quot;
End With
.LinkedStyle = &quot;&quot;
End With
With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(7)
.NumberFormat = &quot;%7.&quot;
.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 = &quot;&quot;
End With
.LinkedStyle = &quot;&quot;
End With
With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(8)
.NumberFormat = &quot;%8.&quot;
.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 = &quot;&quot;
End With
.LinkedStyle = &quot;&quot;
End With
With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(9)
.NumberFormat = &quot;%9.&quot;
.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 = &quot;&quot;
End With
.LinkedStyle = &quot;&quot;
End With
ListGalleries(wdOutlineNumberGallery).ListTemplates(1).Name = &quot;&quot;
Selection.Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
ListGalleries(wdOutlineNumberGallery).ListTemplates(1), _
ContinuePreviousList:=False, ApplyTo:=wdListApplyToWholeList, _
DefaultListBehavior:=wdWord10ListBehavior
Selection.TypeText Text:=&quot;Test&quot;
Selection.TypeParagraph
Selection.TypeText Text:=&quot;Test&quot;
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(&quot;Heading 1&quot;)

It's also possible to invoke styles with keyboard shortcuts, so you don't necessarily need to use VBA at all.

huangapple
  • 本文由 发表于 2020年1月6日 23:43:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614983.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定