Reputation: 590
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:
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.
Upvotes: 2
Views: 949
Reputation: 134
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
Upvotes: 2