Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using WORD Automation

Status
Not open for further replies.

TekRite

Technical User
Sep 25, 2005
17
US
Below is the code that I am using to CREATE an instance of WORD, if none exists…then OPEN the specified document…then GoTo the specified page.

The code is activated from the ‘click’ event of a Command Button that is inserted into a Visio drawing.

The purpose of the Command Button is to allow the User to immediately view the Step-by-Step procedures associated with the Work Flows on the respective drawing.

The code for the Command Button <cmdImpResTer> works great…when activated it takes the User to the specified WORD document…and OPENS that document up at PAGE 37.

The problem that I have is that I have approximately 60 drawings that each need to have an associated Command Button that will perform exactly the same…with the only difference being possibly a different DOCUMENT and different PAGE number.

Each time I try to use the code with another Command Button…see <cmdNotRevTer>…it OPENS the document just fine at Page 1…but I cannot get the GoToPage to activate like it does in the code I created for <cmdImpResTer>.

When I StepInto the successful code…wdGoToPage =1

When I StepInto any succession of the same code for another button…wdGoToPage = Empty

Here is my successful code:

Private Sub cmdImpResTer_Click()

Set Wd = Nothing

Set Wd = CreateObject(“Word.Application”)
Wd.Visible = True

For Each doc In Wd.Documents
If doc.Name = “OSO1000_TtMngPro.doc” Then Found = True
Next doc
If Found <> True Then
Wd.Documents.Open “C:\Documentation\OSO1000_TtMngPro.doc”
Wd.Selection.GoTo What:=wdGoToPage, Count:=”37”
Else
Wd.Selection.GoTo What:=wdGoToPage, Count:=”37”
End If

End Sub

Here is my unsuccessful code (only Opens the document…but does not GoTo specified Page):

Private Sub cmdNotRevTer_Click()

Set Wd = Nothing

Set Wd = CreateObject(“Word.Application”)
Wd.Visible = True

For Each doc In Wd.Documents
If doc.Name = “OSO1000_TtMngPro.doc” Then Found = True
Next doc
If Found <> True Then
Wd.Documents.Open “C:\Documentation\OSO1000_TtMngPro.doc”
Wd.Selection.GoTo What:=wdGoToPage, Count:=”35”
Else
Wd.Selection.GoTo What:=wdGoToPage, Count:=”35”
End If

End Sub
 
Tip: use Option Explicit

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
PH,

I used the 'Dim wd' to force explicit declaration of the object variable...but it did not help...I still have my 1st created cmdButton<cmdImpResTer>...that works great...

but I cannot get the GoToPage to work again for another button...

Here is the code with the Option Explicit change...it still only OPENS the doc at Page1...

Private Sub cmdNotRevTer_Click()
'
'Notification Review Procedures - page/section locator for OSO001001_TtMngPro
''Make sure that MS Word 9.0 Object(or higher) is available by selecting <tools> then <references> and then selecting the checkbox next to MS Word 11.0 Object
'
Dim Wd
'
Set Wd = CreateObject("Word.Application")
Wd.Visible = True
'
For Each doc In Wd.Documents
If doc.Name = "OSO1001_TtMngPro.doc" Then Found = True
Next doc
If Found <> True Then
Wd.Documents.Open "C:\My Documents\WORD documents\OSO1001_TtMngPro.doc"
Wd.Selection.GoTo What:=wdGoToPage, Count:="35"
Else
Wd.Selection.GoTo What:=wdGoToPage, Count:="35"
End If
'
End Sub

Any other suggestions?...This code is kicking my butt...please help...Thank you!!!

Garry
 
Garry,

This probably has nothing to do with your question, but if "OSO1001_TtMngPro.doc" is already open when the macro runs, along with other word documents, how does Word know which document to Goto page 35 in?

Best Regards,
Walter
 
Hi Walter,

That is an excellent question. I thought that the 'IfFound' statement would handle that...but I am not a VBA programmer...I just dabble a bit.

I am actually a Technical Writer who discovered that my drawings would benefit greatly by adding ActiveX Controls running small macros...that would allow me to go to specific pages...within procedural documents that may be 50-80 pages long...with a single click of a button.

I am trying to keep this code as simple as possible...while still fulfilling my requirements.

