Can I run a file with a known extension from Excel VBA

M

MikeZz

Hi,
I have a file with a ".mac" extention that's associated with an application
in windows.

Is there a way that I can have Excel run the file from within VBA?

Thanks
MikeZz
 
P

Peter T

Try something like this -

Private Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" (ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long

Private Declare Function GetDesktopWindow Lib "user32" () As Long

Sub test()
Dim ret As Long
Dim sFile As String

sFile = "C:\Temp\test.txt"

ret = ShellExecute(GetDesktopWindow, vbNullString, sFile, _
vbNullString, vbNullString, vbNormalFocus)

If ret > 32 Then
' it worked
Else
' it failed, the code 1-32 will give the reason, eg
' 2 file not found
' 31 file not associated
Debug.Print ret
End If

End Sub

If it doesn't work and you are sure the extention is correctly associated
and sure the file is correct, try changing the first vbNullString to "open"
(with the quotes)

Regards,
Peter T
 

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