Thread: [Solved] Confusion about paragraphs
View Single Post
 
Old 04-10-2014, 12:02 AM
Sektor Sektor is offline Windows 8 Office 2013
Novice
 
Join Date: Mar 2014
Location: Russia
Posts: 6
Sektor is on a distinguished road
Default Confusion about paragraphs

I have created new document and inserted two paragraphs. First paragraph holds text "par2" and second paragraph holds text "par2". Now I execute the following macro (comments explain behavior).

Code:
Sub TestParagraphs()

    Dim p As Paragraph
    
    With ActiveDocument
        
        ' Variable "p" seems to hold reference
        ' to new (third) paragraph....
        Set p = .Paragraphs.Add()
        ' ...or does it?
        p.Range.Text = "par3"
        
        ' What we get here is that third paragraph is gone
        ' AND second paragraph now holds "par3" text.
        
        ' This is work around, which works.
        .Paragraphs.Add
        Set p = .Paragraphs(.Paragraphs.Count)
        p.Range.Text = "par3"
        
    End With

End Sub
According to documentation "Paragraphs.Add returns a Paragraph object that represents a new, blank paragraph added to a document." What I missed?
Reply With Quote