MrExcel Publishing
Your One Stop for Excel Tips & Solutions

Launching a location-specific application

Posted by Tim Seddon on July 25, 2001 4:21 PM

I would like to make a macro that launches the Windows Calculator, no matter what type of Operating system it is on.

Since location of "Calc.exe" is normally found in the PATH, is there a VBA command/set of commands that will return the PATH of the system and let me assign it to a variable that can be used in a SHELL command?

If anyone has any alternative suggestion, it would be greatly appreciated.

Posted by Ivan F Moala on July 26, 2001 2:18 AM

Try something like this ....??

Private Declare Function GetWindowsDirectoryA Lib "KERNEL32" _
(ByVal lpBuffer As String, ByVal nSize As Long) As Long
'This function returns the length, in characters,
'of the string copied to the buffer, excluding the terminating null character.
'· lpBuffer
'Points to the buffer to receive the null-terminated string containing the path.
'This path does not end with a backslash unless the Windows directory is the root directory. For example, if the Windows directory is named WINDOWS on drive C, the path of the Windows directory retrieved by this function is C:\WINDOWS. If Windows was installed in the root directory of drive C, the path retrieved is C:\.

'· nSize
'Specifies the maximum size, in characters, of the buffer specified by the lpBuffer

Private Function GetWindowsDirectory() As String
Dim sWinDir As String
Dim i As Integer

i = GetWindowsDirectoryA("", 0)
sWinDir = Space(i)
GetWindowsDirectoryA sWinDir, i
GetWindowsDirectory = AddBackslash(Left$(sWinDir, i - 1))

End Function

Private Function AddBackslash(sWinDir As String) As String

If Len(sWinDir) > 0 Then
If Right$(sWinDir, 1) <> "\" Then
AddBackslash = sWinDir + "\"
AddBackslash = sWinDir
End If
AddBackslash = "\"
End If

End Function

Sub Lauch_Calc_()
Dim FilePath As String
Dim Run

FilePath = GetWindowsDirectory & "Calc.exe"
Run = Shell(FilePath, vbMaximizedFocus)

End Sub