Home All Groups Group Topic Archive Search About

Excel already runnig, how to update with VB

Author
3 Aug 2006 4:10 AM
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

Author
3 Aug 2006 2:51 PM
John
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)





Show quoteHide quote
"easoftw***@gmail.com" wrote:

> 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
>
>
Author
3 Aug 2006 6:25 PM
John
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.


Show quoteHide quote
"John" wrote:

> 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)
>
>
>
>
>
> "easoftw***@gmail.com" wrote:
>
> > 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
> >
> >