Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 593
  • Last Modified:

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
0
gksewell
Asked:
gksewell
  • 3
  • 3
  • 2
1 Solution
 
Phillip BurtonCommented:
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

0
 
gksewellAuthor Commented:
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?
0
 
gksewellAuthor Commented:
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
0
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
Phillip BurtonCommented:
You dont't need the Word library for my code..
0
 
Phillip BurtonCommented:
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
0
 
Nick67Commented:
Well,
no matter what else, you should understand why your code went wrong

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

On Error Resume Next
'while this is nice, it can be sloppy coding and will cause your code
'to fail without any indication why if you aren't careful.
'Given that you originally has Word Application -- which is incorrect syntax
'your code was dead in the water

 Error.Clear
'this too is sloppiness and not how you handle a failure to GetObject
 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

'now, you went to the trouble of opening work
'why kill it?

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

So, what could your code have looked like?
It looks like you've found code for trying to recycle an existing, open Word instance.
Certainly for Outlook -- which should only have one instance on the go at time -- that would be important
It is not so needful for Word, though.

What would have been perfectly good is

        Dim oApp As Object ' Going to be Word
        Set oApp = CreateObject(Class:="Word.Application") 'late binding so we don't need a reference
        oApp.Visible = True ' let's see it
        oApp.Documents.Add  "C:\Users\sewellg\Desktop\New Router Config Tool 2\DC2 VPN Router Config.docm"
        oApp.UserControl = True
0
 
Nick67Commented:
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
0
 
gksewellAuthor Commented:
everyhing is fine
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now