Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between GetObject and New

I usually create an Excel (or ppt) object like:

Imports Microsoft.Office.Interop

Private _xlApp as Excel.Application

_xlApp = CType(GetObject(, "Excel.Application"), Excel.Application)
...

now checking something on msdn I saw in an example they are doing it this way:

Imports Excel = Microsoft.Officee.Interop.Excel

Dim xlApp as Excel.Application = Nothing
xlApp = New Excel.Application

Could anyone explain the difference between these two methods since I want to apply it right and cannot see the differences.

like image 790
ruedi Avatar asked Jan 01 '26 06:01

ruedi


1 Answers

in the first case, you're just getting a COM object by name and casting it to its own Type. really only useful to instance objects from a file library which isn't registered for COM interop, or to get some other object from a file. i suppose you can also use it to get the instance of a running application.

hmm, it seems you can load excel files as objects directly from file: http://msdn.microsoft.com/en-us/library/e9waz863%28v=vs.90%29.aspx

in the second case, you're just referencing the managed interop library and creating the object directly. http://msdn.microsoft.com/en-us/library/aax7sdch%28v=vs.110%29.aspx

like image 93
porkchop Avatar answered Jan 06 '26 10:01

porkchop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!