VBA WORD 标签在选择的开始和结束

VBA WORD Labels at start at end of selection

我正在尝试 select 文本并在 start/end 处添加一些标签,并在 selection 的每个段落中做一些事情,但我不知道为什么会这样::

Sub TESTBotones()
    Dim p As Paragraph, i As Integer, total As Integer, r As Range
    Set r = Selection.Range
    total = Selection.Paragraphs.Count
    For i = 1 To total
        Set p = r.Paragraphs(i)
        p.Range.Text = "***" & p.Range.Text
    Next
    r.Text = "((BOTONES))" & Chr(13) & r.Text & Chr(13) & "((/BOTONES))" & Chr(13)
    Debug.Print r.Paragraphs.Count

End Sub

你可以看到如果你执行这个宏,在Debug.print行,退出For循环后段落数减少1...为什么??

好的,这样就解决了:

Sub Pestañas()
    Dim p As Paragraph, i As Integer, j As Integer, total As Integer, r As Range, ini As Long, fin As Long, menos As Integer
    Set r = Selection.Range
    ini = Selection.Start
    fin = Selection.End
    If ini = fin Then Exit Sub
    For i = 1 To total
       Set p = r.Paragraphs(i)
       p.Range.Text = "***" & p.Range.Text
    Next
    r.SetRange Start:=ini, End:=fin
    r.Text = "((THING))" & Chr(13) & r.Text & "((OTHER THING))" & Chr(13)



End Sub

试试这个效果很好:)

Sub TESTBotones()
    Dim p As Paragraph, i As Integer, total As Integer, r As Range
    Set r = Selection.Range
    total = Selection.Paragraphs.Count
    For i = 1 To total
        Set p = r.Paragraphs(i)
        p.Range.InsertBefore "***"
    Next
    r.InsertBefore "((BOTONES))" & Chr(13)
    r.InsertAfter Chr(13) & "((/BOTONES))" & Chr(13)
    Debug.Print r.Paragraphs.Count
End Sub