Opening files from Excel

S

S Taylor

I am trying open non-excel files that are not .exe files from Excel VBA

When I use the Shell command it doesn't work, I believe that this is because
Shell only opens files that are .exe.

Is there a method that can open other files from Excel VBA?
 
M

Mike H

Generally opening any file requires an executable to do it wether its a .doc,
..xls or anything else

Try this

myfile = Shell("NOTEPAD.EXE " & "c:\lastrow.txt", vbNormalFocus)

This would open lastrow.txt from the root of C with Notepad

Mike
 
S

S Taylor

Thanks Mike,
The problem is that the .exe application is already open, and if I shell
the executable file will create another instance of it. However if I call the
file that I am trying to open it opens it from within the existing instance
of the .exe file. Thanks anyway. (Hope that makes sense!)

Regards

Steven
 
L

Libby

'Put this in a module

Option Explicit

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

Function openanyfile(FileToOpen As String)
Call ShellExecute(0, "Open", FileToOpen & vbNullString, _
vbNullString, vbNullString, 1)
End Function

'To open a file, in this case a pdf

Sub Open_file
Call openanyfile("C:\My Documents\FileToOpen.pdf")
end sub
 

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