Wednesday, June 13, 2007

Run a VB6.0 Executable from Excel/Word

Run an Executable from Excel VBA / Word VBA

If you need to use some grid for showing data / use the feautres in Visual Basic 6.0 that arenot available in VBA, you can create the application in VB6.0 or anyother program and show the User Interface in VBA code


Sub Run_VB6App_FromWord()

--- Some VBA Code here

sCmd = "C:\Program Files\MyFile.exe"
vntResult = OpenProcess(PROCESS_QUERY_INFORMATION, False, Shell(sCmd, 1))
GetExitCodeProcess vntResult, lngExitCode

' -----------------------------------------------------------
' Coded by Shasur for http://vbadud.blogspot.com
' -----------------------------------------------------------

Do
GetExitCodeProcess vntResult, lngExitCode
DoEvents
Loop While lngExitCode = STILL_ACTIVE

--- some more VBA Code

End Sub


The above program will show the MyFile executable till the user clicks OK/Cancel. Once the application is closed the control will return to the calling VBA program

This used WinAPI Functions

Public Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long

Now it depends on how you use the external application. The most often used scenario will be to store the output from the called application (External App) to Registry or Database

1 comment:

  1. Anonymous2:18 PM

    Thanks very much for the code. Just what I was looking for.
    I should just point out, for anyone else looking for this solution, that you also have to declare the correct values for STILL_ACTIVE and PROCESS_QUERY_INFORMATION.

    Here's the complete code that works for me:

    Public Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
    Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long

    Public Const STILL_ACTIVE = &H103
    Public Const PROCESS_QUERY_INFORMATION = &H400

    Sub Run_VB6App_FromWord()

    '--- Some VBA Code here

    sCmd = "C:\test\test.exe"
    vntResult = OpenProcess(PROCESS_QUERY_INFORMATION, False, Shell(sCmd, 1))
    GetExitCodeProcess vntResult, lngExitCode

    ' -----------------------------------------------------------
    ' Coded by Shasur for http://vbadud.blogspot.com
    ' -----------------------------------------------------------

    Do
    GetExitCodeProcess vntResult, lngExitCode
    DoEvents
    Loop While lngExitCode = STILL_ACTIVE

    '--- some more VBA Code

    End Sub

    ReplyDelete

StumbleUpon
Share on Facebook
Related Posts Plugin for WordPress, Blogger...
Download Windows Live Toolbar and personalize your Web experience! Add custom buttons to get the information you care about most.