How to start a Word 2007 document at a bookmark?

M

Marty

I want to start a Word 2007 document from the command line, at a specific
part of the document. I assume I should use bookmarks and a VBA module, but
I'm not sure about the syntax.

Any help?

TIA,
Marty
 
J

Jean-Guy Marcil

Marty said:
I want to start a Word 2007 document from the command line, at a specific
part of the document. I assume I should use bookmarks and a VBA module, but
I'm not sure about the syntax.

In your Document_Open sub (In the ThisDocument module) use code like this:

Selection.GoTo What:=wdGoToBookmark, Name:="Test"

If the bookmark is a range of text and you do not want the whole range
selected, add this second line:

Selection.Collapse wdCollapseStart
 
M

Marty

That works perfectly, thanks!

Just one more question: How can I specify the bookmark name while calling
winword.exe from the command line? Should this work?

"c:\program files\...\winword.exe /f MyDoc.docm MyBkMrkName

Then use command() from within the document_open sub to set the Name param?
 
J

Jean-Guy Marcil

Marty said:
That works perfectly, thanks!

Just one more question: How can I specify the bookmark name while calling
winword.exe from the command line? Should this work?

"c:\program files\...\winword.exe /f MyDoc.docm MyBkMrkName

Then use command() from within the document_open sub to set the Name param?

From the Word VBA help:

"
Command Function

Returns the argument portion of the command line used to launch Microsoft
Visual Basic or an executable program developed with Visual Basic. The Visual
Basic Command function is not available in Microsoft Office applications.
"

So, no can do! :-(

But, why not use the Document_Open sub? It will execute as soon as you open
the document.
Why do you need to specify the bookmark name from the command line?
 
M

Marty

I am in fact using the document_open sub. I need to specify the bookmark
from the command line because I want to display the particular page of the
document (at the location of the bookmark) from another application. Can you
suggest another way to do that?
 
J

Jean-Guy Marcil

Marty said:
I am in fact using the document_open sub. I need to specify the bookmark
from the command line because I want to display the particular page of the
document (at the location of the bookmark) from another application. Can you
suggest another way to do that?

I still do not get it. Why can't you use the sub I suggested to specify the
name of the bookmark.
Why must this bookmark name be specified from another app? (Which other app?)
 
M

Marty

The sub you suggested has the bookmark "hard coded." The "other app" is an
application i am writing in another language that will open my Word document
to a specific page based on the value of a variable in my app.

For example, if that variable is an alphanumeric that has some computed
value, say "BookMark1", I want my application to open the Word document with
the text at bookmark1 displayed.
 
J

Jay Freedman

Hi Marty,

As this discussion has already covered, there is no mechanism for passing any
arbitrary string parameter into Word through its command line. However, there
are several other ways to get the information from one place to another.

- If the number of bookmarks in the document is limited and known, you could
write a separate macro for each bookmark, each one containing just a Select
statement for that bookmark and a call to your other code (which will have to be
moved from Document_Open); then use the /m switch on Word's command line to
execute the proper macro. Be aware, though, that this is likely to be a
maintenance nightmare.

- In your app, store the name of the bookmark in a registry entry. Then the Word
Document_Open macro can use the System.PrivateProfileString property to retrieve
the name and select the bookmark.

- Similar to the preceding one, store the bookmark name in a text file in a
known location. The macro can read the text file and then delete it.

There are probably some other possibilities, but these are the simplest.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 
M

Marty

Jay -

As ineligent as it may be, I think the last solution--to write a text file
from my app then read it into the VBA sup--is the best one.

The application I'm writing does not have the sophistication to write to the
registry, and i would rather not have to write yet a third application to do
that.

Thanks for the idea!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top