Search code examples
vb6

Passing variables in Shell command in VB 6


I would like to know if there is a way to pass a variable to the vb6 Shell command. Something like this:

Var$ = "filename.jpg"

'I've already tried:

Shell Var$, vbNormalFocus
Shell "mspaint.exe" & txtFileBande.Text, 1
Run = Shell (Var$, vbNormalFocus)
Run = Shell ("mspaint.exe" & txtFileBande.Text, 1)

I need to be able to run files with different extensions like: .jpg, .cdr, .pdf, and so on, so the OS can call the default program for that extension.

Thank you in advance for your time.


Solution

  • The approach I have always used is ShellExecute which will call the default program for an extension. Here is a simplified example of using it:

    Option Explicit
    
    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 Const SW_SHOWNORMAL As Long = 1
    Private Const SW_SHOWMAXIMIZED As Long = 3
    Private Const SW_SHOWDEFAULT As Long = 10
    
    Private Sub Command1_Click()
       ShellExecute 0&, "open", "c:\temp\filename.jpg", vbNullString, vbNullString, SW_SHOWNORMAL
    End Sub