Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ruunig an Excell Program From VB6

Status
Not open for further replies.

Bitaoo

Programmer
Dec 18, 2002
81
0
0
US
Hello guys,

I need your help. I want to invoke an excel file named "Main2.xls" from my VB6
program but first of all I need to check if this XLS file is running already or not.
So if it is running I won't run it again. How can I do that?

Thanks for your help,
--Bita
 
Thanks for your response. I tried this:

Set MyXL = GetObject("C:\Main2.xls", "Excel.Application")

but if it is running or not. It returns this error:

File name or class name not found during Automation operation

Do you know why? The file exists.
--Bita
 
Set MyXL = GetObject("C:\Main2.xls", "Excel.Application")
should be:
Set MyXL = GetObject("C:\Main2.xls", "Excel.Sheet")

by the way, if you just need to check for a specific file,
try this:

Dim myXL As Object

On Error Resume Next

'no need to specify class name
Set myXL = GetObject("c:\Main2.xls")

If Err.Number <> 0 Then
Debug.Print &quot;Not Exists&quot;
Err.Clear
Else
Debug.Print &quot;Exists&quot;
End If

myXL.Application.Quit

...Have Fun!
 
Thanks, but I don't want to know that the file exists
or not. I want to know if it is running or not. It doesn't
work. :(

--Bita
 
...sorry for the misunderstanding. try this:

Dim myXL As Excel.Application
Dim FileName As String

On Error Resume Next

FileName = &quot;C:\Main2.xls&quot;

Set myXL = GetObject(, &quot;Excel.Application&quot;)

Debug.Print myXL.ActiveWorkbook.FullName
If Err.Number <> 0 Then
Debug.Print &quot;Not Running&quot;
Err.Clear
Else
If myXL.ActiveWorkbook.FullName = FileName Then
Debug.Print &quot;Running&quot;
Else
Debug.Print &quot;Not Running&quot;
End If
End If
 
Thanks a lot. It works. :)

But I have another question, now how can I find that
a VB6 program e.g. named &quot;myProg.exe&quot; is running or not?

Thanks again,
--Bita
 
...does this check happen when you run another instance of myProg.exe or different vb instance?

Have Fun!
 
Sorry about that. :( I mean different VB instance.
 
If this is two instances of the same vb program, then test for App.PrevInstance = True in the sub Main or Form Load event.


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
...for checking if an application is running from another vb app, there are atlease two ways to do it.

1- using pslist from pstools. here's a link to download it:

...from vb use shell command to execute the pslist command and pass the name of the app that you want to check and output the results to a text file. then, write some codes to evaluate the results from that text file.
ex:
shell &quot;pslist temp.exe > c:\temp.txt&quot;

2- using WMI ActiveX. here's a link to download it:

...in vb try this:

Dim strAppName As String
Dim strComputer As String
Dim objWMIService As Object
Dim colProcesses, objProcess

strAppName = &quot;temp.exe&quot;
strComputer = &quot;.&quot;

Set objWMIService = GetObject(&quot;winmgmts:&quot; _
& &quot;{impersonationLevel=impersonate}!\\&quot; & strComputer & &quot;\root\cimv2&quot;)

Set colProcesses = objWMIService.ExecQuery(&quot;select * from win32_process where name = '&quot; & strAppName & &quot;'&quot;)
If colProcesses.Count > 0 Then
Debug.Print strAppName & &quot; is running&quot;
Else
Debug.Print strAppName & &quot; is not running&quot;
End If


Have Fun!
 
Sorry, but it made another question for me, if the
VB6 program is running, how can I activate its screen?

Thanks,
--Bita
 
...it can be done with several API function or just make it simplier, use AppActivate statement from within VB like this:

AppActivate &quot;temp&quot;, 1

...Have Fun!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top