简体   繁体   English

使用VBA在MS Word中创建动态列表号

[英]Creating a dynamic list number in MS word using VBA

I wonder how can I insert dynamic multilevel list numbers. 我想知道如何插入动态多级列表号。
The client request applying his Word 2010 template a "Point Headings" - similar to Point Pages behavior. 客户端请求将其Word 2010模板应用为“点标题”-与点页面行为类似。

Meaning: 含义:
Assuming the following Heading formats: 假设以下标题格式:
Heading 2 - 1.1 标题2-1.1
Heading 3 - 1.1.1 标题3-1.1.1
Heading 4 - 1.1.1.1 标题4-1.1.1.1
Heading 5 - 1.1.1.1.1 标题5-1.1.1.1.1

The purpose is to keep existent numbering and add in between numbered list. 目的是保持现有编号并在编号列表之间添加。
For example: 例如:
Between two Heading 3 style headers 1.3.5 and 1.3.6 will come 1.3.5.A followed by 1.3.5.B and so on. 在两个标题3样式标题1.3.5和1.3.6之间将出现1.3.5.A,然后是1.3.5.B,依此类推。 obviously a requirement is that 1.3.6 won't be changed. 显然,要求1.3.6不会被更改。

What I wish to get & the easiest way to get there (in my opinion): 我希望达到的目标和最简单的到达目标的方法(在我看来):
I want 4 macro subroutines (one for each heading style) which will insert in the current caret position (can be assumed that Selection is the caret location alone) the selected "point heading". 我想要4个宏子例程(每个标题样式一个),它们将插入当前的插入符号位置(可以假定Selection仅是插入符号位置)中所选的“点标题”。

My thinking was to execute the following flow: 我的想法是执行以下流程:
(Line format: Action - Example) (行格式:操作-示例)

  • Apply selected style (let's say Heading 3) - result: 1.2.3 应用选定的样式(例如标题3)-结果:1.2.3
  • Find previous value (can be assumed that it isn't the first one) - result: 1.2.2 查找先前的值(可以假设它不是第一个)-结果:1.2.2
  • Copy the string content (using ListFormat.ListString ) - result: value 1.2.2 is copied 复制字符串内容(使用ListFormat.ListString )-结果:复制了值1.2.2
  • Apply a new style to this specific location with alphabetical number format and the prefix I have. 使用字母数字格式和前缀将新样式应用于此特定位置。 - result: The list number will be 1.2.2.A -结果:列表编号为1.2.2.A

What I've tried so far: 到目前为止,我已经尝试过:

  • First sanity check was to manually insert a new list style in the multiLevel list menu. 首要的检查是在multiLevel列表菜单中手动插入新的列表样式。 The problem is that this style cannot be used more than once (or at least that's what I've seen). 问题在于这种样式不能被多次使用(或者至少是我所看到的)。 If I wish to change the number prefix, it applies to all of its siblings with the same style. 如果我想更改数字前缀,则将其应用于所有具有相同样式的兄弟姐妹。

  • Then I've tried a code snippet taken from HERE with some adjustments 然后,我尝试了从此处获取的代码段,并进行了一些调整

     Sub applyPointHeadingTest() With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(2) .NumberFormat = "1.1.%2" .TrailingCharacter = wdTrailingSpace .NumberStyle = wdListNumberStyleUppercaseLetter .NumberPosition = CentimetersToPoints(0) .Alignment = wdListLevelAlignLeft .TextPosition = CentimetersToPoints(5.31) .TabPosition = CentimetersToPoints(5.95) .StartAt = 1 .LinkedStyle = "Point Heading Style 2" End With ListGalleries(wdOutlineNumberGallery).ListTemplates(1).name = "ComplexNo2" Selection.Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _ ListGalleries(wdOutlineNumberGallery).ListTemplates(1), _ ContinuePreviousList:=True, ApplyTo:=wdListApplyToSelection, _ DefaultListBehavior:=wdWord10ListBehavior End Sub 

    While this work for the first time I run the method, on next executions it changes every list in the document. 当我第一次运行该方法时,在下次执行时,它会更改文档中的每个列表。

  • That's it basically. 基本上就是这样。 I'm out of ideas.. 我没主意了。

Can anyone guide me how to do that? 谁能指导我该怎么做?

Thanks. 谢谢。

Field codes can be used for "intermittent" numbering that's related to the Heading numbering, but doesn't fit for some reason in the ListTemplate assigned to the Heading levels. 字段代码可用于与“标题”编号相关的“间歇”编号,但由于某种原因而不适用于分配给“标题”级别的ListTemplate。 This numbering is dynamic, in that it updates according to referenced numbering preceding it. 该编号是动态的,因为它根据其前面的引用编号进行更新。 It cannot, however, update automatically - the field results must be updated either manually (F9, most often Ctrl+A, F9 for the entire document) or using a macro ( Fields.Update ). 但是,它不能自动更新-字段结果必须手动更新(F9,对于整个文档通常是Ctrl + A,F9)或使用宏( Fields.Update )。 You can also set the document's Print options to automatically update fields. 您还可以将文档的“打印”选项设置为自动更新字段。

For the situation you outline, this pair of fields should work: 对于您概述的情况,这对字段应该起作用:

{ StyleRef 3 \n }{ SEQ "between" \* ALPHABETIC \s 3 }
  • StyleRef reflects the first preceding instance of the specified style. StyleRef反映指定样式的第一个在前实例。 In this case, that's "Heading 3" - you can use 1 - 9 to specify a built-in Heading style which makes the field local language independent. 在这种情况下,这就是“标题3”-您可以使用1-9指定内置的标题样式,该样式使字段本地语言独立。
  • The \\n displays only the numbering, no text. \\n仅显示编号,不显示文本。
  • SEQ inserts sequential numbering - you give it a label for the numbering belonging to that sequence throughout the document. SEQ插入顺序编号-您在整个文档中给该标签赋予该编号的标签。
  • \\* ALPHABETIC puts the result in capitalized letters. \\* ALPHABETIC将结果以大写字母表示。
  • \\s lets you say from what Heading level onwards it should restart numbering. \\s可让您说出应从哪个标题级别开始重新编号。

If you want to use code to insert these fields, then something along these lines: 如果要使用代码插入这些字段,请遵循以下几行:

Dim rng As word.Range
Dim fld As word.Field
Dim sQuote As String

sQuote = Chr(34)
Set rng = Selection.Range
Set fld = rng.Fields.Add(rng, wdFieldEmpty, "StyleRef 3 \n", False)
Set rng = fld.result
'Move focus after the inserted field
rng.Collapse wdCollapseEnd
rng.MoveStart wdCharacter, 1
rng.InsertAfter "."
rng.Collapse wdCollapseEnd
rng.Fields.Add rng, wdFieldEmpty, "SEQ " & sQuote & "between" & _
                    sQuote & " \* ALPHABETIC \s 3", False
rng.Parent.Fields.Update 'update fields in the document body

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM