Open Word Documents to Specific Page

  • Thread starter Thread starter mikeg
  • Start date Start date
M

mikeg

Using /t switch we can open specified documents from the command line. But is
there a way to open at a specific page number? Not the last page edited or a
fixed page number, but rather the ability to specify a page on the command
line. Any suggestions are appreciated.
 
Using /t switch we can open specified documents from the command line. But is
there a way to open at a specific page number? Not the last page edited or a
fixed page number, but rather the ability to specify a page on the command
line. Any suggestions are appreciated.

Not directly.

You can put a bookmark on the page you want to open to. Then write a macro to
select the bookmark when the document opens (see
http://word.mvps.org/faqs/macrosvba/DocumentEvents.htm).

It would be possible -- but not advisable -- to write an AutoOpen or
Document_Open macro in the document itself, like this (assuming the bookmark is
named OpenMe):

Sub AutoOpen()
If ActiveDocument.Bookmarks.Exists("OpenMe") Then
ActiveDocument.Bookmarks("OpenMe").Range.Select
End If
End Sub

The problem with this is that Word always suspects a macro in a document, as
opposed to one in a template, to be a virus. Depending on your security
settings, Word will silently disable the macro or display a dialog asking
whether to disable or enable the macro. To avoid this, you can digitally sign
the macro code.

A better idea, if the document in question is only for your use and won't be
distributed to other users, would be to put the macro into Normal.dot. There are
several ways to arrange that only certain documents get the treatment.

- Choose a bookmark name, maybe some randomly generated nonsense, for the
bookmark name to ensure that only the desired documents will ever contain that
specific bookmark. Then the code above will be OK; if the current document
doesn't contain a bookmark with the odd name, the macro won't do anything.

- Enhance the macro to look at ActiveDocument.Name, and simply exit if it isn't
one of the desired documents.

- Change the name of the macro to something that isn't automatically executed by
Word. Then use the /m switch on the command line to open a desired document and
run the macro.
 
Not directly.

You can put a bookmark on the page you want to open to. Then write a macro to
select the bookmark when the document opens (see
http://word.mvps.org/faqs/macrosvba/DocumentEvents.htm).

It would be possible -- but not advisable -- to write an AutoOpen or
Document_Open macro in the document itself, like this (assuming the bookmark is
named OpenMe):

Sub AutoOpen()
If ActiveDocument.Bookmarks.Exists("OpenMe") Then
ActiveDocument.Bookmarks("OpenMe").Range.Select
End If
End Sub

The problem with this is that Word always suspects a macro in a document, as
opposed to one in a template, to be a virus. Depending on your security
settings, Word will silently disable the macro or display a dialog asking
whether to disable or enable the macro. To avoid this, you can digitally sign
the macro code.

A better idea, if the document in question is only for your use and won't be
distributed to other users, would be to put the macro into Normal.dot. There are
several ways to arrange that only certain documents get the treatment.

- Choose a bookmark name, maybe some randomly generated nonsense, for the
bookmark name to ensure that only the desired documents will ever contain that
specific bookmark. Then the code above will be OK; if the current document
doesn't contain a bookmark with the odd name, the macro won't do anything.

- Enhance the macro to look at ActiveDocument.Name, and simply exit if it isn't
one of the desired documents.

- Change the name of the macro to something that isn't automatically executed by
Word. Then use the /m switch on the command line to open a desired document and
run the macro.

By the way, the /t switch you mentioned isn't for "opening specified documents".
It's for starting Word with a new document based on the specified template. To
open a specified document when Word starts, you just put the document's
path/filename on the command line after winword.exe, without any switch
preceding the document name. Read http://support.microsoft.com/?kbid=210565 for
all the rules.
 
Jay, thanks for your very helpful information including walking me through
the security issues and command line switch correction.

The bookmark solution sounds great for opening a handful of bookmarks. In
this case, we have a book with 365 lessons saved to a WORD document. Setting
365 bookmarks and creating a macro to refer to each one sounds a bit daunting.

The idea is to use an alarm application such as Atomic Clock or Kirby Alarm
to open the appropriate lesson hourly each day. I might try setting 365
bookmarks for the benefit of others if the template macro is designed to
accept bookmark names from the command line.

Another thought... what about a macro that opens to specific text? Each
lesson starts with Step 1, Step 2, etc. If possible, that might be a little
easier to implement. I didn't give you much detail in the original post, but
what do you think in light of this additional info?
 
mikeg said:
Jay, thanks for your very helpful information including walking me
through the security issues and command line switch correction.

The bookmark solution sounds great for opening a handful of
bookmarks. In this case, we have a book with 365 lessons saved to a
WORD document. Setting 365 bookmarks and creating a macro to refer to
each one sounds a bit daunting.

The idea is to use an alarm application such as Atomic Clock or Kirby
Alarm to open the appropriate lesson hourly each day. I might try
setting 365 bookmarks for the benefit of others if the template macro
is designed to accept bookmark names from the command line.

Another thought... what about a macro that opens to specific text?
Each lesson starts with Step 1, Step 2, etc. If possible, that might
be a little easier to implement. I didn't give you much detail in the
original post, but what do you think in light of this additional info?

You might be able to work with the Step X idea, but I'd need more
information. For example, is the start of the lesson the _only_ place that
particular phrase appears in the document? You're sure there aren't any
places with text like "as you saw in Step 46"? Or, if there are such other
mentions, are the phrases at the start of each lesson formatted with a
specific style, so you could look for text that says "Step X" _and_ that
style to locate the unique spot?

Assuming you just want to open to the next step each time, you could adapt
the macro in http://www.word.mvps.org/FAQs/MacrosVBA/NumberDocs.htm. Instead
of inserting the next number into the document, the macro would combine the
word "Step" with the next number and search for that phrase in the document.

I'm not familiar with the alarm programs you mentioned, but Windows itself
has a task scheduler that you could probably use. In any case, you would
launch Word with the proper document and the /m switch to run the macro.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
Yes, I understand about the phrase "Step x" appearing in other places. In
this case it will not appear anywhere else except at the heading of each
lesson and is also in a distinctive font not found anywhere else. So it looks
like this could be a viable approach.

I'll look at adapting the macro you referenced, or look around a bit more or
write from scratch. I wrote a few macros several years ago including one that
formats and populates an Excel workbook from multiple SQL data sources. That
knowledge is a bit rusty now, but it'll come back when I can find a little
time...

Thanks again!
 
Back
Top