Rubén P S
Rubén P S

Reputation: 113

VBA WORD Labels at start at end of selection

I'm trying to select text and add some labels at the start/end, and do some stuff in each paragraph of the selection, but I don't know why happens this::

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

As you can see if you exe this macro, in the Debug.print line, the number of paragraphs decrease in 1 after exiting For Loop...WHY??

Upvotes: 0

Views: 598

Answers (2)

Arya
Arya

Reputation: 341

Try this working fine :)

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

Upvotes: 1

Rubén P S
Rubén P S

Reputation: 113

Ok solved with this:

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

Upvotes: 0

Related Questions