Import vba module to excel workbook using c#

Hello All,

Can someone tell me if this is possible?
Using c# as the language, I would like to grab a particular excel workbook, and import a vba module.

And also delete the vba module when program is done running using c#.

Suggestions anyone?
Thanks in advance
solarissfAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:
Import the VBA module to where?

Another workbook?
solarissfAuthor Commented:
I have previously manually right clicked on module and exported my desktop.  It created a file on desktop named "Module2.bas".  

Now using visualstudio express c#, I want to grab that module on my desktop, and import it to a workbook of my choosing.

'Do something

Then delete the module in c#.
Frank WhiteCommented:
Yes, it is possible.

I'm not particularly proficient in C# (nor at navigating MSDN), so you'll have to excuse my generic response to that particular aspect of the task:

In general, you would use OLE/ActiveX objects to communicate with Excel application, which if I remember correctly is through an Application Interop interface in C languages. Perhaps this page (link) might provide more helpful details on how to achieve that.

As for the method you need to import the module, an example within Excel VBA can be found here. I'm not quite sure how or if the syntax would change in C#, but what you're looking for in particular is this line: Application.VBE.ActiveVBProject.VBComponents.Import ("c:\MrXL1.bas")

You would probably replace "Application" with your declared interface instance, and use corresponding C# syntax to access the properties thereof and then run the method. This particular method will probably require having the workbook open and activated first through this interop interface before you can import successfully using VBE.ActiveVBProject.

I'm still mostly guessing  for the C# end of things though.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

NorieAnalyst Assistant Commented:
So you do want to put it in another workbook?

What is the 'something' you want to do?
solarissfAuthor Commented:
yes I want to put it into another workbook.  The something is just run macro... which I already know how to do.

DaFranker, I do know how to communicate with the excel.object. I guess I need to find the equivalent of Application.VBE.ActiveVBProject.VBComponents.Import ("c:\MrXL1.bas")
for c#.
NorieAnalyst Assistant Commented:
You need at least a reference to Microsoft.Vbe.Interop.
solarissfAuthor Commented:
is there actually a reference called that, or do I need to download it from somewhere?
NorieAnalyst Assistant Commented:
Yes there is a reference to that.

Mind you just like the equivalent reference in VBA it's quite near the bottom of the list and hard to locate, weill it was for me anyway.

By the way what code is in this module?
Frank WhiteCommented:
The reference in question for Office 2003 is:

Microsoft Visual Basic for Applications Extensibility 5.3
 Microsoft.Vbe.Interop.dll
 
According to the MSDN page on Office Interops. The page also states that it should be referenced by Excel and thus added automatically, so I'm guessing if your IDE supports browsing reference objects you should be able to search for a VBComponents somewhere in there.
NorieAnalyst Assistant Commented:
I've been able to put some code together which can do the import part.
solarissfAuthor Commented:
thanks DaFranker and imnorie,  I've used your suggestions and I have it working.  Apologies,  security blocker at work will not let me use button to block the code as I should.

this imports and works!!!
                    VBComponent oModule;
                    oModule = file1.VBProject.VBComponents.Import(@"C:\Documents and Settings\xxxx\Desktop\Module2.bas");

this clears all the modules... for some reason I couldnt get .Remove to work,.. something with my syntax.

                    VBProject project = file1.VBProject;
                   
                    //foreach (VBComponent VBComp in file1.VBProject.VBComponents)
                    for (int h = project.VBComponents.Count; h>=1; h--)
                    {
                        VBComponent component = project.VBComponents.Item(h);
                        component.CodeModule.DeleteLines(1, component.CodeModule.CountOfLines);
                    }


thanks very much!!!
NorieAnalyst Assistant Commented:
Almost the same as I had, wanted to solve the remove before posting though.

This is what I can up with for that.
 appvbe.VBComponents.Remove (appvbe.VBComponents.Item("Module1"));

Open in new window


That actually removes the whole module, not just the code in it.
solarissfAuthor Commented:
thank you... maybe I will switch to the .Remove..
some people online were saying it does not remove till the end of the programs.  Didnt want to chance it by when it actually removes the code.
NorieAnalyst Assistant Commented:
Not sure what you mean by the 'end of the programs'.

I only tested with a small example sub that did some formatting and data manipulation in the workkbook it had been imported to.

Once that was done the entire module was removed and the file saved.
solarissfAuthor Commented:
appreciate your help , thank you
solarissfAuthor Commented:
summarized the solution in my answer
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.