Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Hint: Starting a project file from VB

176 views
Skip to first unread message

HKSHK

unread,
Aug 29, 2005, 3:18:49 AM8/29/05
to
Hi folks!

If you want to start a project file (e.g. a PDF, TXT, DOC) from VB you would
traditionally use Shell "start " & Chr$(34) & FileName & Chr$(34).
This method works fine as long as you use Windows 9x/ME. However with Windows XP
it does not work like this. Windows XP doesn't have start.exe so it won't
work. If you use "cmd /c" there are these traps:

* If a file name has a white space(s) in it you need to put quotes around it.

* If a file name has NO white space in it you MUST NOT put quotes around it.

The code below help you with that. It will:

* Convert spaces in URLs to %20

* Check if it's running on a Windows 9x/ME system (checking for Windows\Command\start.exe)

* Handle the quotes correctly

Best Regards,

HKSHK

Option Explicit

Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long

Sub HKStartFile(ByVal StartThisFile As String)
Dim tmpFilename$, ThisFile$
ThisFile$ = Trim(StartThisFile$)
If Len(ThisFile$) > 0 Then
If InStr(ThisFile$, " ") > 0 Then
If InStr(ThisFile$, "://") > 0 Then
ThisFile$ = Replace(ThisFile$, " ", "%20") 'For URLs replace spaces with %20.

'This removes the doublequotes from the file name if they exist and no
'white space can be found in the file name. This is required for Windows XP.
If Left$(ThisFile$, 1) = Chr$(34) Then ThisFile$ = Right$(ThisFile$, Len(ThisFile$) - 1)
If Right$(ThisFile$, 1) = Chr$(34) Then ThisFile$ = Left$(ThisFile$, Len(ThisFile$) - 1)
Else
If Left$(ThisFile$, 1) <> Chr$(34) Then ThisFile$ = Chr$(34) & ThisFile$
If Right$(ThisFile$, 1) <> Chr$(34) Then ThisFile$ = ThisFile$ & Chr$(34)
End If
Else
'This removes the doublequotes from the file name if they exist and no
'white space can be found in the file name. This is required for Windows XP.
If Left$(ThisFile$, 1) = Chr$(34) Then ThisFile$ = Right$(ThisFile$, Len(ThisFile$) - 1)
If Right$(ThisFile$, 1) = Chr$(34) Then ThisFile$ = Left$(ThisFile$, Len(ThisFile$) - 1)
End If


If Dir(GetWinDir$ & "command\start.exe") > "" Then 'Windows 9x/ME
If Left$(ThisFile$, 1) <> Chr$(34) Then ThisFile$ = Chr$(34) & ThisFile$
If Right$(ThisFile$, 1) <> Chr$(34) Then ThisFile$ = ThisFile$ & Chr$(34)
Shell "start " & ThisFile$

Else 'Windows NT/2000/XP

If InStr(ThisFile$, "://") > 0 Then
Shell "cmd /c start " & ThisFile$
Else
Shell "cmd /c " & ThisFile$
End If
End If
End If
End Sub

Function GetWinDir() As String
Dim strWinPath As String
Dim lngWinPath As Long

' Fill string with null characters.
strWinPath = String(144, vbNullChar)

' Get length of string.
lngWinPath = Len(strWinPath)

' Call GetWindowsDirectory, passing in string length and string.
If (GetWindowsDirectory(strWinPath, lngWinPath) > 0) Then
' GetTempPath returns path into string.
' Truncate string at first null character.
GetWinDir$ = Left(strWinPath, InStr(1, strWinPath, vbNullChar) - 1)

'Check, if the last character is a Backslash.
If Right$(GetWinDir$, 1) <> "\" Then GetWinDir$ = GetWinDir$ & "\"
Else
GetWinDir$ = ""
End If
End Function

Jan Hyde

unread,
Aug 30, 2005, 4:18:53 AM8/30/05
to
HKSHK <hk...@gmx.net>'s wild thoughts were released on Mon,
29 Aug 2005 15:18:49 +0800 bearing the following fruit:

>Hi folks!


http://vbnet.mvps.org/index.html?code/shell/shellexecute.htm

J


Jan Hyde (VB MVP)

--
A good pun is it’s own reword.

[Abolish the TV Licence - http://www.tvlicensing.biz/]

0 new messages