Solved

Open Word Doc from Button in MS Access

Posted on 2014-11-24
9
484 Views
Last Modified: 2014-11-26
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
Comment
Question by:gksewell
  • 3
  • 3
  • 2
9 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40462381
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
 

Author Comment

by:gksewell
ID: 40462581
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
 

Author Comment

by:gksewell
ID: 40463117
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
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40463115
You dont't need the Word library for my code..
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40463118
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
 
LVL 26

Accepted Solution

by:
Nick67 earned 500 total points
ID: 40463542
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
 
LVL 26

Expert Comment

by:Nick67
ID: 40463564
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
 

Author Comment

by:gksewell
ID: 40466593
everyhing is fine
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now