Solved

Calling other applications within VB

Posted on 2000-04-06
8
162 Views
Last Modified: 2010-05-02
I have written 2 Vb projects and i want to call one from the other.  Is this easy and how is it done?
0
Comment
Question by:CRICE
8 Comments
 
LVL 32

Expert Comment

by:Erick37
ID: 2690401
Do you want to start the other application, or do you want the applications to be able to communicate with each other?
0
 
LVL 20

Expert Comment

by:hes
ID: 2690524
In a module put:

Public Type STARTUPINFO
    cb As Long
    lpReserved As String
    lpDesktop As String
    lpTitle As String
    dwX As Long
    dwY As Long
    dwXSize As Long
    dwYSize As Long
    dwXCountChars As Long
    dwYCountChars As Long
    dwFillAttribute As Long
    dwFlags As Long
    wShowWindow As Integer
    cbReserved2 As Integer
    lpReserved2 As Long
    hStdInput As Long
    hStdOutput As Long
    hStdError As Long
End Type

Public Type PROCESS_INFORMATION
    hProcess As Long
    hThread As Long
    dwProcessID As Long
    dwThreadID As Long
End Type

Public Declare Function WaitForSingleObject Lib _
"kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds _
As Long) As Long

Public Declare Function CreateProcessA Lib "kernel32" _
(ByVal lpApplicationName As Long, ByVal lpCommandLine As _
String, ByVal lpProcessAttributes As Long, ByVal _
lpThreadAttributes As Long, ByVal bInheritHandles As Long, _
ByVal dwCreationFlags As Long, ByVal lpEnvironment As Long, _
ByVal lpCurrentDirectory As Long, lpStartupInfo As _
STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) _
As Long

Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject _
As Long) As Long

Public Const NORMAL_PRIORITY_CLASS = &H20&
Public Const INFINITE = -1&

In your code where you want to start the other application put:

Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO

'Initialize the STARTUPINFO structure:
start.cb = Len(start)

'Start the shelled application:
ret& = CreateProcessA(0&, "Your Other Program", 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)

'Wait for the shelled application to finish:
ret& = WaitForSingleObject(proc.hProcess, INFINITE)
ret& = CloseHandle(proc.hProcess)
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2690551
Well, if you just want to start it, why not use:

Call Shell("c:\Project2.exe", vbNormalFocus)
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 3

Expert Comment

by:ashunigam
ID: 2690774
Are these standard EXE or ActiveX?
0
 

Expert Comment

by:AshokKumar
ID: 2692260
Yeah hes. y all these codings (Smile. Of course u have done a great job but . . . ) Erick37's code is enough to start an application. Or if u need to start a new instance of an existing application u can use this:

First give the reference in Project->reference
Then

Dim obj as object
Set Obj = CreateObject("MsWord.Application")

0
 

Author Comment

by:CRICE
ID: 2692598
Thank you all for your help, just one more thing before i allocate the points, what if i wanted to pass information from one application to the other. Like a parameter list.  
0
 
LVL 32

Accepted Solution

by:
Erick37 earned 50 total points
ID: 2693249
Your application can process command line parameters.

eg, start the program like this

Call Shell("c:\Project2.exe x y z", vbNormalFocus)

In Projext2's form load you can read the command line switch(es) like this:

Dim sCmdLine As String
sCmdLine = Command

sCmdLine will contain the string "x y z" which you can parse using the Split function.

VB help has an example of reading the command line, search for "Command Line."
0
 

Author Comment

by:CRICE
ID: 2693264
Thanks very much for your help, it's helped me loads!!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question