Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to convert VBA to C#?

Tags:

c#

vba

ms-access

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.

like image 387
tksy Avatar asked Dec 23 '08 13:12

tksy


People also ask

Will VBA ever be replaced?

Microsoft is finally planning to block Visual Basic for Applications (VBA) macros by default in a variety of Office apps. The change will apply to Office files that are downloaded from the internet and include macros, so Office users will no longer be able to enable certain content with a simple click of a button.

Is VBA still in demand?

Are VBA skills in-demand? Yes, knowledge and skills in VBA are still highly sought after. According to the TIOBE index , Visual Basic for Applications ranks number six in their popular programming language list.

Can I use C# in VBA?

You can expose code in a Visual C# project to Visual Basic for Applications (VBA) code if you want the two types of code to interact with each other. The Visual C# process is different from the Visual Basic process.

Can we convert VBA code to Python?

Everything you can write in VBA can be done in Python. This page contains information that will help you translate your VBA code into Python. Please note that the Excel Object Model is part of Excel and documented by Microsoft.


3 Answers

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.

like image 162
Dave R. Avatar answered Oct 31 '22 07:10

Dave R.


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

like image 29
Eric Ness Avatar answered Oct 31 '22 07:10

Eric Ness


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.

like image 1
Dave Markle Avatar answered Oct 31 '22 08:10

Dave Markle