Skip to main content
added 36 characters in body
Source Link
JMK
  • 374
  • 3
  • 12

It really depends on how you intend to move to C# (ie what technology you are going to use).

If you are going to use OpenXML, then you are talking a total re-write which, if you have a solution that works, isn't recommended.

If you are talking about using Interop, you may find that the process is surprisingly smooth. I've moved a lot of code from VBA to C# (with Interop) in the past and once you are plugged into the workbook, like so:

var excelApplication = new Application();
var workbook = excelApplication.Workbooks.Open(@"C:\location.xlsx");

In a lot of cases you can copy/pasta your VBA code, prefixing function calls with workbook., replacing Dim with var etc where appropriate and adding semicolons on the end.

It's essentially the same Framework underneath (Excel), you are just changing Syntax from VBA to C#.

When you're done, be sure to save and close the application:

workbook.Save();
excelApplication.Quit();

Then release the application with the Marshal:

Marshal.ReleaseComObject(excelApplication);

I would probably write a wrapper class implementing IDisposable around the Excel Application object, then be sure to use using when this object is used.

A good way to make your case would be to spend an hour or so doing this, which in a short space of time would demonstrate how quickly you could port code across, and convince your colleagues that this is a good idea.

The code would remain largely unchanged in a way, but you have all of the benefits you mentioned of having a C# project within Visual Studio.

It really depends on how you intend to move to C# (ie what technology you are going to use).

If you are going to use OpenXML, then you are talking a total re-write which, if you have a solution that works, isn't recommended.

If you are talking about using Interop, you may find that the process is surprisingly smooth. I've moved a lot of code from VBA to C# (with Interop) in the past and once you are plugged into the workbook, like so:

var excelApplication = new Application();
var workbook = excelApplication.Workbooks.Open(@"C:\location.xlsx");

In a lot of cases you can copy/pasta your VBA code, prefixing function calls with workbook. where appropriate and adding semicolons on the end.

It's essentially the same Framework underneath (Excel), you are just changing Syntax from VBA to C#.

When you're done, be sure to save and close the application:

workbook.Save();
excelApplication.Quit();

Then release the application with the Marshal:

Marshal.ReleaseComObject(excelApplication);

I would probably write a wrapper class implementing IDisposable around the Excel Application object, then be sure to use using when this object is used.

A good way to make your case would be to spend an hour or so doing this, which in a short space of time would demonstrate how quickly you could port code across, and convince your colleagues that this is a good idea.

The code would remain largely unchanged in a way, but you have all of the benefits you mentioned of having a C# project within Visual Studio.

It really depends on how you intend to move to C# (ie what technology you are going to use).

If you are going to use OpenXML, then you are talking a total re-write which, if you have a solution that works, isn't recommended.

If you are talking about using Interop, you may find that the process is surprisingly smooth. I've moved a lot of code from VBA to C# (with Interop) in the past and once you are plugged into the workbook, like so:

var excelApplication = new Application();
var workbook = excelApplication.Workbooks.Open(@"C:\location.xlsx");

In a lot of cases you can copy/pasta your VBA code, prefixing function calls with workbook., replacing Dim with var etc where appropriate and adding semicolons on the end.

It's essentially the same Framework underneath (Excel), you are just changing Syntax from VBA to C#.

When you're done, be sure to save and close the application:

workbook.Save();
excelApplication.Quit();

Then release the application with the Marshal:

Marshal.ReleaseComObject(excelApplication);

I would probably write a wrapper class implementing IDisposable around the Excel Application object, then be sure to use using when this object is used.

A good way to make your case would be to spend an hour or so doing this, which in a short space of time would demonstrate how quickly you could port code across, and convince your colleagues that this is a good idea.

The code would remain largely unchanged in a way, but you have all of the benefits you mentioned of having a C# project within Visual Studio.

Source Link
JMK
  • 374
  • 3
  • 12

It really depends on how you intend to move to C# (ie what technology you are going to use).

If you are going to use OpenXML, then you are talking a total re-write which, if you have a solution that works, isn't recommended.

If you are talking about using Interop, you may find that the process is surprisingly smooth. I've moved a lot of code from VBA to C# (with Interop) in the past and once you are plugged into the workbook, like so:

var excelApplication = new Application();
var workbook = excelApplication.Workbooks.Open(@"C:\location.xlsx");

In a lot of cases you can copy/pasta your VBA code, prefixing function calls with workbook. where appropriate and adding semicolons on the end.

It's essentially the same Framework underneath (Excel), you are just changing Syntax from VBA to C#.

When you're done, be sure to save and close the application:

workbook.Save();
excelApplication.Quit();

Then release the application with the Marshal:

Marshal.ReleaseComObject(excelApplication);

I would probably write a wrapper class implementing IDisposable around the Excel Application object, then be sure to use using when this object is used.

A good way to make your case would be to spend an hour or so doing this, which in a short space of time would demonstrate how quickly you could port code across, and convince your colleagues that this is a good idea.

The code would remain largely unchanged in a way, but you have all of the benefits you mentioned of having a C# project within Visual Studio.