Avatar of gksewell
gksewell
 asked on

Open Word Doc from Button in MS Access

Win7 - MSaccess ver10

below is code for event on button but get no errors and no document to open

Function Openword(conpath) As String)
dim appword As Word Applicatiion
Dim doc As Word Document

On Error Resume Next
Error.Clear
Set appword = Getobject(, "word.aplication")
If Err.number <> 0 Then
Set appword = New Word Application
appword.Visible = True
End If
Set doc = appword.Documents.Open(conPath), , True)
appword.Activate

Set doc = Nothing
Set Appword = Nothing

End Function

Private Sub Command20_Click()
Dim mydoc As String
mydoc = "C:\Users\sewellg\Desktop\New Router Config Tool 2\DC2 VPN Router Config.docm
Call Openword (mydoc)

End Sub
Microsoft Access

Avatar of undefined
Last Comment
gksewell

8/22/2022 - Mon
Phillip Burton

I prefer simple. Why not replace all of that with this?:

Private Sub Command20_Click()
Dim mydoc As String
mydoc = "C:\Users\sewellg\Desktop\New Router Config Tool 2\DC2 VPN Router Config.docm
Application.FollowHyperlink mydoc
End Sub

Open in new window

gksewell

ASKER
much simpler but still no initiation, nor error message
Have this added as a button event procedure for on click and do have word library avail in references
other suggestions?
gksewell

ASKER
I've requested that this question be closed as follows:

Accepted answer: 0 points for gksewell's comment #a40462581

for the following reason:

much shorter code for same result
Your help has saved me hundreds of hours of internet surfing.
fblack61
Phillip Burton

You dont't need the Word library for my code..
Phillip Burton

I think you've pressed the wrong button - I think from your message you meant to accept my answer as the answer to the question
ASKER CERTIFIED SOLUTION
Nick67

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Nick67

If you were going to try and recycle an open Word Instance then this is how you do it:

Private Function FireWord() As Object
On Error Resume Next
Dim oApp As Object

Set oApp = GetObject(, "Word.Application")
'MsgBox Err.Number & " " & Err.Description
If Err.Number = 429 Then
    Err.Clear
    wasOpen = False
    ' Create the Word App.
    Set oApp = CreateObject("Word.Application")
End If

Set FireWord = oApp
End Function

Private Sub Command20_Click()
Dim oApp As Object
Set oApp = FireWord()
oApp.Visible = True
End Sub

Open in new window


This way, the function has the bare minimum of things that can go wrong in it, and can go BANG! for anything but the 429 (object doesn't exits) error.
Everything else happens in the command button click event.
If you haven't picked a naming convention yet, now's the time.  :)
command20 _click()
That will be just evilly unmaintainable down the line.

Nick67
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
gksewell

ASKER
everyhing is fine