c# Word互操作将光标移动到形状之后
本文关键字:之后 移动 光标 Word 互操作 | 更新日期: 2023-09-27 18:05:26
我需要在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);