_Click action - syntax to open a file?

L

Lyndon Rickards

This must be too easy, if only it wasn't for
the vicodin...so please be patient with me ;->

Private Sub CommandButton1_Click()
system("pdfFile.pdf")
' or "docfile.doc"
' or "file.html"
End Sub

The external files will be distributed with the workbook, so I'm
not concerned about path relativity, though it would be nice to
be able to throw a msgbox just in case the file is not present in
the same folder as the workbook, or in the unlikely event
a suitable application is not available.

TIA - Lynn.
 
D

Dave Peterson

Option Explicit
Private Sub CommandButton1_Click()
dim teststr as string
dim myFileName as string

myfilename = "x:\pathtoyourfile\yourfile.ext")

teststr = ""
on error resume next
teststr = myfilename
on error goto 0
if teststr = "" then
msgbox "file not found
exit sub
end if

'take a look at Shell to see how to start other programs
'maybe something like:
Shell "Start " & chr(34) & myfilename & chr(34)

End Sub
 
L

Lyndon Rickards

Thanks as always Dave. And I never would have guessed
chr(<anything>)...haven't used that since my early Basic
days - early 80's.

Geez how I wish Excel supported PerlScript!

Best - Lynn.
 
D

Dave Peterson

I find working with quotes sometimes confusing, but you could use this instead:

Shell "Start """ & myFilename & """"
instead of
Shell "Start " & chr(34) & myfilename & chr(34)
 

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