C# 修改使用InsertParagraphAfter()添加的段落

C# 修改使用InsertParagraphAfter()添加的段落,c#,ms-word,C#,Ms Word,现在,我想获取刚刚使用InsertParagraphAfter()创建的段落,并对其进行修改。如何访问它?插入段落后应该扩展当前选择以包括新段落。因此,如果您首先在现有段落的末尾创建一个空选择,那么在调用InsertParagraphAfter后,当前选择应设置为新段落 请注意,我还没有测试以下代码(我甚至还没有尝试编译它),所以我可能离测试还有一段距离 var p1 = document.Paragraphs.Add(ref o); p1.Range.InsertParagraphAfter(

现在,我想获取刚刚使用InsertParagraphAfter()创建的段落,并对其进行修改。如何访问它?

插入段落后
应该扩展当前选择以包括新段落。因此,如果您首先在现有段落的末尾创建一个空选择,那么在调用
InsertParagraphAfter
后,当前选择应设置为新段落

请注意,我还没有测试以下代码(我甚至还没有尝试编译它),所以我可能离测试还有一段距离

var p1 = document.Paragraphs.Add(ref o);
p1.Range.InsertParagraphAfter();

您可以通过在第一段的基础上添加新段落来完成此操作:

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;

不幸的是,这不起作用:(也许你可以帮我。我这样做的原因是将两个表分开,这样它们就不会被word自动合并,但我希望这些表直接堆叠在另一个表上。通过执行InsertParagraphAfter(),两个表之间有一个整行分隔符,我想修改新的段落,使两个表正好在一起。有没有其他方法来完成我要寻找的内容?@sooprise:在录制宏时,尝试在Word中执行您想执行的操作,然后在VBA中查看代码。也许这可以给您一个提示如何做到?尝试了这个,不幸的是没有得到我想要的结果。@sooprise:你能在Word中发布一些你已经拥有的代码,也许还有一个你想要的截图来帮助我更好地理解你想要做什么吗?根据MSDN,当你给段落打电话时。添加它会创建段落在范围之前。示例的结果应该类似于:BazBarFoo如果您想要类似于:Foo Bar Baz的内容,您应该使用以下内容:
段落p1=document.parations.Add(System.Reflection.Missing.Value);p1.range.Text=“Foo”;段落p2=p1.range.parations.Add(System.Reflection.Missing.Value);p2.Range.Text=“Bar”;段落p3=p2.Range.parations.Add(System.Reflection.Missing.Value);p3.Range.Text=“Baz”
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";