Results 1 to 22 of 22

Thread: execute other program from vb

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Posts
    172

    execute other program from vb

    hi all,

    i want to do a vb program that will automatic execute other software exe file. for example in my vb i have 3 command button. when click command button 1 will execute microsoft word, click command 2 will execute notepad, click command button 3 will execute adobe reader. how to do it? what is the coding? help...plz...

    mic

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: execute other program from vb

    Have a search for SHELL and/or SHELLEXECUTE.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: execute other program from vb

    ShellExecute is a bit more powerful and also handles spaces in the file paths without issue.

    Also, ShellExecute will open any file passed in its default associatted program, Shell will not and will require the filepath and name of the application passed as well.

    Code:
    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_HIDE As Long = 0
    Private Const SW_SHOWNORMAL As Long = 1
    Private Const SW_SHOWMAXIMIZED As Long = 3
    Private Const SW_SHOWMINIMIZED As Long = 2
    
    Private Sub Command1_Click()
        ShellExecute Me.hwnd, "open", "C:\Users\MyUserName\Documents\Document1.doc", vbNullString, "C:\", SW_SHOWNORMAL
    End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Posts
    172

    Re: execute other program from vb

    for exe file, i use the coding below.

    Shell "notepad.exe", vbNormalFocus
    or coding by robdog888 and i found it work fine.

    however, for other type file like .doc, .jpg, i need to use robdog888 coding. however, this coding i have little problem. if my vb program i use in other pc, i need to change the location of the file each time i use at different pc. is there anyway to made the vb program work fine at every pc?

    (for example at every pc has a file aaa.jpg at "C:\" but different folder.)
    Last edited by mic_k86; Dec 20th, 2008 at 03:17 AM.

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: execute other program from vb

    Yes, use the App.Path function. It returns the path where the executing assembly is in. So if your exe is located in "C:\MyFolder" then the App.Path will return that path. You can append that with your file name and dynamically build the path/location.

    Code:
    Private Sub Command1_Click()
        ShellExecute Me.hwnd, "open", App.Path & "\Document1.doc", vbNullString, "C:\", SW_SHOWNORMAL
    End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Posts
    172

    Re: execute other program from vb

    Is that like this?
    ShellExecute Me.hwnd, "open", C:\my documents\project\ & "\Document1.doc", vbNullString, "C:\", SW_SHOWNORMAL

  7. #7
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: execute other program from vb

    Nope, that would be like this:
    Code:
    ShellExecute Me.hwnd, "open", App.Path & "\Document1.doc", vbNullString, "C:\", SW_SHOWNORMAL
    App.Path isn't something you have to fill in. It's an environment variable, meaning VB assigns it value itself.

    Easier explained:
    When you run your program Windows tells it: "Your location is C:\My Project".
    And your program replaces App.Path with the appropriate folder.
    Delete it. They just clutter threads anyway.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Posts
    172

    Re: execute other program from vb

    Private Sub Command3_Click()
    ShellExecute Me.hwnd, "open", App.Path & "\WebCam.exe", vbNullString, "C:\", SW_SHOWNORMAL
    End Sub
    i put the code like this. but when i click the command button, no function at all. how?
    Last edited by mic_k86; Dec 23rd, 2008 at 09:45 AM.

  9. #9
    Lively Member
    Join Date
    Jan 2006
    Posts
    75

    Re: execute other program from vb

    When you execute exes using ShellExecute, you need to set lpDirectory to the last folder that your exe is in. So, using your example, "C:\" (the second last parameter) needs to be replaced with App.Path.
    Code:
    ShellExecute Me.hwnd, "open", App.Path & "\WebCam.exe", vbNullString, App.Path & "\", SW_SHOWNORMAL
    Extra explanation:
    If your exe was inside another folder inside App.Path (for example, App.Path & "\Extras\WebCam.exe") then you would have to set lpDirectory to this folder (in my example, the Extras folder):
    Code:
    ShellExecute Me.hwnd, "open", App.Path & "\Extras\WebCam.exe", vbNullString, App.Path & "\Extras\", SW_SHOWNORMAL

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Posts
    172

    Re: execute other program from vb

    following is the full link of my exe file
    C:\Documents and Settings\mic_k86\Desktop\underwater sensor network for monitoring system software\WebCam v3\WebCam.exe
    and following link will by same at each pc
    \Desktop\underwater sensor network for monitoring system software\WebCam v3\WebCam.exe
    i hv try put the app.path until extension in 2nd quote, still not work.

  11. #11
    Lively Member
    Join Date
    Jan 2006
    Posts
    75

    Re: execute other program from vb

    No, App.Path won't work for that situation. This is a totally different problem.
    http://www.vbforums.com/showpost.php...32&postcount=2
    This post shows GetSpecialFolder using the My Documents folder as an example. Line 3 contains the constant for My Documents, but what you want is the Desktop. The constant for Desktop can be found here:
    http://www.vbforums.com/showpost.php...06&postcount=5
    After replacing line 3 from the first link with the list from the second link, you can store the path to the desktop in a string and then replace App.Path with the string.
    Code:
    Dim strDesktop As String
    strDesktop = GetSpecialfolder(CSIDL_DESKTOP)
    ShellExecute Me.hwnd, "open", strDesktop & "\underwater sensor network for monitoring system software\WebCam v3\WebCam.exe", vbNullString, strDesktop & "\underwater sensor network for monitoring system software\WebCam v3\", SW_SHOWNORMAL

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: execute other program from vb

    As I had stated, App.Path is for retrieving the location of where your executing assembly is. The lpDirectory is the Working directory which can be different then the location of the executable.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Posts
    172

    Re: execute other program from vb

    thanks everone!!! i get it. but here i need some extra help:

    at my main vb program, i have three command button to execute three different exe file. when i close the main vb program i plan to close all the three exe file together. how to do it?

  14. #14
    Lively Member
    Join Date
    Jan 2006
    Posts
    75

    Re: execute other program from vb

    The lpDirectory is the Working directory which can be different then the location of the executable.
    Ok, the location of the executable is the only place that it has worked for me with exes.

    mic_k86, for your new question, I have no idea how to do this. You might want to try searching the forums for "Terminate Process" (as RobDog888 suggested in this thread).

  15. #15
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: execute other program from vb

    You pass the full path to the exe and then you can set a different path for the working directory. If you only pass the exe name and set a different working directory then the api will look in the working directory for hte exe.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Posts
    172

    Re: execute other program from vb

    hi all, i hv search on the terminate process, but i cant find any related link for my problem. i hv find some killing vb coding in terminate process but after i killed my main vb program, the three exe file still running there. anyone can help?

  17. #17
    Addicted Member
    Join Date
    Jul 2007
    Posts
    228

    Re: execute other program from vb


  18. #18
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: execute other program from vb

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Posts
    172

    Re: execute other program from vb

    Code:
    Option Explicit
    
    Private Const CSIDL_DESKTOP = &H0
    Private Const NOERROR = 0
    
    Private Type SHTEMID
        cb As Long
        abID As Byte
    End Type
    Private Type ITEMIDLIST
        mkid As SHTEMID
    End Type
    
    Private Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As ITEMIDLIST) As Long
    
    Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
    
    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
    
    
    'disable desktop icon
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    Private Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
    
    Private Const SW_HIDE As Long = 0
    Private Const SW_SHOWNORMAL As Long = 1
    Private Const SW_SHOWMAXIMIZED As Long = 3
    Private Const SW_SHOWMINIMIZED As Long = 2
    
    Private Test As String
    Dim strDesktop As String
    
    Private Sub Command1_Click()
    
    strDesktop = GetSpecialfolder(CSIDL_DESKTOP)
    ShellExecute Me.hWnd, "open", strDesktop & "\underwater sensor network for monitoring system software\PhidgetPHSensor(seconds selection) v5\ph sensor.exe", vbNullString, strDesktop & "\underwater sensor network for monitoring system software\PhidgetPHSensor(seconds selection) v5\", SW_SHOWNORMAL
    
    End Sub
    
    Private Sub Command2_Click()
    
    strDesktop = GetSpecialfolder(CSIDL_DESKTOP)
    ShellExecute Me.hWnd, "open", strDesktop & "\underwater sensor network for monitoring system software\temp\temp.exe", vbNullString, strDesktop & "\underwater sensor network for monitoring system software\temp\", SW_SHOWNORMAL
    
    End Sub
    
    Private Sub Command3_Click()
    
    strDesktop = GetSpecialfolder(CSIDL_DESKTOP)
    ShellExecute Me.hWnd, "open", strDesktop & "\underwater sensor network for monitoring system software\WebCam v5\WebCam.exe", vbNullString, strDesktop & "\underwater sensor network for monitoring system software\WebCam v5\", SW_SHOWNORMAL
    
    End Sub
    
    Private Sub Command4_Click()
        ' restore wallpaper
        Wallpaper = Test
        'To Show The Desktop Use This Code
    Dim hWnd As Long
    hWnd = FindWindowEx(0&, 0&, "Progman", vbNullString)
    ShowWindow hWnd, 5
    
    Unload Me
    End Sub
    
    Private Sub Form_Load()
    Debug.Print "Desktop path: " & GetSpecialfolder(CSIDL_DESKTOP)
        ' remove wallpaper
        Test = Wallpaper
        Wallpaper = GetSpecialfolder(CSIDL_DESKTOP) & "\underwater sensor network for monitoring system software\Underwater.bmp"
    'To Hide Desktop Use This Code
    Dim hWnd As Long
    hWnd = FindWindowEx(0&, 0&, "Progman", vbNullString)
    ShowWindow hWnd, 0
    End Sub
    
    Private Function GetSpecialfolder(CSIDL As Long) As String
    Dim r As Long
    Dim IDL As ITEMIDLIST
    Dim Path$
    
        'Get the special folder
        r = SHGetSpecialFolderLocation(100, CSIDL, IDL)
            If r = NOERROR Then
                'Create a buffer
                Path$ = Space$(512)
                'Get the path from the IDList
                r = SHGetPathFromIDList(ByVal IDL.mkid.cb, ByVal Path$)
                'Remove the unnecessary chr$(0)'s
                GetSpecialfolder = Left$(Path, InStr(Path, Chr$(0)) - 1)
                Exit Function
        End If
        GetSpecialfolder = ""
    End Function
    
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
        ' restore wallpaper
        Wallpaper = Test
        Unload Me
    End Sub
    attached is my full vb coding. i failed to kill the exe based on link given above post. help...plz...

  20. #20
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: execute other program from vb

    I cannot see anywhere in your code how you utilized the link provided by RobDog888.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  21. #21

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Posts
    172

    Re: execute other program from vb

    dee-u, i have got try but cant. so i no post it. i post my code hope that anyone can help me troubleshoot it.

  22. #22
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: execute other program from vb

    You should try it again then tell us what is not working.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width