c#Word互操作在形状后移动光标

c#Word互操作在形状后移动光标,c#,ms-word,interop,paragraph,C#,Ms Word,Interop,Paragraph,我需要在shape对象之后添加一个新段落。在我下面的代码中,我在保存的Word文档中找不到形状,但只能得到段落 Microsoft.Office.Interop.Word._Application wordApplication; Microsoft.Office.Interop.Word._Document wordDocument; object missing = System.Type.Missing; wordApplication = new Microsoft.Office.Int

我需要在shape对象之后添加一个新段落。在我下面的代码中,我在保存的Word文档中找不到形状,但只能得到段落

Microsoft.Office.Interop.Word._Application wordApplication;
Microsoft.Office.Interop.Word._Document wordDocument;
object missing = System.Type.Missing;
wordApplication = new Microsoft.Office.Interop.Word.Application();
wordDocument = wordApplication.Documents.Add(string.Empty, ref missing, ref missing,  ref  missing);
object outputFile = Path.Combine("C:\\Users\\pttinu01", "Test1423.docx");

Microsoft.Office.Interop.Word.Shape oShape = null;
oShape = wordDocument.Shapes.AddTextbox (Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal, CentimetersToPoints(5.6),  CentimetersToPoints(1.5), CentimetersToPoints(18), CentimetersToPoints(1.45), ref missing);
oShape.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
oShape.TextFrame.TextRange.Font.Name = "Comic Sans MS";
oShape.TextFrame.TextRange.Font.Size = 18;
//oShape.TextFrame.TextRange.Font.Bold = (Int32)Microsoft.Office.Core.MsoTriState.msoTrue;
oShape.TextFrame.TextRange.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorGray50;
oShape.TextFrame.TextRange.Text = "Test Shape";

object Count = 5;
wordApplication.ActiveDocument.GoTo(WdGoToItem.wdGoToLine,WdGoToDirection.wdGoToNext, ref Count,ref missing);

var pText = wordDocument.Paragraphs.Add();
// pText.Format.SpaceAfter = 10f;
pText.Range.Text = String.Format("This is line #{0}", "test para");
wordDocument.SaveAs(ref outputFile , ref missing, ref missing ...);                         
wordDocument = null;
wordApplication.Quit(ref missing, ref missing, ref missing);
wordApplication = null;
GC.Collect();

如果您插入一个图像,然后想继续写入文档,您可能会想将光标设置到页面的末尾

可以这样做:

//Set the cursor to the end of document.
Object toWhat = WdGoToItem.wdGoToLine;
Object toWhich = WdGoToDirection.wdGoToLast;
wordApp.Selection.GoTo(toWhat, toWhich, ref missing, ref missing);