0

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:

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 .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?

Community
  • 1
  • 1
Zakir_SZH
  • 466
  • 7
  • 21
  • 2
    The equivalen of VB6 `CreateObject` is... `CreateObject`. You'll find it in the `Microsoft.VisualBasic` namespace. However, you should add a reference to the MS Excel Object library to ensure strongly-typed variables. – Bjørn-Roger Kringsjå Sep 20 '14 at 18:36
  • @Bjørn-RogerKringsjå, thanks sir, but i don't want to use Microsoft.VisualBasic namespace as that's not they right way to learn vb.net properly. I have overcome many issues without that name space and now stuck with that. – Zakir_SZH Sep 20 '14 at 18:38
  • Hallelujah! You're on the right path. Go to project > add reference > com > Microsoft Excel (version) Object Library. If you don't find it, then search and download. – Bjørn-Roger Kringsjå Sep 20 '14 at 18:41
  • @Bjørn-RogerKringsjå, thanks again sir, however please note "Microsoft Excel (version) Object" that the issue why i didn't add direct reference as user may not have same version office/excel library installed :( (may be i am wrong) but how to use my code regardless what version user have installed on their pc? – Zakir_SZH Sep 20 '14 at 18:47
  • 1
    @user3614957 You seem to be confusing `Microsoft.VisualBasic` namespace with `Microsoft.VisualBasic.Compatibility` namespace. You might avoid the latter, but the former is the proper VB that contains many actual VB features and functions. If you intend to avoid that, you might as well avoid VB as a language in general. – GSerg Sep 20 '14 at 21:55
  • Without using VB's `CreateObject()` the code is the same as in C#. You just need to adjust the syntax and add a few `Using`s. – Deanna Sep 23 '14 at 11:44

3 Answers3

3

VB.Net way, no late binding as you can create the objects directly from the library. Clean them up with the Marshal class since in it a COM object - in reverse order.

Dim objXLS As New Excel.Application
Dim objWorkBook As Excel.Workbook = objXLS.Workbooks.Open("Excel File Goes Here")
objXLS.Visible = False
'work with file
objWorkBook.SaveAs strCurPath & "\Temp.csv", 6
objWorkBook.Close 2
objXLS.Quit
Marshall.FinalReleaseComObject(objWorkBook)
Marshall.FinalReleaseComObject(objXLS)
OneFineDay
  • 9,004
  • 3
  • 26
  • 37
  • 1
    I'll give you a +1 solely on `FinalRealeaseComObject` ;) – Bjørn-Roger Kringsjå Sep 20 '14 at 18:39
  • @OneFineDay, thanks sir, but i don't like to use the that way as end user may or may not have excel installed. (may be i am wrong, but i using createobject funcion and use a error handler to fire up if excel is not installed). – Zakir_SZH Sep 20 '14 at 18:40
  • I didn't think `CreateObject` would work without it either - so your saying it does? – OneFineDay Sep 20 '14 at 21:15
  • @OneFineDay, thanks again sir, 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? – Zakir_SZH Sep 21 '14 at 02:50
  • You will need to research which library goes with which version of excel. If the user does not have excel it will not work - how could it the wrapper is the engine so it still needs the foundation. Once the library is in place and you import the namespace you will see Excel and it's hierarchy of classes. – OneFineDay Sep 21 '14 at 03:08
  • Use CreateObject. You always operate on the latest version on the end users machine. With late binding you don't need to know about the obect. If you create objects from every file in a folder with CreateObject you can attempt to print all of them. Any with a print command will print. Even if you have never heard of that file. – Noodles Sep 22 '14 at 23:48
1

With one formatting exception, vbscript and VB6 code will run in VB.NET. The exception is all methods and function/subs require brackets (rather than only ones that return a value). So CreateObject works VBScript/VB6, and VB.NET. In VB.Net you need to have these lines to use COM,

Imports System
Imports System.Runtime.InteropServices

CreateObject uses IDispatch (aka Automation). You don't need to know what an object is to use it.

Noodles
  • 1,981
  • 1
  • 11
  • 4
0

Another option for late binding in VB (but you should seriously consider the non-late-binding answer already given instead

Dim objXLS As Object = System.Activator.CreateInstance(System.Type.GetTypeFromProgID("Excel.Application"))

With either CreateObject or CreateInstance, you need to have "Option Strict Off".

Dave Doknjas
  • 6,394
  • 1
  • 15
  • 28