WordMacroToViewDocument

M

mike

-- I am used to activating a macro in excel to perform functions using a
command button embeded in the spreadsheet. Can I use a Macro button a in Word
document?
For example, I open a 20 page document, I would like a macro button to bring
the user to page 15. Code? Or to print page 12.
Thanks for any assistance.
Michael
 
G

Graham Mayor

You can use macros attached to toolbar buttons
http://www.gmayor.com/installing_macro.htm or macrobutton fields
http://www.gmayor.com/Macrobutton.htm .

For the first macro

Sub SelectAPage()
Dim sPage As String
Dim nPages As Long
nPages = Selection.Information(wdNumberOfPagesInDocument)
Start:
sPage = InputBox("Go to which page number?", "Page?", 15)
If Not IsNumeric(sPage) Then
MsgBox "You must enter a number!", vbOKOnly, "Error"
GoTo Start
End If
If sPage > nPages Then
MsgBox "There are only " & nPages & " pages in the document!", _
vbOKOnly, "Error"
GoTo Start
End If
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, name:=sPage
End Sub

The print macro would be very similar

Sub PrintAPage()
Dim sPage As String
Dim nPages As Long
nPages = Selection.Information(wdNumberOfPagesInDocument)
Start:
sPage = InputBox("Print which page number?", "Page?", 15)
If Not IsNumeric(sPage) Then
MsgBox "You must enter a number!", vbOKOnly, "Error"
GoTo Start
End If
If sPage > nPages Then
MsgBox "There are only " & nPages & " pages in the document!", _
vbOKOnly, "Error"
GoTo Start
End If
ActiveDocument.PrintOut Range:=wdPrintRangeOfPages, Pages:=sPage,
Copies:=1
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
M

mike

Graham
Thanks for responding. I would need the Input Box
I was planning a 3 column Word document. A titled document in column 3.
I wanted to place a button in column 1 to view the document listed in column 3
listed on the same line.
I also wanted to place a macro button in the 2 column, when activated
this would print automatically the document the column 3 listed on the same
line.

Can I also have a free standing Macro button, specifically placed on each of
the 1 and 2 columns to view or print the document on the same line listed
on column 3?

Any suggestions?
 
G

Graham Mayor

I am having difficulty visualising this document, but I think a three column
table rather than columns would be the way forward. The following macro will
toggle the display of the content of the first cell in column three of the
table

Dim oTable As Table
Dim oRng As Range
Set oTable = ActiveDocument.Tables(1)
ActiveWindow.View.ShowHiddenText = False
With oTable.Rows(1)
Set oRng = oTable.Columns(3).Cells(1).Range
With oRng
.Font.Hidden = Not .Font.Hidden
End With
End With

If you then want to print the same text from column 3

Dim oTable As Table
Dim oRng As Range
Dim RngText As String
Dim TempDoc As Document
Set oTable = ActiveDocument.Tables(1)
With oTable.Rows(1)
Set oRng = oTable.Columns(3).Cells(1).Range
RngText = Left(oRng, Len(oRng) - 1)
Set TempDoc = Documents.Add
Selection.TypeText RngText
TempDoc.PrintOut
TempDoc.Close savechanges:=wdDoNotSaveChanges
End With

You can activate the macros from macrobutton fields or from a custom toolbar
which would be my preference

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
M

mike

Graham
Thanks for responding, I am almost there
The document is a table with 3 columns.
The third column lists a document title " red"
The red document is on page 25.
I would like the viewer to view the document name listed "red" on page
25 by clicking on the View this document button listed in column 1.
OR, the viewer could print out the document "red" by clicking on the
Print this document button located in column 2
Looks like this, wher "View document" is listed on each button
View document print document "Red"
View document print document "green"

Additonally, would I add your code to the a macro for
each button on each line by clicking on the button, viewing code selection
and then copying the code into the macro. If I did this would I then
be able to activate the button by clicking on it?
thanks for trying to understand what I am trying to do.
 
G

Graham Mayor

That's not how I imagined it.

If you have a document on page 25 then you only need a simple macro to view
that run from your macro button

Sub RedView()
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, name:="25"
End Sub

You'll probably need a similar macro attached to a toolbar button to go back
to page 1, which I assume is where your table is?

Similarly the following will print page 25

Sub PrintRed()
ActiveDocument.PrintOut Range:=wdPrintRangeOfPages, _
Pages:="25", _
Copies:=1
End Sub

Why do I have the feeling that this all has something to do with a web page
.... in which case none of this will work?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
M

mike

Graham
thanks for your help but after viewing my document of at least 40 pages, I
have decided to change format, if you can help I would appreciate it very
much.
Page one or index page would have table with 3 columns.
1 st colum, I would like to have a drop down table listing the names of each
document
in shown on a specific num ered page. The operator would scroll though the
drop down table and pick out
the document name that he or she wants. The second column would show a macro
button to view this specific document. The third colum would show a macro
button to print the specific document.

thanks again for your help.
 
G

Graham Mayor

Create a pair of macros for each document similar to those in my last post
and put in the appropriate page numbers.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
M

mike

Graham
thanks for responding but I am still uncertain.
1. how can I make a dropdown list appear in Column 1 of the 3 column table.
When the user clicks on the dropdown list he or she can pick from a list of
40 document names, eg red, blue,green.
In Excell, I would use data-validation-list but obviously it is different in
Word.
2. the macro in column 2 to view selected document in column 1, would it not
need an "If " statment component, eg "if column 1 = "red" then view page 25
if column 1 = "green" then view page 26"

thanks again for your help!
 
M

mike

Graham
Since I am limited to 25 entries in drop down field, this format will not work
for me. I have returned to the 3 column table. Name of document ,red,in
first column,
a control button named "View "red"document" in second column and a control
button named "print "red" document" in third column. The red document in on
page 4. I copied your sub into the code for the view red document

Private Sub ViewRedDocument_Click()
Sub RedView()
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="4"
End Sub
--
when I double click on the control button, I only see the code that I
entered, instead
of viewing page 4. I know I am doing something in error.

thanks
Michael
 
G

Graham Mayor

If you are using control buttons (which are really intended for web page
creation) then you are still in design mode (first button on control toolbox
toolbar).

You also have two title lines. It should be:

Private Sub ViewRedDocument_Click()
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="4"
End Sub

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
M

mike

Graham
I copied the code into the macro, exited the design mode.
I clicked and double clicked on the button -"view document" which
was on page 4, but nothing happened.
Any ideas, I was using Office 2000. In Excel, a right click on thw button
would
give an option to assign a macro to the button but I can not find this
option in Word.
 
G

Graham Mayor

I don't have Word 2000 to check, but it works in Word 2003 as described.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
M

mike

Graham
I was making a slight error in following your directions. Finally corrected
this
error and my document working great!!
Thanks for hanging in there and answering my questions.
 
G

Graham Mayor

You are welcome :)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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