Excel already runnig, how to update with VB

E

easoftware

I would like to be able to use VB 2003 .NET to check if Excel is
running, and if it is, then do something with the active workbook.

I can see if Excel is running:

Private Sub GetAllExcelApps()

Dim AllExeclProcesses() As Process
Dim OneExcelProcess As Process
Dim OneExcelApp As Excel.Application

' get all open instances of excel
AllExeclProcesses = Process.GetProcessesByName("EXCEL")

' Iterate through the process array.
For Each OneExcelProcess In AllExeclProcesses
' do something
Next
End Sub

But how do I convert OneExcelProcess into OneExcelApp? I can create an
Excel Application instance:

Private Sub MyExcelProcess()
Dim MyExcelApp As Excel.Application = New Excel.Application

' do somthing

MyExcelApp.Quit()
MyExcelApp = Nothing
End Sub

But I do not want a new Excel application. I want to be able to do
something only is Excel is already running.

Thanks
 
G

Guest

I'm not sure if it will work, but maybe try using a direct cast statement or
something similar:

Dim OneExcelApp as Excel.Application
OneExcelApp = DirectCast(OneExcelProcess, Excel.Application)
 
G

Guest

Okay I tried DirectCast and it didn't work. I also tried CType and that
didn't work either.

If you absolutley have to perform something with the Excel file that is
already open, maybe you should use VBA?

A temporary workaround might be to terminate the running Excel process and
then force the user to reselect the file in an OpenFileDialog and then create
a new instance of the workbook that way. Although that is rather lame.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top