Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Import vba module to excel workbook using c#

Posted on 2012-03-20
16
Medium Priority
?
1,426 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 35

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:
Frank White earned 1200 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

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 35

Assisted Solution

by:Norie
Norie earned 800 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 35

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:Frank White
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 35

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 35

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 35

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

824 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