【问题标题】:VBA WORD How could I increment multilevel list number?VBA WORD 如何增加多级列表编号?
【发布时间】:2015-03-05 14:26:11
【问题描述】:

当我在其他 Sub 上输入特定数字时,我正在寻找修改列表分级数字的代码。
我的另一个 Sub 从段落中读取一个数字并将其保存为一个整数。
然后,有了这个整数,我需要做点什么:

输入列表:

1.5.BLABLABLA
1.5.1.BLIBLIBLI
(SOMEWHERE WILL BE A Paragraph with "14" Text, this to integer)

输出列表:

14.5.BLABLABLA
14.5.1.BLIBLIBLI

【问题讨论】:

  • 听起来你已经有了一些代码,你能告诉我们你到目前为止得到了什么

标签: list vba ms-word increment


【解决方案1】:

嗯,我处理得很好

Sub LimpiaTitulos()
    'Normalizador de títulos de nivel determinado(3 en este caso)
    ActiveDocument.Repaginate
    If ActiveDocument.BuiltInDocumentProperties(wdPropertyPages) > 50 Then
         If MsgBox("El documento tiene " & ActiveDocument.BuiltInDocumentProperties(wdPropertyPages) & _
        " páginas." & vbCr & "Esta macro no esta pensada para tantas páginas y puede afectar" & vbCr _
        & "al rendimiento general del ordenador." & vbCr & "¿Desea realmente ejecutarla?", vbYesNo + _
        vbInformation, "Güornin!!!!!") = vbYes Then
            GoTo Comienzo
        Else
            Exit Sub
        End If


    Else
    Comienzo:
    Dim ele As List, iTotalParas As Integer, iParacoutner As Integer, p As Paragraph, numeracion As String
         iTotalParas = ActiveDocument.ListParagraphs.Count


    'Dejamos el primer párrafo con texto, quitando retornos
    Borraespacios
    Set p = ActiveDocument.Paragraphs(1)
    lvl = Int(p.Range.Text)
    p.Range.Delete
    Borraespacios

    'Formato de título
    'Formato de título
        p.Range.Font.Size = 20
        p.Range.Font.Bold = True
        p.Range.Font.Italic = False
        p.Range.Font.Name = "Arial"
        p.OutlineLevel = wdOutlineLevel3

    '-------------------------------------------------REGEX----------------------------------------------------

        Dim lvl1 As New RegExp, lvl2 As New RegExp
        'REGEX primer nivel
        lvl1.Pattern = "[0-9]{1,}[.,][0-9]{1,}[.,][0-9]{1,}" 'Admite #.,#.,# - SUPONEMOS JAMAS HABRA #,.#,.#,.#
        lvl1.Global = False
        'REGEX segundo nivel
        lvl2.Pattern = "[0-9]{1,}[.,][0-9]{1,}" 'Admite #.,#
        lvl2.Global = False
        'Si nos topamos con un nivel 2 de mas caracteres de los deseados, lo tragamos

    '-----------------------------------------------FIN REGEX--------------------------------------------------
                For iParaCounter = 1 To iTotalParas
            '   Cogemos el parrafo actual y lo tratamos


                        Set p = ActiveDocument.ListParagraphs(iParaCounter)
                        'Metemos el formato de la numeración en un String
                            numeracion = p.Range.ListFormat.ListString
                            'Si es de más de 2 carácteres
                            'If Len(numeracion) > 2 Then
                                'Filtramos la numeración con la REGEX
                                If lvl1.test(numeracion) Then
                                    numeración = "1.1.1"
                                Else
                                    If lvl2.test(numeracion) Then
                                        numeracion = "1.1"
                                    Else
                                        GoTo Siguiente
                                    End If
                                End If

                                If p.Range.ListParagraphs.Count = 1 Then
                                        'Borramos el formato y aplicamos el nuevo

                                         p.Range.Select
                                         Selection.ClearFormatting
                                                If Len(numeracion) <= 4 Then

                                                    'p.Range.SetListLevel Level:=2
                                                    p.OutlineLevel = wdOutlineLevel3

                                                Else

                                                    'p.Range.SetListLevel Level:=3
                                                    p.OutlineLevel = 10
                                                End If
                                        'Aplicamos lista multinivel de números
                                        p.Range.ListFormat.ApplyListTemplate ListTemplate:=ListGalleries(wdOutlineNumberGallery).ListTemplates(2), _
                                        ContinuePreviousList:=True, ApplyTo:=wdListApplyToWholeList, DefaultListBehavior:=wdWord10ListBehavior

                                        'Establecemos el numero del tema

                                        p.Range.ListFormat.ListTemplate.ListLevels(1).StartAt = lvl

                                        'Si el nivel de esquema es 3, aplicamos lista nivel 2
                                        If p.OutlineLevel = 3 Then
                                                    p.Range.Font.Name = "Calibri"
                                                    p.Range.Font.Size = 14
                                                    p.Range.Font.Bold = True
                                                    p.Range.Font.Italic = False
                                                    '-----SOLO PARA TEST p.Range.Font.ColorIndex = wdBrightGreen
                                                    p.Range.SetListLevel Level:=2



                                        'Si el nivel de esquema es 4, aplicamos lista nivel 3
                                        Else
                                            If p.OutlineLevel = 10 Then
                                                        p.Range.Font.Name = "Calibri"
                                                        p.Range.Font.Size = 12
                                                        p.Range.Font.Bold = False
                                                        p.Range.Font.Italic = False
                                                        '-----SOLO PARA TEST p.Range.Font.ColorIndex = wdBlue
                                                        p.Range.SetListLevel Level:=3

                                            End If
                                        End If

                                End If
                            'End If
    Siguiente:
                Next iParaCounter


    End If
    End Sub

有了这个,我阅读了第一段带有数字格式的文档,并将其用作列表编号。现在我在想,我怎么能在一个有很多级别的文档中控制它呢?

【讨论】:

    猜你喜欢
    • 2013-11-07
    • 1970-01-01
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 2012-05-06
    • 2019-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多