Solved

Import vba module to excel workbook using c#

Posted on 2012-03-20
16
1,182 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:solarissf
  • 7
  • 7
  • 2
16 Comments
 
LVL 33

Expert Comment

by:Norie
ID: 37743482
Import the VBA module to where?

Another workbook?
0
 

Author Comment

by:solarissf
ID: 37743501
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#.
0
 
LVL 3

Accepted Solution

by:
DaFranker earned 300 total points
ID: 37743526
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.
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 33

Expert Comment

by:Norie
ID: 37743532
So you do want to put it in another workbook?

What is the 'something' you want to do?
0
 

Author Comment

by:solarissf
ID: 37743625
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#.
0
 
LVL 33

Assisted Solution

by:Norie
Norie earned 200 total points
ID: 37743677
You need at least a reference to Microsoft.Vbe.Interop.
0
 

Author Comment

by:solarissf
ID: 37743687
is there actually a reference called that, or do I need to download it from somewhere?
0
 
LVL 33

Expert Comment

by:Norie
ID: 37743736
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?
0
 
LVL 3

Expert Comment

by:DaFranker
ID: 37743787
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.
0
 
LVL 33

Expert Comment

by:Norie
ID: 37743939
I've been able to put some code together which can do the import part.
0
 

Assisted Solution

by:solarissf
solarissf earned 0 total points
ID: 37744001
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!!!
0
 
LVL 33

Expert Comment

by:Norie
ID: 37744040
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.
0
 

Author Comment

by:solarissf
ID: 37744053
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.
0
 
LVL 33

Expert Comment

by:Norie
ID: 37744075
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.
0
 

Author Comment

by:solarissf
ID: 37744082
appreciate your help , thank you
0
 

Author Closing Comment

by:solarissf
ID: 37762397
summarized the solution in my answer
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question