ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   GetObject() returns different types of instances (https://www.excelbanter.com/excel-programming/322341-getobject-returns-different-types-instances.html)

Yi

GetObject() returns different types of instances
 
Hi,

I am puzzled by my VB.NET application behavior. In the following code
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' ''''
dim ExcelApp as Excel.Application
ExcelApp = GetObject(Nothing, "Excel.Application")
if (ExcelApp is nothing) then
ExcelApp = CreateObject("Excel.Application")
endi
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' ''''
if there is an Excel process running before running this code, the variable
"ExcelApp" will refer to an instance of type "System.__ComObject". But if
Excel process is not present before running this code, this variable will
point to an instance of "Microsoft.Office.Interop.Excel.ApplicationCla ss".

If "ExcelApp" is pointing to "System.__ComObject", the code
"ExcelApp.Workbooks.Add()" will succeed. But if it is pointing to the other
type, this code will throw an exception which says "Object reference not set
to an instance of an object". Can anybody give some advices? Many thanks in
advance.

Regards,

--
Yi Chu

Juan Pablo González

GetObject() returns different types of instances
 
A short in the dark, what happens if you use

ExcelApp = New Excel.Application

?

--
Regards

Juan Pablo González

"Yi" wrote in message
...
Hi,

I am puzzled by my VB.NET application behavior. In the following code:

'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''
''''''''''''''''''''''''''''
dim ExcelApp as Excel.Application
ExcelApp = GetObject(Nothing, "Excel.Application")
if (ExcelApp is nothing) then
ExcelApp = CreateObject("Excel.Application")
endif

'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''
''''''''''''''''''''''''''''
if there is an Excel process running before running this code, the

variable
"ExcelApp" will refer to an instance of type "System.__ComObject". But if
Excel process is not present before running this code, this variable will
point to an instance of "Microsoft.Office.Interop.Excel.ApplicationCla ss".

If "ExcelApp" is pointing to "System.__ComObject", the code
"ExcelApp.Workbooks.Add()" will succeed. But if it is pointing to the

other
type, this code will throw an exception which says "Object reference not

set
to an instance of an object". Can anybody give some advices? Many thanks

in
advance.

Regards,

--
Yi Chu





All times are GMT +1. The time now is 06:10 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com