Opening a Word doc using a command button on a form.

G

Guest

Hi all:
Can anyone please tell me what code is needed to open a particular MS Word
document? For example say the path to the file is: C:\Folder\FileName.doc.
What code do I need to open the document?

The background is...I have a form that the user selects a value from a combo
box that populates the criteria in a query. The query provides the records
to be used in a mail merge in MS word.

Any help would be greatly appreciated.

Thanks,
FatMan
 
G

Guest

This does not appear to work. the times I have tried it the only way it would
work was if the parameter was a valid URL such as http://abc.com.au UNC or
File paths do not work. If you try you get the message "Cannot open..."
Please advise if I am wrong.
Terry
 
G

Guest

Sorry Regan
It does work. After seeing it in other posts I tried it again successfully.
Terry
 
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 command
button I have created using the mini wizard the VB code ends up like so (with
some subsituted server/folder names but format is exactly the same):

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\servername_highlevel_folder\BUSINESS FOLDER\TeamFolder\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.
(\\servername\servername_highlevel_folder\BUSINESS)


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.
It seems to be falling over at one of the folder names (BUSINESS FOLDER) by
not recognising the space character. How to I represent a space in a VB
string?? I have trawed Google but can't get a straight, clear answer.

Your help would be much appreciated.
Cheers Venia
 
G

Guest

Hello Venia
Try
Application.FollowHyperlink <filename>
or, in your case
Application.FollowHyperlink "\\<servername>...How_Do_I.doc"
Note 1: Replace <servername>... with the path you specified
Note 2: Notice that winword.exe is not used, only the fully qualified path
to the file to be opened.

This will open the document in Word (assuming Word is the application
registered for .DOC extension otherwise it will be opened in the app
registered). I think what you used would work but you would need to either:
1. specify the fully qualified path for Winword.exe Of course this is
problematic as you would need to either hard wire the Winword.exe path or
locate it from the registry (possible but messy)
OR
2. Use your same line but omit "winword.exe" and allow Windows to use the
default app for DOC extensions

The beauty of using the Hyperlink method is it saves you having to worry
about locating the app which handles DOC extensions and where it is located.
The user may ot may not have Word and may or may not use Word as the default
handler for DOC extensions. Winword.exe can be in many folders depending on
the version and can also vary if the user has not accepted the default path
for the installation of Office.
Hope this helps and is not too convoluted
Terry
 
G

Guest

As mentioned I am a complete VB noobie, where exactly would I substitue your

Application.FollowHyperlink "\\<servername>...How_Do_I.doc"

in the VB already in existance? Would it be:

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 <----- do I remove/replace/amend this line???

Application.FollowHyperlink
"\\servername\servername_highlevel_folder\BUSINESS FOLDER\TeamFolder\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

?? Is there a specific character in VB to represent a space character in a
string (I have already tried _ underscore in all the spaces in the existing
string and it produces a slightly different error) Look forward to your
response.

Many thanks, Venia
 
G

Guest

It seems you have a button called Open_LAN_ID_Tech_Request___How_Do_I and you
have the code in the Click event. I suggest you name your controls something
a little more manageable.
On the form right click the control and select Properties.
On the Other tab find Name and call the button btnOpenRequest
(Avoid spaces, follow a convention of calling buttons btn<name> and use case
to make it readable, hence btnOpenRequest)
Right click the button and select Build Event
The code window will be displayed with the cursor already in position with
the first & last lines in place
Your code should end up like:

Private Sub btnOpenRequest_Click()
Dim stAppName As String

stAppName = "\\servername\servername_highlevel_folder\BUSINESS
FOLDER\TeamFolder\DATABASE FOLDER\Working Progress\LAN ID Tech Request How Do
I.doc"
'Previous stAppName lines are all on one line
Application.FollowHyperlink stAppName
End Sub

From your question I assume the path contains SPACES rather than
underscores. Spaces can be put into strings using the space bar eg "Hello
Venia" In creating a variable for your filename use the string exactly as it
appears in the path in My Computer or Windows Explorer eg "C:\Program
Files\Microsoft Office\Some File" stAppName in this example would be set
using:
stAppName = "C:\Program Files\Microsoft Office\Some File"
You probably mistook using the underscore because VBA editor puts
underscores in place of spaces because NAMES of objects cannot contain spaces
(not entirely true but let's not confuse the issue. Spaces complicate object
names beyond belief). A single space can be referred to as " " while an empty
string is nothing between the quotes ""
When you open the editor to enter code for a control event the editor will
append the event name eg Click to the control name; so, if the control is a
button with the name btnOpenRequest the event name will be
btnOpenRequest_Click
You can also insert a space using the CHR function eg
stAppName = stVar1 & chr(32) & stVar2
Chr(32) equates to a space, stVar1 and stVar2 are two variables you may have
set somewhere else and & is concatenate. So
stVar1 = "Joe"
stVar2 = "Goose"
stAppName = stVar1 & chr(32) & stVar2 &" ROCKS"
stAppname now contains "Joe Goose ROCKS"
Hope this helps
Terry
 
D

Den

I am trying to do something similar. I want to open a word doc that is
associated with a particular record. I created autonumbers and want to open
a word doc associated with that record. Currently I have a hyperlink that
opens the folder containing the word docs associated with the records and I
can choose the corresponding docs. I would like it to read the records
autonumber and open that particular doc -- if the records autonumber is
"123", I want the hyperlink to open "123.doc". Is this possible?
 
T

Terry

Hello Den
Yes. The code is almost identical to the previous reply. BUT, it does rely
on the application for .DOC extensions being registered. Instead of putting
the app into appname put the fully qualified path to the doc.
Terry
 
A

Albert D. Kallal

Above code would be:

dim strDoc as string

strdoc = "c:\Folder name\" & me!id & ".doc"

Application.FollowHyperlink strDoc


The above assumes that your autonumber field is "id" (so, change above if
different.
 
D

Den

Thanks for the help but I am very green and I guess I did not understand your
help. When I edited the hyperlink I received a message "unable to open.
cannot open the specified file." The folder I keep the files in is:
c:\Program Files\APP My autonumber field is AUTONUMBER so I made the
hyperlink address: c:\Program Files\APP\" & me!AUTONUMBER & ".doc
What did I do wrong?
 
A

Albert D. Kallal

We not using a hyperlink field, and I recommend you don't.

We placing simply placing a button on the form that runs the code mentioned.

So, no hyperlink field or control on the form is needed

Simply place that code behind a button on the form...
help. When I edited the hyperlink I received a message "unable to open.
cannot open the specified file." The folder I keep the files in is:
c:\Program Files\APP My autonumber field is AUTONUMBER so I made the
hyperlink address: c:\Program Files\APP\" & me!AUTONUMBER & ".doc
What did I do wrong?

A hyperlink field should work here however.

You have:

c:\Program Files\APP\" & me!AUTONUMBER & ".doc

I don't see a starting quote the above should be:

"c:\Program Files\APP\" & me!AUTONUMBER & ".doc"

so, each string part needs to have a starting " and an ending "

Try the above syntax. If it don't work, then you could try the button + code
idea....
 

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