Examples for Visual Basic for Application accessing the Windows API: ShellExecute
This example creates two files and then opens them by calling ShellExecute.
The first file is opened by passing its filename as lpFile. Windows will then use the default application for .txt file to open the file. By default, this is notepad.
Then, ShellExecute is invoked by passing notepad.exe as lpFile argument and the filename of the file to be opened as lpParameters argument. Thus, it makes sure that the file is opened with notepad.
This example needs the VBA declarations of the Windows API which can be found here.
option explicit
sub writeFile(filename as string)
open filename for output as #1
print# 1, "This is the file"
print# 1, "with the filename " & filename
close# 1
end sub
sub main()
dim file_1 as string
dim file_2 as string
file_1 = environ("TEMP") & "\one.txt"
file_2 = environ("TEMP") & "\two.txt"
call writeFile(file_1)
call writeFile(file_2)
call ShellExecute(0, "Open", file_1 , "" , "", 1)
call ShellExecute(0, "Open", "notepad.exe", file_2, "", 1)
end sub