【问题标题】:Excel vba insert before bookmark does not give expected word orderExcel vba 在书签之前插入没有给出预期的词序
【发布时间】:2016-05-03 16:12:39
【问题描述】:

我正在使用下面的代码打开一个新的 Word 文档并添加一个书签。我正在尝试在书签“MyBookmark”中插入多个单词以形成句子:“Once Upon a time...”

我希望通过使用 InsertBefore 将单词插入到书签之前,并且我可以在第一个单词之后添加下一个单词,因为书签最终位于单词的末尾。这不是发生的情况,而是在创建句子的句子开头添加了单词:“a time...on Once”

如何在句末添加单词?

我尝试过使用 InsertAfter,结果相同。我不想更改添加单词的顺序,因为这在更大范围内是不可行的,我想实现它。下面的代码是我希望在实际实现中实现的示例,我正在打开一个保存为 dotx 文件的模板。

Sub InsertBefore()
    ' Open Word document from template
    Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = True
    wrdApp.Documents.Add
    wrdApp.Activedocument.Bookmarks.Add Name:="MyBookmark"

    ' Insert text
    wrdApp.Activedocument.Bookmarks("MyBookmark").Range.InsertBefore "Once "
    wrdApp.Activedocument.Bookmarks("MyBookmark").Range.InsertBefore "upon "
    wrdApp.Activedocument.Bookmarks("MyBookmark").Range.InsertBefore "a time..."
End Sub

【问题讨论】:

    标签: vba excel bookmarks


    【解决方案1】:

    最简单的方法是使用Selection 对象。你先去那里,然后你就从那里开始打字:

    wrdApp.Activedocument.Bookmarks("MyBookmark").Range.Select
    
    'Then from there on you just use the Selection object
    
    wrdApp.Activedocument.ActiveWindow.Selection.TypeText("Once ")
    wrdApp.Activedocument.ActiveWindow.Selection.TypeText("upon ")
    wrdApp.Activedocument.ActiveWindow.Selection.TypeText("a time...")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-10
      • 2021-08-07
      • 2021-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-23
      • 1970-01-01
      相关资源
      最近更新 更多