vb.net VB.net中CreateObject的等效代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25951997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:15:14  来源:igfitidea点击:

Equivalent code of CreateObject in VB.net

c#.netvb.netobjectvb6

提问by Zakir_SZH

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.

我正在尝试绑定/使用 vb.net 中的对象,如 excel.application 等。我主要是一名 vb6 编码员,现在正在转移和学习 vb.net。

in vb6 i can easily handle that by using createobject function

在 vb6 中,我可以使用 createobject 函数轻松处理

here is the vb6 code:

这是vb6代码:

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.

我查看了互联网并找到了以下 c# 解决方案,但不是 .net 解决方案。我未能在 vb.net 中使用动态类型/命令。

here is the link:

链接在这里:

Equivalent code of CreateObject in C#

C#中CreateObject的等效代码

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 中使用动态密钥,或者 vb.net 中的等效项是什么?

回答by OneFineDay

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.

VB.Net 方式,没有后期绑定,因为您可以直接从库中创建对象。用 Marshal 类清理它们,因为它是一个 COM 对象 - 以相反的顺序。

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)

回答by Noodles

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,

除了一个格式化例外,vbscript 和 VB6 代码将在 VB.NET 中运行。例外是所有方法和函数/子都需要括号(而不仅仅是那些返回值的)。所以 CreateObject 可以在 VBScript/VB6 和 VB.NET 中工作。在 VB.Net 中,您需要有这些行才能使用 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.

CreateObject 使用 IDispatch(又名自动化)。你不需要知道一个对象是什么来使用它。

回答by Dave Doknjas

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

VB 中后期绑定的另一种选择(但您应该认真考虑已经给出的非后期绑定答案

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

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

无论是 CreateObject 还是 CreateInstance,您都需要“Option Strict Off”。