Is it possible to convert VBA to C#?

前端 未结 4 1109
予麋鹿
予麋鹿 2020-12-05 11:28

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

相关标签:
4条回答
  • 2020-12-05 12:15

    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,

    Selection.TypeText("foo")
    

    in VBA becomes

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

    0 讨论(0)
  • 2020-12-05 12:21

    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.

    0 讨论(0)
  • 2020-12-05 12:24

    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.

    0 讨论(0)
  • 2020-12-05 12:33

    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:

    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#:

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

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

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

    Good luck and I hope this helps.

    0 讨论(0)
提交回复
热议问题