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

MS Excel Button to open Word doc inconsistent...

Status
Not open for further replies.

shellig

Technical User
Apr 19, 2002
34
US
To open a specific Word document from Excel (Office 2003), I have the following (obviously on a button):

Code:
Private Sub CommandButton1_Click()
    On Error Resume Next
    Err.Clear
    Set wrd = GetObject(, "Word.Application")
    If Err.Number = 429 Then
        Err.Clear
        Set wrd = CreateObject("Word.Application")
        If Err.Number = 429 Then
            MsgBox "MS Word is not installed on the computer."
        End If
    End If
    wrd.Documents.Open "March05.doc"
    wrd.Documents("March05.doc").Activate
    wrd.Visible = True
    Set wrd = Nothing
End Sub
When CreateObject is used, the Word document is the active application...when GetObject is used, I cannot figure out a way to make it the active application. I have tried various iterations of visible and activate...any suggestions?

BTW, the reason I am using a button is a hyperlink to a network document does not work in a protected worksheet...strange hunh!!
 
GetObject just grabs one. If the user has more than one Word instance running, you cannot control which one it grabs. Since the operating system allows multiple instances, maybe you should just create a new one every time. That way, when you are done with it, you can close Word without worrying about it.

If that isn't an object, there is an API called GetWindow, I think, which allows you to iterate looking for a specific titlebar text which you would know since you just opened the document using automation. Sorry I can't give you more, but it may at least get you started. Good Luck!

Have a great day!

j2consulting@yahoo.com
 
It doesn't move to any Word document...it stays on the Excel document...I just don't get it!!!
 
Your code works fine for me - you probably don't need the .Activate line, but you do need a fully qualified file path

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'

Essex Steam UK for steam enthusiasts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top