Reputation: 23187
var p1 = document.Paragraphs.Add(ref o);
p1.Range.InsertParagraphAfter();
Now I want to grab the paragraph that was just created using InsertParagraphAfter() and modify it. How can I access it?
Upvotes: 2
Views: 5768
Reputation:
I know this is way old but could not resist.
Here is the working solution (rng is one paragraph's range):
rng.InsertParagraphAfter()
If rng.Paragraphs(1).Next IsNot Nothing Then
rng.Paragraphs(1).Next.Style = ActiveDocument.Styles(WdBuiltinStyle.wdStyleNormal)
End If
Upvotes: 0
Reputation: 2480
You can accomplish this by adding a new paragraph relatively to the first paragraph:
Paragraph p1 = document.Paragraphs.Add(System.Reflection.Missing.Value);
p1.Range.Text = "Foo";
p1.Range.InsertParagraphAfter();
// Add new paragraph relative to first paragraph
Paragraph p2 = document.Paragraphs.Add(p1.Range);
p2.Range.Text = "Bar";
p2.Range.InsertParagraphAfter();
// Add new paragraph relative to the second paragraph
Paragraph p3 = document.Paragraphs.Add(p2.Range);
p3.Range.Text = "Baz";
Upvotes: 0
Reputation: 14956
InsertParagraphAfter
is supposed to extend the current selection to include the new paragraph. So if you start by creating an empty selection at the end of the existing paragraph, the current selection should be set to the new paragraph after calling InsertParagraphAfter
.
Note that I have not tested the following code (I have not even tried compiling it), so I may be way off.
var p1 = document.Paragraphs.Add(ref o);
// Set the selection to the end of the paragraph.
document.Range(p1.Range.End, p1.Range.End).Select();
p1.Range.InsertParagraphAfter();
// InsertParagraphAfter should expand the active selection to include
// the newly inserted paragraph.
var newParagraph = document.Application.Selection;
Upvotes: 3