Opening a word document from a command button on a form

G

Guest

I have created a new command button on a form which I would like to open a
word document with. This is the code I have put in when creating the
cmdButton

Private Sub cmdReport1_Click()
On Error GoTo Err_cmdReport1_Click

Dim oApp As Object

Set oApp = CreateObject("Word.Application")
oApp.Visible = True

Exit_cmdReport1_Click:
Exit Sub

Err_cmdReport1_Click:
MsgBox Err.Description
Resume Exit_cmdReport1_Click

End Sub


This opens word like a dream but how do I point it to my document so that
when the application opens it opens with the document.

For this exercise lets just say the document name I wish to open is TEST.DOC
and is located at c:\database\word\TEST.DOC.

Thanks in advance and I am going to post this same message in Programming as
I am not sure which group to go to on this question.
 
G

Graham Mandeno

You're missing only one line:
oApp.Documents.Open "c:\database\word\TEST.DOC"

Of course, the file name could be a variable, or the name of a textbox, or a
field in a recordset, etc.
 
G

Guest

much appreciated thank you

Graham Mandeno said:
You're missing only one line:
oApp.Documents.Open "c:\database\word\TEST.DOC"

Of course, the file name could be a variable, or the name of a textbox, or a
field in a recordset, etc.
--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand

Slow Learner said:
I have created a new command button on a form which I would like to open a
word document with. This is the code I have put in when creating the
cmdButton

Private Sub cmdReport1_Click()
On Error GoTo Err_cmdReport1_Click

Dim oApp As Object

Set oApp = CreateObject("Word.Application")
oApp.Visible = True

Exit_cmdReport1_Click:
Exit Sub

Err_cmdReport1_Click:
MsgBox Err.Description
Resume Exit_cmdReport1_Click

End Sub


This opens word like a dream but how do I point it to my document so that
when the application opens it opens with the document.

For this exercise lets just say the document name I wish to open is
TEST.DOC
and is located at c:\database\word\TEST.DOC.

Thanks in advance and I am going to post this same message in Programming
as
I am not sure which group to go to on this question.
 
G

Guest

I too have this problem. When I attempt to use this fix, I get an error
message that says: "Object doesn't support this property or method". I am
using Access 2000. The code is:
Dim oApp As Object

Set oApp = CreateObject("Word.Application")
oApp.Visible = True
oApp.Document.Open "C:\Documents and Settings\u8ra10\My
Documents\AltCreditPendDoc.doc"

Did this suggestion work for you?
 
J

John Nurick

It's
oApp.Documents.Open "C:\...

When I'm writing Automation code like this I always set a reference to
the appropriate object libraries (Tools|References, and for Word select
the "Microsoft Word X.xx Object Library"). This means that the Word
objects etc. are available in the Object Browser for easy reference
(press F2).

Usually I also declare objects with their specific type, e.g.

Dim oWordApp As Word.Application

which among other things makes them known to the Intellisense dropdown
lists in the VBA editor.

All this makes the coding a lot easier. Then when the code is working
right I switch back to late binding by changing the declarations, e.g.

Dim oWord App As Object

and removing the reference to their object library.

But if you just want to launch a Word document, just try

Dim strFileSpec as String

strFileSpec = "C:\blah blah blah.doc"
Application.FollowHyperlink strFileSpec
 
G

Guest

Hi I am trying to do a very similar thing, but I am trying to open a Word doc
over an internal network. I am using Access 2002 and from the form comand
button I have created using the mini wizard the VB code ends up like so (with
some subsituted path names):

Private Sub Open_LAN_ID_Tech_Request___How_Do_I_Click()
On Error GoTo Err_Open_LAN_ID_Tech_Request___How_Do_I_Click

Dim stAppName As String

stAppName = "Winword.exe \\servername\highlevel folder\group
folder\database folder\Working Progress\LAN ID Tech Request - How Do I.doc"
Call Shell(stAppName, 1)

Exit_Open_LAN_ID_Tech_Request___How_Do_I:
Exit Sub

Err_Open_LAN_ID_Tech_Request___How_Do_I_Click:
MsgBox Err.Description
Resume Exit_Open_LAN_ID_Tech_Request___How_Do_I

End Sub

I am a complete noobie when it comes to VB coding and the error message I
get from Word is:
The document or path name is not vaild. Try these suggestions.
* Check the file permissions for the document or drive.
* Use the file open dialog box top locate the document.
(C:\Documents and Settings\mylanid\...\ID)


Firstly is it actually possible to open an Office document over a network
from an Access command button??

Secondly, if it is possibly where am I going wrong with the command/string?
I do have access/permissions to the drive and document I am trying to open.

Cheers Venia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top