Home All Groups Group Topic Archive Search About

Creating Worksheets from Existing Excel Instances in VB.NET?

Author
1 May 2006 7:47 PM
leeedw
Hi everyone,

I use the following code whenever I export to Excel from VB.NET

               objExcel = New Excel.Application()
               objWorkBooks = objExcel.Workbooks
               objWorkBook = objWorkBooks.Add
               objWorkSheet = objExcel.ActiveWorkbook.ActiveSheet

The problem is that every time I need to export, it results in creating
a new instance of Excel in memory.  Excel is a bit of a resource hog,
and suffers from memory leaks.  Is there a way to open another
worksheet from an Excel process/instance that is already running if
there is one?

How would I do this?

Thanks!

Author
2 May 2006 1:03 AM
+Vice
If objExcel is nothing then
    objExcel = New Excel.Application()
End if

Use that code and you won't generate a new Excel application every time.

<lee***@hotmail.com> wrote in message
Show quoteHide quote
news:1146512847.906148.11950@j33g2000cwa.googlegroups.com...
> Hi everyone,
>
> I use the following code whenever I export to Excel from VB.NET
>
>               objExcel = New Excel.Application()
>               objWorkBooks = objExcel.Workbooks
>               objWorkBook = objWorkBooks.Add
>               objWorkSheet = objExcel.ActiveWorkbook.ActiveSheet
>
> The problem is that every time I need to export, it results in creating
> a new instance of Excel in memory.  Excel is a bit of a resource hog,
> and suffers from memory leaks.  Is there a way to open another
> worksheet from an Excel process/instance that is already running if
> there is one?
>
> How would I do this?
>
> Thanks!
>
Author
2 May 2006 6:33 AM
leeedw
objExcel is not a global variable and only accessible in this function.
If Excel is already opened manually by the user I would like to use
that instance as well.  Is there a way to attach to an existing
process?

+Vice wrote:
Show quoteHide quote
> If objExcel is nothing then
>     objExcel = New Excel.Application()
> End if
>
> Use that code and you won't generate a new Excel application every time.
>
> <lee***@hotmail.com> wrote in message
> news:1146512847.906148.11950@j33g2000cwa.googlegroups.com...
> > Hi everyone,
> >
> > I use the following code whenever I export to Excel from VB.NET
> >
> >               objExcel = New Excel.Application()
> >               objWorkBooks = objExcel.Workbooks
> >               objWorkBook = objWorkBooks.Add
> >               objWorkSheet = objExcel.ActiveWorkbook.ActiveSheet
> >
> > The problem is that every time I need to export, it results in creating
> > a new instance of Excel in memory.  Excel is a bit of a resource hog,
> > and suffers from memory leaks.  Is there a way to open another
> > worksheet from an Excel process/instance that is already running if
> > there is one?
> >
> > How would I do this?
> >
> > Thanks!
> >