What I don't understand is...the code for the initial Command Button works exactly like I need it to...but when I try to duplicate that code for additional buttons...it fails at the wdgoToPage line...

Instead of wdGoToPage = 1...which is what I get with the initial cmd button...I get wdGotoPage = Empty.

Using Option Explicit...which is what 'PH' suggested...did not resolve my issue.

Any suggestions you may have...would be muchly appreciated!

Thank you in advance...

Garry
 
Gary,

Not sure if I have the answer, but I hope this is useful. I tried your code, and it worked on my machine (Win 2000/Office 2000). However, if two word documents were open, it didn't show me the one I wanted to see. I found that I had to do something like the following to see the document that I wanted.
Code:
        Wd.Documents("Word1.doc").Activate
        Wd.Activate

This may be incidental, but I changed the code so as not to create a new instance of Word if one is already available. In the end, it looked like this

Code:
Option Explicit

Sub OpenWord1()

Dim Wd As Object

Set Wd = Nothing
'

'Get Reference to open Word instance, if any
On Error Resume Next
Set Wd = GetObject(class:="Word.Application")
On Error GoTo 0

'Create new Word instance, if necessary
If Wd Is Nothing Then _
    Set Wd = CreateObject("Word.Application")

Wd.Visible = True
'
Dim doc As Document
Dim Found As Boolean
For Each doc In Wd.Documents
       If doc.Name = "Word1.doc" Then Found = True
Next doc

If Found <> True Then
        Dim sPath As String
        sPath = ActiveWorkbook.Path & "\"
       Wd.Documents.Open sPath & "Word1.doc"
       Wd.Documents("Word1.doc").Goto What:=wdGoToPage, Count:="5"
Else

        Wd.Documents("Word1.doc").Goto What:=wdGoToPage, Count:="5"
        '**********************************************
        'Show me "Word1.doc", not the other document! 
        '**********************************************
        Wd.Documents("Word1.doc").Activate  
        Wd.Activate
End If

End Sub


Best Regards,
Walter
 
Hi Walter,

Thank you so much for this...I tried the code...but I am getting a 'Compile Error' "Variable not defined" for:

ActiveWorkbook

With my changes to your code...here is what it looks like so far:

Option Explicit

Sub OpenWord1()

Dim Wd As Object

Set Wd = Nothing
'

'Get Reference to open Word instance, if any
On Error Resume Next
Set Wd = GetObject(class:="Word.Application")
On Error GoTo 0

'Create new Word instance, if necessary
If Wd Is Nothing Then _
Set Wd = CreateObject("Word.Application")

Wd.Visible = True
'
Dim doc As Document
Dim Found As Boolean
For Each doc In Wd.Documents
If doc.Name = "OSO1001_TtMngPro.doc" Then Found = True
Next doc

If Found <> True Then
Dim sPath As String
sPath = ActiveWorkbook.Path & "C:\My Documents\WORD documents\OSO1001_TtMngPro.doc"
Wd.Documents.Open sPath & "OSO1001_TtMngPro.doc"
Wd.Documents("OSO1001_TtMngPro.doc").Goto What:=wdGoToPage, Count:="35"
Else

Wd.Documents("OSO1001_TtMngPro.doc").Goto What:=wdGoToPage, Count:="35"
'**********************************************
'Show me "Word1.doc", not the other document!
'**********************************************
Wd.Documents("OSO1001_TtMngPro.doc").Activate
Wd.Activate
End If

End Sub

Did I edit this improperly? How do I fix the error? I tried 'Dim ActiveWorkbook As String'...but then I got a different error message...

Garry
 
Garry,

My apologies. I did this in Excel, like most of my work, and that is why "ActiveWorkbook" appears.

Please try again after removing lines with "ActiveWorkbook", and writing the open statement as in your original:


Code:
Wd.Documents.Open "C:\My Documents\WORD documents\OSO1001_TtMngPro.doc"

W
 
Garry,

I think that this version is "Excel Free". Sorry for the previous version.

Code:
Option Explicit

Sub OpenWord1()

Dim Wd As Object

Set Wd = Nothing
'

'Get Reference to open Word instance, if any
On Error Resume Next
Set Wd = GetObject(class:="Word.Application")
On Error GoTo 0

