I'm looking for a way to write code from a VB.NET add-in into a VBA project

So say write "Msgbox("Hello world!")" into a .bas module as part of an Excel project from .NET so the code written from .NET would be some pre written VBA stored as a string and injected into the VBA project

I have legacy code which I use for doing this sort of thing within VBA using the VBE properties to make a command bar for the VBA IDE and to write code into other modules or create new modules, but I'd like to be able to do this from either like I have said in the subject from VB.NET [preferably from a created add-in to be displayed on the Excel Ribbon] or maybe from a standalone winform app that can be minimised to the tray.

The two things I want to do this are for:

  1. To monitor VBA code performance, the legacy code I mention basically inserts a call to another sub at the start and end of each procedure in the project and writes out to a .csv so that you know how long each sub and function took to run
  2. To break up VBA project into component parts so that they can be added to TFS, basically loop through all the components of a VBA project and export them to a folder so they can be added to TFS enabling better source code tracking of changes

Can someone point me to examples [if any] of writing code to a VBA module from vb.net or give guidance on how this may be achieved.

I'm using .net framework 4.5 and visual studio 2012 with vb, but I'm fine with c# examples as well

Many thanks.

有帮助吗?

解决方案

I assume you've figured this out already, but just in case:

The VBE object tree is exposed through automation - just get the application object for whatever Office file you're dealing with via a CreateObject call, then do the same thing you did in VBA (with appropriate changes from VB6 to .net, obviously).

exl = CreateObject("Excel.Sheet")
exl.Application.Workbooks.Open("whatever.xlsx")
'exl.Application.VBE.ActiveVBProject etc etc
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top