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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WORD Automation (wdGoToPage)

Status
Not open for further replies.

TekRite

Technical User
Sep 25, 2005
17
US
The problem I am having with the code below seems to be with its "reusablity" with subsequent Command Buttons. The code "works fine...lasts a long time" with the initial Command Button it is assigned to...

but when I copy and paste it to another Command Button I get either a 'Compile Error' "Variable not defined" with a reference to 'wdGoToPage' as the supposed undefined variable

OR

The code OPENS the specified document but does not GOTO the page specified in the GoTo Method.

The Command Buttons are inserted on Visio drawings for the purpose of Creating an instance of WORD...Opening the specified document...and going to the Page specified with the What:=wdGoToPage, Count:="page#".

Here is the code that works...but only for this one button:

Option Explicit

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
Dim doc As Document
'
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:="39"
Else
Wd.Selection.GoTo What:=wdGoToPage, Count:="39"
End If
'
End Sub

Can anyone solve this for me?

Garry
 
quit word at the end, specify whether or not you want to save changes.

then set it to nothing.

dont know if it will work or not, but maybe its hanging and causing conflict with 2 objects open.

wd.Quit wdDoNotSaveChanges
Set wdTheApp = Nothing
 
Hi Garry,

For the code to work you need to have a reference to the Microsoft Word Object Library. If you don't have one, "wdGoToPage" will not be recognised and the behaviour (compile error or runtime failure) will depend on whether you have "Option Explicit" at the start of the code module.

If you don't want to have to worry about the reference (as you are already using late binding) use a value of 1 instead of the Word constant.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Am wrong in thinking this has some logic gaps?
Code:
For Each doc In Wd.Documents
    If doc.Name = "OSO1001_TtMngPro.doc" Then Found = True
Next doc

Say - and maybe this would never happen - there are three docs in the Documents collection. They are:

OSO1001_TtMngPro.doc
whatever_A.doc
whatever_B.doc

There are three docs, so there are three iterations of For Each doc. The variable Found changes with every doc in the collection. The IF statement would have the following result.

Iteration 1 - Found = True
Iteration 2 - Found = False
Iteration 3 - Found = False

Further, even if it is True, the truth of it is that the IF statement determines if the doc is IN the collection. It does not test to see if it is the active document. Therefore, it is possible that even if True,
Code:
Wd.Selection.GoTo What:=wdGoToPage, Count:="39"
would act on the incorrect document. As it stands the code will act on the active document, not explicitly the named doc.


Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top