简体   繁体   English

VBA代码未退出列表(MS Word)

[英]VBA code not exiting list (MS Word)

Writing a macro to automatically fix paraphrase spacing issues in MS Word docs generated from software we use. 编写一个宏来自动修复我们使用的软件生成的MS Word文档中的释义间隔问题。

Goal: 目标:

  • All standard paragraphs have 0pt before and after spacing. 所有标准段落在间距之前和之后都有0pt
  • All bullet lists have 3pt before and after spacing. 所有子弹列表在间距之前和之后都有3pt

Progress: 进展:
Currently I have a function that sets the entire document to 0pt , then looks through for all lists and changes them to 3pt . 目前我有一个函数将整个文档设置为0pt ,然后查看所有列表并将它们更改为3pt (currently also have a highlight on so I can easily see what is being treated as a list). (目前也有一个亮点,所以我可以很容易地看到什么被视为列表)。

It works great on some parts, but on other parts (I assume based on how the software we use generates the document), the list doesn't exist and it will continue to format blocks of text and heading to 3pt when it is not wanted (see attached images). 它在某些部分很有用,但在其他部分(我假设我们使用的软件如何生成文档),列表不存在,它将继续格式化文本块并在3pt时前往3pt (见附图)。

Current code is: 目前的代码是:

Sub Paragraph()
    ActiveDocument.Range.ParagraphFormat.SpaceAfter = 0
    ActiveDocument.Range.ParagraphFormat.SpaceBefore = 0

    Dim li As Word.list

    For Each li In ActiveDocument.lists
            li.Range.ParagraphFormat.SpaceBefore = 3
            li.Range.ParagraphFormat.SpaceAfter = 3
            li.Range.HighlightColorIndex = wdYellow
    Next li
End Sub

Working: 工作:

工作

Not working: 不工作:

不工作

According to the MSDN : 根据MSDN

List Object: Represents a single list format that's been applied to specified paragraphs in a document. 列表对象:表示已应用于文档中指定段落的单个列表格式。

So if you have more than one list with some non-bulleted paragraph(s) in the middle, the Range will start with the first item of the first list and end with the last item of the last list including all non-bulleted paragraph(s) in the middle. 因此,如果您有多个列表中间有一些非项目符号的段落,则Range将从第一个列表的第一个项目开始,以最后一个列表的最后一个项目结束,包括所有非项目符号段落( s)在中间。

To fix this issue, you need to separate the lists (right-click on the bullet and select Separate List ). 要解决此问题,您需要分隔列表(右键单击项目符号并选择单独列表 )。 However, you mentioned that the document was generated by some software, so that is probably not an option. 但是,您提到该文档是由某些软件生成的,因此可能不是一种选择。 In that case, you will have to iterate though the paragraphs of the Range of each List and check if it has a ListFormat.ListTemplate which indicates that it is a list item, otherwise it is a non-bulleted paragraph: 在这种情况下,您将不得不迭代每个ListRange的段落,并检查它是否有一个ListFormat.ListTemplate ,表明它是一个列表项,否则它是一个非项目符号的段落:

Sub Paragraph()
    ActiveDocument.Range.ParagraphFormat.SpaceAfter = 0
    ActiveDocument.Range.ParagraphFormat.SpaceBefore = 0

    Dim li As Word.List
    Dim p As Paragraph

    For Each li In ActiveDocument.Lists
        For Each p In li.Range.Paragraphs
            If Not p.Range.ListFormat.ListTemplate Is Nothing Then
                p.Range.ParagraphFormat.SpaceBefore = 3
                p.Range.ParagraphFormat.SpaceAfter = 3
                p.Range.HighlightColorIndex = wdYellow
            End If
        Next p
    Next li
End Sub

Even before touching VBA: 甚至在触摸VBA之前:

  • Use Styles in the document. 在文档中使用样式。
  • Limit the use of Styles in the document to only those that are in the template. 将文档中样式的使用限制为仅限模板中的样式。
  • Set your spacing in the Styles. 在样式中设置间距。

If, at some stage, you change your mind and want to use 6pt spacing, you can adjust the template and re-apply it, rather than finding all the VBA code and re-writing it. 如果在某个阶段,您改变主意并希望使用6pt间距,则可以调整模板并重新应用它,而不是查找所有VBA代码并重新编写它。 Not only that, but by using Styles, you can avoid having VBA code, or having VBA-enabled documents which may interfere with some corporate security settings. 不仅如此,通过使用样式,您可以避免使用VBA代码,或者使用可能会干扰某些公司安全设置的启用VBA的文档。

Oh, and set up your corporate structure to limit the use of templates to only the approved ones. 哦,并设置您的公司结构,以限制模板的使用仅限于批准的模板。

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

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