OPening Word doc from button

wilsation

Registered User.
Local time
Today, 17:57
Joined
Jul 28, 2014
Messages
44
Hi all, I want a word doc to open when I click a button - simple.
The problem is that I have to click the button twice to get the doc to open. How do I get it to work on a single click?

Many thanks

Here's my code:

Code:
 Private Sub cmdInstruct_Click()
 Dim appWord As Word.Application
Dim doc As Word.Document
'Avoid error 429, when Word isn't open.
On Error Resume Next
'Err.Clear
'Set appWord object variable to running instance of Word.
Set appWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then
'If Word isn't open, create a new instance of Word.
Set appWord = New Word.Application
End If
If cboInstruct.Value = "Contractor's Notification" Then
Set doc = appWord.Documents.Open("[URL="file://\\mylocation\StdLetter.doc"]\\mylocation\StdLetter.doc[/URL]", , True)
With doc
.FormFields("ProjRef").Result = Me!txtProjID
.FormFields("ProjName").Result = Me!txtProjTitle
.Visible = True
.Activate
End With
Set doc = Nothing
Set appWord = Nothing
 Exit Sub
errHandler:
MsgBox Err.Number & ": " & Err.Description
End If
End Sub
 
Try by comment out the errorhandling, (On Error Resume Next), - to see if some error pops up.
If yes the post the error description and code line.
 
Hi JHB, thanks for the tip - the error I get is

Run-time error 429, activeX component can't create object

and highlights this code:

Code:
Set appWord = GetObject(, "Word.Application")

a quick google suggests adding DAO references but they are already added. Also suggests reinstalling Access/Word but as I work in a corporate environment that isn't so straightforward.
 
Okay - what happen if you press the button again, you wrote each second click open the document, still doing that?
 
Hi JHB, yes, word still opens on second click (after I uncomment OnError)

BTW I'm running Office/Access 2010 on Win7
 
Hmm - could you post your database?
 
How do I post my db? Site only allows up to 2mb for accdb files.
 
Do a "Compact & Repair", then zip it.
 
Because of the missing data and the document, I couldn't test it 100%.
I've made some change in the code behind cmdInstruct_Click() event.
And also made a test form - Form1, remember to set the path for the document to yours.
Try it.
 

Attachments

Hi, yes I added thumbs up but I can't see a 'Thanks' button
 
I think you need to log in!
Else forget it - good luck anyway. :)
 

Users who are viewing this thread

Back
Top Bottom