是否可以将 VBA 转换为 C#?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/388819/
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-08-04 01:34:07  来源:igfitidea点击:

Is it possible to convert VBA to C#?

c#vbams-accessaccess-vba

提问by tksy

I have a few modules of block of code in VBA to run on few Access databases. I would like to know how I should proceed if I want to convert the coding to C# environment. And is it possible to implement and obtain the same results as I am getting now with Access and VBA? I am completely new to C# at this point.

我在 VBA 中有几个代码块模块可以在几个 Access 数据库上运行。如果我想将编码转换为 C# 环境,我想知道应该如何进行。是否有可能实现并获得与我现在使用 Access 和 VBA 获得的结果相同的结果?在这一点上,我对 C# 完全陌生。

采纳答案by Dave R.

Automatic conversion isn't possible at the moment, but doing it manually will also help improve your C# skills. There's a Top 10 article here that takes you through the common differences:

目前无法自动转换,但手动转换也有助于提高您的 C# 技能。此处有一篇 Top 10 文章,可带您了解常见差异:

http://msdn.microsoft.com/en-us/library/aa164018%28office.10%29.aspx

http://msdn.microsoft.com/en-us/library/aa164018%28office.10%29.aspx

You may also find the following links useful:

您可能还会发现以下链接很有用:

The MSDN page for developing Office solutions with C#:

使用 C# 开发 Office 解决方案的 MSDN 页面:

http://msdn.microsoft.com/en-us/library/ms228286.aspx

http://msdn.microsoft.com/en-us/library/ms228286.aspx

The MSDN Visual C# application development page (for starting out in C# development):

MSDN Visual C# 应用程序开发页面(用于开始 C# 开发):

http://msdn.microsoft.com/en-us/library/aezdt881.aspx

http://msdn.microsoft.com/en-us/library/aezdt881.aspx

Good luck and I hope this helps.

祝你好运,我希望这会有所帮助。

回答by Dave Markle

Generally, you should be able go convert the code manually. You won't find any automated code converters that will do it.

通常,您应该可以手动转换代码。您不会找到任何可以执行此操作的自动代码转换器。

That being said, the frameworks which you use to access data are quite different in the C# and VBA worlds (they're even quite different betwween VB.NET and VBA!). So you're going to want to read up on ADO.NET before you get started.

话虽如此,您用来访问数据的框架在 C# 和 VBA 世界中是完全不同的(它们甚至在 VB.NET 和 VBA 之间也完全不同!)。因此,在开始之前,您将需要阅读 ADO.NET。

回答by Eric Ness

One thing to be aware of is that some object name spaces and library references are included automatically when you are coding in VBA. These need to be explicitly added when working in C#. For example,

需要注意的一件事是,当您在 VBA 中编码时,会自动包含一些对象名称空间和库引用。在 C# 中工作时需要显式添加这些。例如,

Selection.TypeText("foo")

in VBA becomes

在 VBA 中变成

using Microsoft.Office.Interop.Word;

Application word = new Application();
word.Selection.TypeText("foo");

in C#. Library references can be added by right-clicking the References folder in the Solution Explorer and choosing "Add Reference".

在 C# 中。可以通过右键单击“解决方案资源管理器”中的“引用”文件夹并选择“添加引用”来添加库引用。

回答by Albert D. Kallal

Given that VBA is VERY similar to vb.net, then I would suggest you converting to VB.net. In fact you can often cut + paste in code, especially if such code is from a code module. If your code has DAO.Recordsets, then I suggest creating a class in VB.net that “mimics” the dao.recordset. As such most VBA code can near 100% go into a vb.net code module. So this suggests that you don't need a conversion, but simply move the VBA code to VB.net. In fact 99% of the commands in VBA exist with the SAME syntax in VB.net.

鉴于 VBA 与 vb.net 非常相似,那么我建议您转换为 VB.net。事实上,您经常可以在代码中进行剪切 + 粘贴,尤其是当此类代码来自代码模块时。如果你的代码有 DAO.Recordsets,那么我建议在 VB.net 中创建一个“模仿”dao.recordset 的类。因此,大多数 VBA 代码可以接近 100% 进入 vb.net 代码模块。所以这表明您不需要转换,而只需将 VBA 代码移动到 VB.net。事实上,VBA 中 99% 的命令都与 VB.net 中的相同语法存在。