Pregunta

I'm trying to get to a specific point in my Word document from excel VBA. My ultimate goal is to copy new text into the bottom of the document.

So I put a bookmark at the bottom of my document and did this:

wDoc.Goto What:=wdGoToBookmark, Which:=wdGoToFirst, Name:="TemplatePage"

And it tells me "That bookmark does not exist." Which is funny, because it clearly does. It says the same thing about all my bookmarks.

This does not give me an error, instead it doesn't do anything:

wDoc.Goto What:=wdGoToBookmark, which:=wdgotofirst

Am I missing something obvious here?

¿Fue útil?

Solución

Am I missing something obvious here?

IMO you are missing something, but the use of the word "Goto" can create the wrong impression.

.Goto actually makes the object that you specify "goto" the specified location. So if that object is a range, you shouldn't expect the Selection to change.

The other thing is that you have to omit the "which" parameter in this case. I don't know how you could determine that from the documentation - it's a question of trial and error with some of these range-related things.

Since the range implied by wDoc or wDoc.Content won't change anyway, you will lose any useful information immediately by executing the .Goto as a command. If you use it as a function, you get a range, so you can use, e.g.

Dim r As Word.Range
Set r = wDoc.Goto(What:=wdGoToBookmark, Which:=wdGoToFirst)
Debug.print r.start, r.end

Or if you want to select the resulting range, you can use

r.Select

But if you want to set a range

Dim r As Word.Range
Set r = wDoc.Bookmarks("TemplatePage)

is (IMO) simpler and you already have the code that actually selects it if that is what you need.

Otros consejos

Ok, so apparently the GoTo simply doesn't work. I tried all sorts of variations on Page, Section, Bookmark, they either did nothing, gave errors, or even moved to the wrong place (selected text in the header!)

Instead, you can try:

wDoc.Bookmarks("TemplatePage").Select

Works like a champ.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top