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