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
 
Garry,

The bookmark sounds like a good idea...

But I don't understand something. When you copy the code to a second button, is the second button in another Visio document? Does that Visio document also have a reference to Microsoft Word Object Library? If it didn't, I would expect the error that you describe.

Did you try PH's workaround, a couple of posts up from this one?

Walter
 
PH,

Thanks for the TIP...I tried it but it still didn't work...After talking to the Program Manager overseeing this project...I think I'll need to find a true solution...she doesn't even want the 'hard coded' page numbers in there for maintenance reasons...

Thank you though!

Garry
 
WC,

Actually...I hadn't thought about checking each Visio document...I think I figured that that reference was global...once it was checked...

I am at home...and I don't have access to the shared folders at work...from home...so I will check this out first thing in the AM and let you know...

Thank you for hanging in there for me...maybe you have something here!

Garry
 
Walter,

I wish that would have worked...but that does not seem to be the issue. I even changed wdGoToPage to wdGoToBookmark...it works great with the first button I created.

Here is the code for cmdButton #1:

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:=wdGoToBookmark, Name:="NotificationRevTer"
Else
Wd.Selection.GoTo What:=wdGoToBookmark, Name:="NotificationRevTer"
End If
'
Wd.Activate
Set Wd = Nothing
'
End Sub

but when I try to use the code for button 2 or any subsequent command button...I get an 'Error' stating "Variable not defined" for 'wdGoToBookmark'

Here is the code for cmdButton #2:

Option Explicit

Private Sub cmdNotRevPrd_Click()
'
'Notification Review Procedures - page/section locator for OSO001002_PdMngPro
'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 = "OSO1002_PdMngPro.doc" Then Found = True
Next doc
If Found <> True Then
Wd.Documents.Open "\\Svfulcommon1\Salesprojdoc\9 - Documentation\OSO Procedures\Internal\Procedures\Product Management\OSO1001_PdMngPro.doc"
Wd.Selection.GoTo What:=wdGoToBookmark, Name:="NotificationRevPrd"
Else
Wd.Selection.GoTo What:=wdGoToBookmark, Name:="NotificationRevPrd"
End If
'
Wd.Activate
Set Wd = Nothing
'
End Sub

How can this be?

Garry

 
Walter,

You are brilliant!! I am such a lame duck! YES...you were correct! When i checked to see if I had MS Word Object checked in the library...what was actually checked for diagram #2 that contained cmdButton #2 was MS Office Object...but MS Word Object had not been checked for that associated VBA coding page...once I checked the box...it FLEW LIKE A BIRD!!!

I owe you buddy...Thank you so very much...I am supposed to present on Friday...and I was wondering if I was going to have to revert to plan "B"...which I did not want to do...

You have saved the day...and the "farm"!!

I am now naming today...Sept. 28...as "WC" day...and I am gonna celebrate like hell!!...hahahaha

Thank you again Walter! and enjoy the day...it's all yours!

Sincerely,

Garry
 
Garry,

Don't mention it. Actually, I'm likely to be using Visio to do some circuit schematics in the near future, and I've never learned it. I'll probably be looking for help on the appropriate forum around here...

Best Regards,
Walter
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top