Word 2019 宏将列表项目符号转换为简单文本

问题描述 投票:0回答:1

我需要一种方法将Word文档中的列表项目符号转换为简单文本。

该文档有以下类型的项目符号:

•→Lorem ipsum dolor sit amet  
   •→Lorem ipsum dolor sit amet  
      •→Lorem ipsum dolor sit amet  
        •→Lorem ipsum dolor sit amet  
          •→Lorem ipsum dolor sit amet  
            •→Lorem ipsum dolor sit amet  

我需要它们看起来像这样:

•Lorem ipsum dolor sit amet  
   A1Lorem ipsum dolor sit amet  
      B1Lorem ipsum dolor sit amet  
         C1Lorem ipsum dolor sit amet  
            D1Lorem ipsum dolor sit amet  
              E1Lorem ipsum dolor sit amet  

并非所有列表都会达到此深度(初始项目符号下有 5 个级别),因此需要有一种方法来检测列表的末尾并开始下一个列表。

尝试过大部分手动操作,但无济于事,例如将Word内容复制到各种其他程序,试图获取带有前导项目符号的列表文本和没有Word格式的文本,但无济于事。

过去曾做过基本的Word宏,但这对于我有限的经验来说有点复杂。另外,VB 经验有限。

vba ms-word
1个回答
0
投票
  • 代码已在Microsoft 365上测试。希望与Word 2019没有兼容性问题。

微软文档:

ListFormat.ConvertNumbersToText 方法(Word)

ListFormat.RemoveNumbers 方法(Word)

Range.InsertBefore 方法(Word)

Sub ConvertBulletsToText()
    Dim oPara As Paragraph, indentStr As String
    Dim indentLevel As Long, i As Long, aIndex() As Long
    Const INDENT_BLOCK = "   " ' indent spaces for each level, modify as needed
    Const MAX_LEVEL = 6
    ' Loop through each paragraph
    For Each oPara In ActiveDocument.Paragraphs
        ' Check if the paragraph has a bullet
        If oPara.Range.ListFormat.ListType <> wdListNoNumbering Then
            ' Get the indentation level
           indentLevel = oPara.Range.ListFormat.ListLevelNumber
            ' Remove the bullet from the paragraph
            If indentLevel = 1 Then
                oPara.Range.ListFormat.ConvertNumbersToText
                oPara.Range.Characters(2).Delete ' remove Tab after bullet
                ReDim aIndex(2 To MAX_LEVEL) ' Reset array
            Else
                If indentLevel <= MAX_LEVEL Then
                    indentStr = ""
                    For i = 2 To indentLevel
                        indentStr = indentStr & INDENT_BLOCK
                    Next
                    aIndex(indentLevel) = aIndex(indentLevel) + 1
                    indentStr = indentStr & Chr(63 + indentLevel) & aIndex(indentLevel)
                    oPara.Range.ListFormat.RemoveNumbers
                    oPara.Range.InsertBefore indentStr
                End If
            End If
        End If
    Next oPara
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.