'Create new Word instance, if necessary
If Wd Is Nothing Then _
    Set Wd = CreateObject("Word.Application")

Wd.Visible = True
'
Dim doc As Document
Dim Found As Boolean
For Each doc In Wd.Documents
       If doc.Name = "Word1.doc" Then Found = True
Next doc

If Found <> True Then
       Wd.Documents.Open "H:\Programs\VB_Misc\CreateWord\Word1.doc"
       Wd.Documents("Word1.doc").Goto What:=wdGoToPage, Count:="5"
Else

        Wd.Documents("Word1.doc").Goto What:=wdGoToPage, Count:="5"
        Wd.Documents("Word1.doc").Activate
        Wd.Activate
End If

End Sub
 
Walter,

Once again...Thank you so much...

For some reason I am now getting the Error Message 'Compile Error' "Variable not defined" and it is referencing:

'wdGoToPage' as the varibale not defined in the GoTo Method.

I don't understand why it is trying to read the item as a variable. I even tried to define it as an Object...a String...a Variant...but when I do...I get a 'Type Mismatch'

I copy/pasted your code into my module...so I know it isn't a typo error...but I don't get it...

Any suggestions for this?

Garry
 
Garry,

It looks like I am not much help!

Have you added a reference to Microsoft Word to your macro? If not, I believe you can clear up the "undefineed variable" as follows. In the Visual Basic Editor, please go to the menu

Tools -> References

and check the box next to "Microsoft Word x Object Library", where x is a version number, 9.0 in my case.


Now trying this out from Visio, and it is not working as well as it did in Excel. Please let me try a little more...
 
Actually Walter...I think you have been extremely helpful!

You have added the little extras that will keep many future errors out and keep this small program from crashing...and I appreciate that!

I have already referenced the Object library (11.0 in my case) and have included reference to it as a note in the module:

Private Sub cmdNotRevTer_Click()
'
'Notification Review Procedures - page/section locator for OSO001001_TtMngPro
'
'Make sure that MS Word 9.0 Object(or higher) is available by selecting <tools>
''then <references> and then selecting the checkbox next to MS Word 9.0, 10.0 or 11.0 Object
'

since wdGoToPage is a wdGoToItem constant...I do not understand why it is coming up as an undefined variable...

but I do believe that once this little problem can be fixed...that this code will perform the way I need it to...

Any help or suggestions you have to offer are more than I am able to come up with on my own...and I thank you for that Walter.
 
Garry,

Right, it was your comment that clued me in to the reference. I think things are at least working now.

VB is confused about "Selection.Goto". It probably assumes you mean whatever is selected in the Visio document. It worked after changing to "Wd.Selection.Goto".

Here is what it looks like after reworking for Visio. I'm not sure what best practice here, and have indicated in comments where I think there may be a better way to do things.

Code:
Option Explicit

'Maybe we should generalize this so that the file path is a variable.
Sub OpenWord1()

Dim Wd As Object

'Get Reference to open Word instance, if any
On Error Resume Next
Set Wd = GetObject(class:="Word.Application")
On Error GoTo 0

'Create new Word instance, if necessary
If Wd Is Nothing Then _
    Set Wd = CreateObject("Word.Application")

Wd.Visible = True      'So that we can see the word window

'
Dim Doc As Word.Document            'That's a Word Document, not Visio!
Dim Found As Boolean
For Each Doc In Wd.Documents        'Is there a better way?
       If Doc.Name = "Word1.doc" Then Found = True
Next Doc

If Found <> True Then
       Wd.Documents.Open "H:\Programs\VB_Misc\CreateWord\Word1.doc"
End If

Set Doc = Wd.Documents("Word1.doc")

Wd.Activate                   'Get the Word window in front.  Is there a better way?
Doc.Activate
Wd.Selection.GoTo What:=wdGoToPage, Count:="5"

'Not sure whether this is necessary
Set Doc = Nothing
Set Wd = Nothing

End Sub
 
Walter,

I must say...I have a true respect for programmers...I came in with a full head of hair today...and I am now down to just 2 strands of hair left! : )

I am still getting an error referencing wdGoToPage as an undefined variable...yet...my initial code does not give me that error...and it works great...

