[英]Excel vba insert before bookmark does not give expected word order
I'm using the code below to open a new Word document and add a bookmark. 我正在使用下面的代码打开一个新的Word文档并添加书签。 I'm trying to insert multiple words a the bookmark 'MyBookmark' to form the sentence: "Once upon a time..."
我正在尝试在书签“ MyBookmark”中插入多个词以形成以下句子:“从前……”
I was expecting that by using InsertBefore the word would be inserted before the bookmark and I could add the next word after the first one, since the bookmark ends up at the end of the word. 我期望通过使用InsertBefore将单词插入到书签之前,并且我可以在第一个单词之后添加下一个单词,因为书签最终位于单词的末尾。 This is not what happens, instead the word gets added at the start of the sentence creating the sentence: "a time...upon Once"
不会发生这种情况,而是在句子的开头添加单词以创建句子:“一次...一次”
How can I add words at the end of the sentence? 如何在句子末尾添加单词?
I've tried using InsertAfter, which had the same result. 我试过使用InsertAfter,具有相同的结果。 I don't want to change the order in which I add the words as this is not feasible on the larger scale I would like to implement this.
我不想更改添加单词的顺序,因为在较大的范围内这是不可行的,我想实现此功能。 The code below is an example of what I'd like to achieve in the actual implementation I'm opening a template saved as a dotx file.
下面的代码是我要在实际实现中实现的一个示例,我正在打开一个保存为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
The easiest approach is to use the Selection
object. 最简单的方法是使用
Selection
对象。 You go there first and then you just start typing from there: 您先去那里,然后从那里开始输入:
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...")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.