Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-10-2014, 12:02 AM
Sektor Sektor is offline Confusion about paragraphs Windows 8 Confusion about paragraphs Office 2013
Novice
Confusion about paragraphs
 
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
  #2  
Old 04-10-2014, 02:46 AM
macropod's Avatar
macropod macropod is offline Confusion about paragraphs Windows 7 32bit Confusion about paragraphs Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,726
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

The problem with your code is that 'p.Range.Text = "par3"' overwrites the whole of what used to be the final paragraph, including the break. Try 'p.Range.InsertAfter "par3"' to update the new final paragraph or 'p.Range.InsertBefore "par3"' to update the old one (note that this will not delete whatever's already in that paragraph).
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 04-10-2014, 03:30 AM
Sektor Sektor is offline Confusion about paragraphs Windows 8 Confusion about paragraphs Office 2013
Novice
Confusion about paragraphs
 
Join Date: Mar 2014
Location: Russia
Posts: 6
Sektor is on a distinguished road
Default

@macropod
Thanks for answer, but this Word's object model doesn't fit my mind. It's highly non-intuitive.
I add paragraph with Paragraphs.Add(). Then I have these two options to obtain reference to newly created paragraph: 1) with Set p = .... and 2) Paragraphs(Paragraphs.Count). But in reality they are two different objects! Why?
Reply With Quote
  #4  
Old 04-10-2014, 05:41 AM
macropod's Avatar
macropod macropod is offline Confusion about paragraphs Windows 7 32bit Confusion about paragraphs Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,726
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by Sektor View Post
Word's object model doesn't fit my mind. It's highly non-intuitive.
Word's object model is quite logical, actually. Your code defines a range [via Set p = .Paragraphs.Add()], which can't be outside the range defined by the document's final paragraph break (otherwise it wouldn't be part of the document), so it has to occur before that, then replaces whatever's in that range with something else [via p.Range.Text = "par3"]. You can confirm that's what happening be changing 'p.Range.Text = "par3"' to 'p.Range.Font.ColorIndex = wdRed'. I suspect the problem you're having is a conceptual one, rather than anything to do with Word's object model.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 04-10-2014, 06:08 AM
Sektor Sektor is offline Confusion about paragraphs Windows 8 Confusion about paragraphs Office 2013
Novice
Confusion about paragraphs
 
Join Date: Mar 2014
Location: Russia
Posts: 6
Sektor is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
I suspect the problem you're having is a conceptual one, rather than anything to do with Word's object model.
Yes, Paul, you're actually right. I come from Excel environment, and I have difficulty with switching to Word's realm. I felt this since I first tried to write VBA code in Word.
And yes - the main struggle is paragraphs and all manipulating with them. I would like you to help me with this.
So, when I execute Paragraphs.Add(), I see new paragraph mark which happens to be the last one in document. So, this is my paragraph. It has a Range.Text property which can be assigned some text. What I do not understand, why p.Range.Text deletes this brand new paragraph and, moreover, replaces the text of second paragraph?
Look. Paragraphs(Paragraphs.Count) refers to the same paragraph. Then why p.Range.Text fails to work when Paragraphs(3) works fine? Can you explain me more in detail? Thanks beforehand!
Reply With Quote
  #6  
Old 04-10-2014, 07:05 AM
macropod's Avatar
macropod macropod is offline Confusion about paragraphs Windows 7 32bit Confusion about paragraphs Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,726
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by Sektor View Post
So, when I execute Paragraphs.Add(), I see new paragraph mark which happens to be the last one in document.
That's where your conceptual difficulty is - since you're working with the end of the document your new paragraph is inserted before the final paragraph break. You can't insert one after the final paragraph break. Thus, the one you inserted isn't the last one, but the second-last. As I said in my last post:
Quote:
You can confirm that's what happening be changing 'p.Range.Text = "par3"' to 'p.Range.Font.ColorIndex = wdRed'.
When your alternate method of referencing the last paragraph via 'Set p = .Paragraphs(.Paragraphs.Count)' is invoked, that's after you had added the new paragraph (before the final one).

PS If you want to reference the last paragraph, you could also use 'Set p = .Paragraphs.Last'.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 04-10-2014, 10:29 PM
Sektor Sektor is offline Confusion about paragraphs Windows 8 Confusion about paragraphs Office 2013
Novice
Confusion about paragraphs
 
Join Date: Mar 2014
Location: Russia
Posts: 6
Sektor is on a distinguished road
Default

A picture is worth a thousand words.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Confusion over licences Nez Office 5 02-03-2014 03:29 AM
Confusion about paragraphs Word template confusion portia Word 8 11-29-2011 02:08 PM
Confusion about paragraphs Email confusion chris29 Outlook 1 08-24-2011 01:22 PM
Confusion about paragraphs Multiple referencing confusion ridge1988 Word 2 03-31-2011 07:42 PM
Windows Update confusion Quicksilver Office 0 06-24-2010 12:33 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:55 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2022, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2022 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft