c#Word interop在Shape之后移动光标

时间:2014-05-27 12:38:44

标签: c# ms-word interop paragraph

我需要在形状对象后面添加一个新段落。在我下面的代码中,我找不到保存的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();

1 个答案:

答案 0 :(得分:0)

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

这可以这样做:

//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);