Solved

Open Word Doc from Button in MS Access

Posted on 2014-11-24
9
501 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40463115
You dont't need the Word library for my code..
0
 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

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…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

832 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