使用VBA在MS Word中创建动态列表号 [英] Creating a dynamic list number in MS word using VBA

查看:428
本文介绍了使用VBA在MS Word中创建动态列表号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何插入动态多级列表号.
客户请求为他的Word 2010模板应用点标题",类似于点页面行为.

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.

含义:
假定以下标题格式:
标题2-1.1
标题3-1.1.1
标题4-1.1.1.1
标题5-1.1.1.1.1

Meaning:
Assuming the following Heading formats:
Heading 2 - 1.1
Heading 3 - 1.1.1
Heading 4 - 1.1.1.1
Heading 5 - 1.1.1.1.1

目的是保留现有编号并在编号列表之间添加.
例如:
在两个标题3样式标题1.3.5和1.3.6之间将是1.3.5.A,然后是1.3.5.B,依此类推.显然,一个要求是1.3.6不会被更改.

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. obviously a requirement is that 1.3.6 won't be changed.

我希望获得的&到达那里最简单的方法(我认为):
我想要4个宏子例程(每个标题样式一个),它们将插入当前的插入符号位置(可以假定Selection仅是插入符号位置)中选定的点标题".

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".

我的想法是执行以下流程:
(行格式:操作-示例)

My thinking was to execute the following flow:
(Line format: Action - Example)

  • 应用选定的样式(例如标题3)-结果:1.2.3
  • 查找先前的值(可以假设它不是第一个)-结果:1.2.2
  • 使用ListFormat.ListString复制字符串内容-结果:复制了值1.2.2
  • 使用字母数字格式和我所拥有的前缀将新样式应用于此特定位置. -结果:列表编号为1.2.2.A
  • Apply selected style (let's say Heading 3) - result: 1.2.3
  • Find previous value (can be assumed that it isn't the first one) - result: 1.2.2
  • Copy the string content (using ListFormat.ListString) - result: value 1.2.2 is copied
  • 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

到目前为止,我已经尝试过:

  • 第一个健全性检查是在multiLevel列表菜单中手动插入新的列表样式.问题在于这种样式不能被多次使用(或者至少这是我所看到的).如果我想更改数字前缀,则将其应用于所有具有相同样式的兄弟姐妹.

  • First sanity check was to manually insert a new list style in the multiLevel list menu. 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?

谢谢.

推荐答案

字段代码可用于与标题"编号相关的间歇性"编号,但由于某些原因不适用于分配给标题"级别的ListTemplate .该编号是动态的,因为它根据其前面的引用编号进行更新.但是,它不能自动更新-字段结果必须手动更新(F9,对于整个文档通常是Ctrl + A,F9)或使用宏(Fields.Update).您还可以将文档的打印"选项设置为自动更新字段.

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. 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). 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反映指定样式的第一个在前实例.在这种情况下,这就是标题3"-您可以使用1-9指定内置的标题样式,从而使字段本地语言独立.
  • \n仅显示编号,不显示文本.
  • SEQ插入顺序编号-您在整个文档中给它指定属于该顺序的编号.
  • \* ALPHABETIC将结果以大写字母表示.
  • \s让您说应该从哪个标题级别开始重新编号.
    • StyleRef reflects the first preceding instance of the specified style. 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.
    • The \n displays only the numbering, no text.
    • SEQ inserts sequential numbering - you give it a label for the numbering belonging to that sequence throughout the document.
    • \* ALPHABETIC puts the result in capitalized letters.
    • \s lets you say from what Heading level onwards it should restart numbering.
    • 如果您想使用代码插入这些字段,则遵循以下内容:

      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
      

      这篇关于使用VBA在MS Word中创建动态列表号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