Opening a connected word or excel file from acces

S

Stefan

Hello,

I am trying to open a file from word and excel from my database.
I am using this code, it works fine for pdf and image files.
doc and xls files only work as long as there are no spacing in the name.
When there are spaces in the name I get errors in both word and excel

Public Sub Open_File(Filename As String)
On Error GoTo Err_Click
Dim I As Integer, E As String, Prog As String
'Extensie zoeken
I = InStr(1, Filename, ".")
If I = 0 Then Exit Sub
E = Mid(Filename, I + 1, Len(Filename) - I)
'Juiste programma selecteren
Select Case E
Case "jpg", "jpeg", "bmp", "tif", "tiff"
Prog = "C:\WINDOWS\SYSTEM32\MSPAINT.EXE"
Case "doc"
Prog = "C:\Program Files\Microsoft Office\OFFICE11\WINWORD.EXE"
Case "xls"
Prog = "C:\Program Files\Microsoft Office\OFFICE11\EXCEL.EXE"
Case "pdf"
Prog = "C:\Program Files\Adobe\Acrobat 7.0\Reader\AcroRd32.exe"
Case Else
Exit Sub
End Select
'Applicatie openen
Prog = Prog & " " & Filename
Call Shell(Prog, 1)
Exit_Click:
Exit Sub
Err_Click:
MsgBox Err.Description
Resume Exit_Click
End Sub
 
D

Douglas J Steele

You need to put quotes around the file paths when there are spaces:

Prog = Chr$(34) & Prog & Chr$(34) & " "& Chr$(34) & Filename & Chr$(34)

By the way, you might find it easier to use the FollowHyperlink method, or
the ShellExecute API (see http://www.mvps.org/access/api/api0018.htm at "The
Access Web" for a complete example) so that you don't need to worry about
where the executables are.
 
S

Stefan

Yes that did the trick.
Now any file can be opened easily.
Strange however that pdf file did not requiry this.

So Thanks for the hint.
I also took a look at your site, seems you have nice hobby's
In Belgium We also appreciate the occasional beers
 
D

Douglas J Steele

Stefan said:
So Thanks for the hint.
I also took a look at your site, seems you have nice hobby's
In Belgium We also appreciate the occasional beers

Belgium is my idea of beer nirvana.

I spent a few very pleasant days in Bruges and Brussells (apologies if I
misspelled the names!) too many years ago, and would love to visit again!
 

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