Solved

Open Word Doc from Button in MS Access

Posted on 2014-11-24
9
476 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
You dont't need the Word library for my code..
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 24

Expert Comment

by:Phillip Burton
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
everyhing is fine
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
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 stored procedures 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 Micr…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

744 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

19 Experts available now in Live!

Get 1:1 Help Now