Results 1 to 12 of 12

Thread: Equivalent code of CreateObject in VB.net

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Posts
    340

    Question 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
    Last edited by Shohag_ifas; Sep 20th, 2014 at 09:10 PM.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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

  3. #3
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Equivalent code of CreateObject in VB.net

    i have looked over internet and found below solution for c# but not for .net.
    C# is .net

  4. #4
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    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".
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

  5. #5
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Equivalent code of CreateObject in VB.net

    Quote Originally Posted by Shohag_ifas View Post
    ...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

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Posts
    340

    Re: Equivalent code of CreateObject in VB.net

    Quote Originally Posted by DataMiser View Post
    C# is .net
    Pardon me, that's a typing mistake. I mean vb.net not .net. Thanks for the correction.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Posts
    340

    Re: Equivalent code of CreateObject in VB.net

    Quote Originally Posted by .paul. View Post
    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

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Posts
    340

    Re: Equivalent code of CreateObject in VB.net

    Quote Originally Posted by TnTinMN View Post
    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

  9. #9
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Equivalent code of CreateObject in VB.net

    Quote Originally Posted by Shohag_ifas View Post
    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

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Posts
    340

    Re: Equivalent code of CreateObject in VB.net

    Quote Originally Posted by TnTinMN View Post
    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

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Posts
    340

    Exclamation Re: Equivalent code of CreateObject in VB.net

    Quote Originally Posted by TnTinMN View Post
    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
    Name:  1.png
Views: 5017
Size:  9.1 KB

    b) But my reference window says: "The System Cannot Find The Reference Specified"
    Name:  2.jpg
Views: 4906
Size:  14.1 KB

    and after few seconds my vb get crashed

    any help please?

    thanks in advance..

    best regards

  12. #12
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width