Solved

Import vba module to excel workbook using c#

Posted on 2012-03-20
16
1,108 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now