I cannot thank you enough for the time you have spent with me on this...I'm not sure how to solve it...and I will certainly try any other tricks...solutions you may have...

I will work with it more tonight at home...and if all else fails...I will throw it up here again tomorrow morning..

Thank you again Walter!

Garry
 
Hi Walter,

One more thing...I know this is crazy...but here is the code that works...well...at least for a single Command Button...I just cannot get it to work for any other buttons...(The problem lies within the wdGoToPage constant):

Private Sub cmdImpResTer_Click()
'
'Import Resources Procedures - page/section locator for OSO001001_TtMngPro
'Make sure that MS Word 11.0 Object is available by selecting <tools>
''then <references> and then selecting the checkbox next to MS Word 9.0, 10.0 or 11.0 Object
'
Dim Wd As Object
'
Set Wd = CreateObject("Word.Application")
Wd.Visible = True
'
Dim Found As Boolean
'
For Each doc In Wd.Documents
If doc.Name = "OSO1001_TtMngPro.doc" Then Found = True
Next doc
If Found <> True Then
Wd.Documents.Open "\\Svfulcommon1\Salesprojdoc\9 - Documentation\OSO Procedures\Internal\Procedures\Territory Management\OSO1001_TtMngPro.doc"
Wd.Selection.GoTo What:=wdGoToPage, Count:="36"
Else
Wd.Selection.GoTo What:=wdGoToPage, Count:="36"
End If
'
End Sub

How do we get this code to work again on another Command Button?

garry
 
Garry,

Oh dear, that was the original question, wasn't it. You have been patient.

I'm not sure how you are setting things up, but here's how it worked for me, probably repeating a lot that you already know better than I. This to set up two buttons on the same Visio document.

[ul]
[li]Open new Visio document.[/li]
[li]Insert 2 command buttons. Insert->Control->Microsoft Forms 2.0 Command Button[/li]
[li]Change to VB design mode. (A button on the Developer toolbar)[/li]
[li]Right click on first command button->View Code (VB Editor appears)[/li]
[li]Paste your code between "Private Sub ...1_click" and "End Sub"[/li]
[li]Right click on second command button->View Code (VB Editor appears)[/li]
[li]Paste your code between "Private Sub ...2_click" and "End Sub".[/li]
[li]In the subroutine "...2_click", change all paths as appropriate for the second file you want to show. Also change the page number.[/li]
[li]Quit VB design mode. (Same button on the Developer toolbar)[/li]
[li]Click buttons and see whether they work.[/li]
[/ul]

On my machine, this lets me show the appropriate page of two files.

If I were doing this, once I got it working, I would make a single subroutine to open the word documents. The ..._click subroutines would call this routine, passing the file name and page number as arguments.

WC
 
WC,

Thank you for that TIP...once I get this working...I will put it in a sub-routine...

I 'killed' all of my code and started from scratch...just to see if it was some kind of 'cyber' glitch or something. I used the working code that I had...along with the additions that you provided...and the 1st button worked like a charm!

When I copied that same identical code to a 2nd button...and ran it...I received a compile error again...stating that the "Variable not defined"...and it pointed to 'wdGoToPage' as the variable not defined...

I am simply at my wits end...I am going to throw it out there again...and see if anyone knows why this is happening...

Thank you Walter...for your time...and your diligence!!

Garry
 
Garry,

Sorry I couldn't help. I'll stay out of the way on the next post!

wdGotoPage is a constant that is defined in the Microsoft Word Object Library. Is it possible that you have not referenced it in the Visio document that you have added the button to?

WC
 
A work around:
Wd.Documents("OSO1001_TtMngPro.doc").Goto What:=1, Count:=35 [green]' 1=wdGoToPage[/green]

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
WC,

I thought about that...which is why I copied the code identically...and pasted it to the 2nd button...but I still get the error...

I am thinking about using a 'Bookmark' instead of a Page reference...because as the document changes...so will the Page numbers...but if I reference the Page heading and set it as a Bookmark...I won't have to try and maintain the code each time edit and changes are made to the procedural document...

Maybe that will also get rid of the problem I am having with the wdGoToPage constant...

Any suggestions??

Thanks Walter!

Garry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top