Range question

J

Jay

I want to add a new paragraph to the end of the document, then change the font size of the preceding
paragraph, then add a table after the new last paragraph.

I've done this before using the selection object, but would like instead to use the range object.

How do I 'locate' the last paragraph?

Very bad pseudo-code:

MyDoc.Range(end, end) = Add_New_Paragraph
MyDoc.Range(end-1, end-1).Font.Size = 1
MyDoc.Range.Tables.Add Range(end, end), NumColumns:=?, NumRows:=?
 
J

Jonathan West

Jay said:
I want to add a new paragraph to the end of the document, then change the
font size of the preceding
paragraph, then add a table after the new last paragraph.

I've done this before using the selection object, but would like instead
to use the range object.

How do I 'locate' the last paragraph?

Very bad pseudo-code:

MyDoc.Range(end, end) = Add_New_Paragraph
MyDoc.Range(end-1, end-1).Font.Size = 1
MyDoc.Range.Tables.Add Range(end, end), NumColumns:=?, NumRows:=?


Try this

ActiveDocument.Range.InsertParagraphAfter
ActiveDocument.Paragraphs.Last.Previous.Range.Font.Shrink
ActiveDocument.Tables.Add _
Range:=ActiveDocument.Range(ActiveDocument.Range.End - 1, _
ActiveDocument.Range.End - 1), NumRows:=2, NumColumns:=2


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
T

Tony Jollans

You can't do it - with the Selection or without. You can't add a table after
the last paragraph - you can only add it *in* the last paragraph.

Given that, this is one way to do it:

With ActiveDocument.Paragraphs(ActiveDocument.Paragraphs.Count)
.Range.Paragraphs.Add
.Previous.Range.Font.Size = 1
.Range.Tables.Add .Range, 2, 2
End With
 
G

Greg

Tony,

To squeeze out the "last" drop of economy, perhaps:)

Sub Test()
With ActiveDocument.Paragraphs.Last
.Range.Paragraphs.Add
.Previous.Range.Font.Size = 6
.Range.Tables.Add .Range, 2, 2
End With
End Sub
 
T

Tony Jollans

Yes, thanks, Greg, I always forget Last!

What I find interesting is that the object of the With is dynamic.
Conventional wisdom has it that using a With avoids the overhead of
repeatedly evaluating the object, but here we see that this is not always
the case.

To see the difference with different objects, make sure you have some text
in the last paragraph of a document and run this:

Sub dynamo()
Dim r As Paragraph
Set r = ActiveDocument.Paragraphs.Last
With r
MsgBox .Range.Text
.Range.InsertParagraphAfter
MsgBox .Range.Text
End With
End Sub

Then undo the changes (or otherwise again make sure the last paragraph
contains text) and run this one:

Sub stasis()
Dim r As Paragraph
Set r = ActiveDocument.Paragraphs.Last
With r.Range
MsgBox .Text
.InsertParagraphAfter
MsgBox .Text
End With
End Sub
 
J

Jay

Thanks everyone for your suggestions.

Good point Tony . You are right, I want to add the table in the last paragraph, not after it.

The reason I'm shrinking the font (to size 1) is because it follows a preceding table, and I want
the paragraph mark to remain on the same page as the table.

Best wishes,

Jay


"Tony Jollans" <My Forename at My Surname dot com> wrote in message
You can't do it - with the Selection or without. You can't add a table after
the last paragraph - you can only add it *in* the last paragraph.

Given that, this is one way to do it:

With ActiveDocument.Paragraphs(ActiveDocument.Paragraphs.Count)
.Range.Paragraphs.Add
.Previous.Range.Font.Size = 1
.Range.Tables.Add .Range, 2, 2
End With
 
J

Jay

Hi Tony,

I've tried what you suggested, and spotted the difference. I'm not sure what the implications are
though. Are you saying that dynamo is slower, so is best avoided for applications that need to run
fast?

Thanks,

Jay

"Tony Jollans" <My Forename at My Surname dot com> wrote in message
Yes, thanks, Greg, I always forget Last!

What I find interesting is that the object of the With is dynamic.
Conventional wisdom has it that using a With avoids the overhead of
repeatedly evaluating the object, but here we see that this is not always
the case.

To see the difference with different objects, make sure you have some text
in the last paragraph of a document and run this:

Sub dynamo()
Dim r As Paragraph
Set r = ActiveDocument.Paragraphs.Last
With r
MsgBox .Range.Text
.Range.InsertParagraphAfter
MsgBox .Range.Text
End With
End Sub

Then undo the changes (or otherwise again make sure the last paragraph
contains text) and run this one:

Sub stasis()
Dim r As Paragraph
Set r = ActiveDocument.Paragraphs.Last
With r.Range
MsgBox .Text
.InsertParagraphAfter
MsgBox .Text
End With
End Sub
 
T

Tony Jollans

I am wrong. There is an obvious difference between the two pieces of code,
of course, but the reason is not a dynamic With.

The reason for the difference is that, in just the same way as I said that
you can't add a table after the last paragraph, you can't add a paragraph
after it either. Trying to add a paragraph after the last one, actually adds
it before.

In effect, a Paragraph object is represented by the paragraph mark in the
document - and that is different from the range which is associated with the
paragraph. When adding a paragraph 'after' the last one, the Range which was
associated with the (original) last paragraph becomes associated with the
(new) next-to-last one, and a new empty range is associated with the (new)
last paragraph.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top