VBA WORD标签在选择结束时开始标记

时间:2015-03-17 11:13:28

标签: vba ms-word selection labels

我试图选择文字并在开头/结尾添加一些标签,并在选择的每个段落中做一些事情,但我不知道为什么会发生这种情况::

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 ...为什么?

2 个答案:

答案 0 :(得分:1)

试试这个工作正常:)

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

答案 1 :(得分:0)

好的解决了这个:

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