如何在特定位置的Word文档中添加文本?
如何写入Word文档中的特定位置,例如第5行,第50个字符? 我搜索了几个小时,但找不到解决方案。
我正在使用Microsoft.Office.Interop.Word
如果你满足于更简单的句子,而不是行:
ActiveDocument.Sentences(1).Characters(5).Select Selection.Collapse Selection.InsertBefore "added "
VBA中有五个段落和50个空格
Selection.Text = String(5, vbCrLf) Selection.Collapse wdCollapseEnd Selection.Text = String(50, " ")
但是,对于特定的位置,我更喜欢文本框:
Set sh = doc.Shapes.AddTextbox(1, 10, 344, 575, 80) sh.Name = "Course1"
有一些属性:
sh.Fill.Visible = False sh.Line.Visible = False sh.TextFrame.MarginLeft = 0# sh.TextFrame.MarginRight = 0# sh.TextFrame.MarginTop = 0# sh.TextFrame.MarginBottom = 0#
如果每次执行此操作的简单方法是在同一位置插入文本,则创建一个带有书签的.dotx模板文件。 确保模板包含在构建中
Doc = Word.Documents.Add("Directory\Filename") Doc.Bookmarks.Item("BookmarkName").Range.Text = "Text to be inserted"
在word文档中查找位置以插入表格
您可以在上述位置找到一些有用的信息。