PDA

View Full Version : Working With Excel Objects



MWE
09-11-2010, 08:56 AM
I am running MS Office 2003. I have dozens of VBA procedures that run in Access, Outlook, PowerPoint, Project and Word that open Excel workbooks for information. They all work essentially the same way, typical code is
Dim xlAppl As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlsheet As Excel.Worksheet

Dim xlBookName As String
Dim xlSheetName As String


Set xlAppl = CreateObject("Excel.Application")
Set xlBook = xlAppl.Workbooks.Open _
(xlBookName, UpdateLinks = False)
Set xlsheet = xlBook.Worksheets(xlSheetName)

' do operations


xlCleanUp:
xlBook.Close SaveChanges:=False
xlAppl.Quit
Set xlsheet = Nothing
Set xlBook = Nothing
Set xlAppl = Nothing

All these procedures work fine. But when I do the same type of thing from inside Outlook, I often encounter a problem. The codes runs fine and do exactly what I want. But the Excel instance does not always disappear (as it does with other appls) until I close Outlook. If I run 3 such procedures during an Outlook session, there are normally 3 instances of Excel shown in the Processes window of Task Master. All disappear when I close Outlook.

Any idea what is happening here? Any thoughts on how I can force the Excel instance to actually Quit?

Thanks

JP2112
09-17-2010, 12:19 PM
This can happen if you refer to Excel using objects other than the ones you created. For example, if you use xlAppl in one place, then Excel.Application in another, Outlook will create another instance of Excel.

There's actually a recent article about this on the Access Team Blog, check it out:

http://blogs.msdn.com/b/access/archive/2010/09/16/power-tip-use-explicit-objects-when-referring-to-excel-in-code.aspx

MWE
09-18-2010, 07:39 AM
This can happen if you refer to Excel using objects other than the ones you created. For example, if you use xlAppl in one place, then Excel.Application in another, Outlook will create another instance of Excel.

There's actually a recent article about this on the Access Team Blog, check it out:

http://blogs.msdn.com/b/access/archive/2010/09/16/power-tip-use-explicit-objects-when-referring-to-excel-in-code.aspx Thanks for the reply. I had checked for that type of programming error and that is not the cause. I did some additional testing and found the cause. The problem was the way in which I was passing the identify of the worksheet to a lower level procedure that extracted information from the worksheet and passed that info back. When I passed the actual object instead of the object's name, the multiple instances of Excel problem disappeared.