Equivalent code of CreateObject in VB.net
Greetings everyone
i am trying to bind/use objects in vb.net like excel.application etc etc. I am mainly a vb6 coder and now shifting and learning vb.net.
in vb6 i can easily handle that by using createobject function
here is the vb6 code:
Code:
Dim objXLS As Object
Dim objWorkBook As Object
Set objXLS = CreateObject("Excel.Application")
objXLS.Visible = False
Set objWorkBook = objXLS.Workbooks.Open("Excel File Goes Here")
objWorkBook.SaveAs strCurPath & "\Temp.csv", 6
objWorkBook.Close 2
objXLS.Quit
Set objWorkBook = Nothing
Set objXLS = Nothing
i have looked over internet and found below solution for c# but not for VB.net. and i failed to use dynamic type/command with vb.net.
here is the link:
Equivalent code of CreateObject in C#
there is also messy way.. but i like to go with the easy way (label binding or so)
so, is the any way to use dynamic key to use in vb.net or what is the Equivalent in vb.net?
thanks in advance
best regards
Re: Equivalent code of CreateObject in VB.net
Here's how to write that in vb.net with late binding. You need Option Strict off for late binding:
Code:
Dim objXLS As Object
Dim objWorkBook As Object
objXLS = CreateObject("Excel.Application")
objXLS.Visible = False
objWorkBook = objXLS.Workbooks.Open("Excel File Goes Here")
objWorkBook.SaveAs(strCurPath & "\Temp.csv", 6)
objWorkBook.Close(2)
objXLS.Quit()
objWorkBook = Nothing
objXLS = Nothing
Re: Equivalent code of CreateObject in VB.net
Quote:
i have looked over internet and found below solution for c# but not for .net.
C# is .net
Re: Equivalent code of CreateObject in VB.net
Another option:
Code:
Dim objXLS As Object = System.Activator.CreateInstance(System.Type.GetTypeFromProgID("Excel.Application"))
Either way (CreateObject or CreateInstance), you need to have "Option Strict Off".
Re: Equivalent code of CreateObject in VB.net
Quote:
Originally Posted by
Shohag_ifas
...i am trying to bind/use objects in vb.net like excel.application etc etc. I am mainly a vb6 coder and now shifting and learning vb.net.
in vb6 i can easily handle that by using createobject function. [/URL]
there is also messy way.. but i like to go with the easy way (label binding or so)
so, is the any way to use dynamic key to use in vb.net or what is the Equivalent in vb.net?
As the others have shown you can either use the VB CreateObject or the .Net System.Activator class to create an instance of Excel or any other registered class. CreateObject is just a helper method that calls System.Activator with a few checks through in to mix.
Unfortunately, there is no VB.Net equivalent of the C# Dynamic keyword. In VB.Net, it is an all or nothing approach to enabling late binding. The only granularity that can be imposed is at the code file level where you use Option Strict Off to enable late binding or Option Strict On to prevent late binding. All variable declaration syntax remains the same either way.
In the case of Excel or any Office application, it is advisable to make use of the primary interop assemblies (PIA's) to facilitate early binding. Please refer to How to automate Microsoft Excel from Visual Basic .NET for instructions.
Starting with VB2010, the option exists to embed the PIA information in the .Net assembly. In many cases this is not an issue, but I have seen some cases where it appears to cause problems. The PIA's either need to be registered on the computer or you can included then as Dll's instaled along with your program. It is advisable to either use the embedded option or package the Dll's with you application as not all systems may have them installed in the GAC.
As far as I know, the only version related issue with using the PIA's is during the creation of the application object. This is where the CreateObject method can be useful.
Standard Excel PIA instantiation:
Code:
Dim app As New Excel.Application
Using System.Activator or GetActiveObject:
Code:
Private Shared ExcelType As Type = Type.GetTypeFromProgID("Excel.Application")
Private Function GetExcelApplication(Optional ByVal createNewInstance As Boolean = True) As Excel.Application
Dim app As Excel.Application
If Not createNewInstance Then
app = DirectCast(System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application"), Excel.Application)
End If
If app Is Nothing Then
app = DirectCast(Activator.CreateInstance(ExcelType), Excel.Application)
End If
Return app
End Function
There can be issues with releasing COM Objects especially those related to Office Automation. Please refer to this for an overview.
Releasing COM objects in .NET from within enumerations and other reference scenarios
Re: Equivalent code of CreateObject in VB.net
Quote:
Originally Posted by
DataMiser
C# is .net
Pardon me, that's a typing mistake. I mean vb.net not .net. Thanks for the correction.
Re: Equivalent code of CreateObject in VB.net
Quote:
Originally Posted by
.paul.
Here's how to write that in vb.net with late binding. You need Option Strict off for late binding:
Code:
Dim objXLS As Object
Dim objWorkBook As Object
objXLS = CreateObject("Excel.Application")
objXLS.Visible = False
objWorkBook = objXLS.Workbooks.Open("Excel File Goes Here")
objWorkBook.SaveAs(strCurPath & "\Temp.csv", 6)
objWorkBook.Close(2)
objXLS.Quit()
objWorkBook = Nothing
objXLS = Nothing
thanks for your help though. But i don't want to use "option strict off". I know there is other way around. Please check out the link.
Here is the example code i am trying but facing issue with setting values as object ;(
Code:
ExcelType.InvokeMember("Visible", BindingFlags.SetProperty, null,
ExcelInst, new object[1] {true});
thanks again for your reply
Re: Equivalent code of CreateObject in VB.net
Quote:
Originally Posted by
TnTinMN
As the others have shown you can either use the VB CreateObject or the .Net System.Activator class to create an instance of Excel or any other registered class. CreateObject is just a helper method that calls System.Activator with a few checks through in to mix.
Unfortunately, there is no VB.Net equivalent of the C# Dynamic keyword. In VB.Net, it is an all or nothing approach to enabling late binding. The only granularity that can be imposed is at the code file level where you use
Option Strict Off to enable late binding or
Option Strict On to prevent late binding. All variable declaration syntax remains the same either way.
There can be issues with releasing COM Objects especially those related to Office Automation. Please refer to this for an overview.
Releasing COM objects in .NET from within enumerations and other reference scenarios
thanks a lot for your long response..
however sir, few question:
1. I don't see Excel.Application as new object in vb.net. Do i need to add COM reference to MS Excel Object Library? if so then:
a) I have Version 11 (Office 2003), but what if end user have office 2002/2007/2010 or even 2013?
b) will all version works as is though i have added Version 2003 library?
c) what if end user don't have ms excel at all?
2. How to trigger a message without runtime error if excel is not installed on user computer?
thanks in advance
take care
may Allah bless you
have a nice day
bye bye
Re: Equivalent code of CreateObject in VB.net
Quote:
Originally Posted by
Shohag_ifas
thanks a lot for your long response..
however sir, few question:
1. I don't see Excel.Application as new object in vb.net. Do i need to add COM reference to MS Excel Object Library? if so then:
a) I have Version 11 (Office 2003), but what if end user have office 2002/2007/2010 or even 2013?
b) will all version works as is though i have added Version 2003 library?
c) what if end user don't have ms excel at all?
2. How to trigger a message without runtime error if excel is not installed on user computer?
Please re-read what I wrote, I covered everything you have just asked except for detecting if Excel is installed. I can not guaranty that the application will work for the latest Office versions as I believe MS has been changing the Office Object Model, But I believe you should be good through Office 2010. The Link I provided to: How to automate Microsoft Excel from Visual Basic .NET was written around Excel 2003.
You can detect if Excel is installed by checking is ExcelType in this statement is Nothing.
Code:
Private Shared ExcelType As Type = Type.GetTypeFromProgID("Excel.Application")
If it is Nothing, then Excel is not installed.
If you want to get the Office 2003 PIAs, they can be downloaded from here:
Office 2003 Update: Redistributable Primary Interop Assemblies
Re: Equivalent code of CreateObject in VB.net
Quote:
Originally Posted by
TnTinMN
Please re-read what I wrote, I covered everything you have just asked except for detecting if Excel is installed. I can not guaranty that the application will work for the latest Office versions as I believe MS has been changing the Office Object Model, But I believe you should be good through Office 2010. The Link I provided to:
How to automate Microsoft Excel from Visual Basic .NET was written around Excel 2003.
You can detect if Excel is installed by checking is ExcelType in this statement is Nothing.
Code:
Private Shared ExcelType As Type = Type.GetTypeFromProgID("Excel.Application")
If it is Nothing, then Excel is not installed.
If you want to get the Office 2003 PIAs, they can be downloaded from here:
Office 2003 Update: Redistributable Primary Interop Assemblies
thanks for your reply sir.. but you didn't answer my question directly.
1. do i need to add com reference to use ?
2. yes you answered how to detect if excel is installed :) thanks
3. sir, did you check stackoverflow link. There is way without com reference to handle but can you help me InvokeMember function. a) how to set properlty, how to call a method with multiple parameters/arguments?
thanks again for your kind help
2 Attachment(s)
Re: Equivalent code of CreateObject in VB.net
Quote:
Originally Posted by
TnTinMN
Please re-read what I wrote,
Dear sir, i just tried to add excel com interface. But i got error. :(
check the image below:
a) I select MS Excel Object Library
Attachment 118943
b) But my reference window says: "The System Cannot Find The Reference Specified"
Attachment 118945
and after few seconds my vb get crashed :(
any help please?
thanks in advance..
best regards
Re: Equivalent code of CreateObject in VB.net
This appears to me to be a corrupted installation problem, but that is only a guess. You could try to perform a repair install of the PIAs.
Here is some reference material: Installing and Using the Office 2003 Primary Interop Assemblies