views:

151

answers:

3

Hello! I know that the web is full of questions like this one, but I still haven't been able to apply the answers I can find to my situation.

I realize there is VBA, but I always disliked having the program/macro living inside the Excel file, with the resulting bloat, security warnings, etc. I'm thinking along the lines of a VBScript that works on a set of Excel files while leaving them macro-free. Now, I've been able to "paint the first column blue" for all files in a directory following this approach, but I need to do more complex operations (charts, pivot tables, etc.), which would be much harder (impossible?) with VBScript than with VBA.

For this specific example knowing how to remove all macros from all files after processing would be enough, but all suggestions are welcome. Any good references? Any advice on how to best approach external batch processing of Excel files will be appreciated.

Thanks!

PS: I eagerly tried Mark Hammond's great PyWin32 package, but the lack of documentation and interpreter feedback discouraged me.

A: 

You could put your macros in a separate excel file.

Karsten W.
Thank you Karsten. I guess that should work. (Any examples along those lines would be welcome.) Cheers.
You could google for "xlstart folder" to see how to make the macros available for every file. Don't hestitate to upvote/accept an answer if you like it.
Karsten W.
Thanks again, Karsten. Too bad you can only accept one answer and that beginners cannot upvote. I will come back and upvote you when I have enough reputation.
Hey, I really like this approach. Thought I had it... but when opening an Excel file from VBScript (i.e., externally) the XLStart contents are not read... I'm lost about how to include it, or otherwise how to invoke a macro in another file without getting a security warning. (Setting the macro security level to low is out of the question.)
If you use the macros on your machine only, you can sign your VBA-project digitally (in the vba-Window under Extras-digital signature). Once you accept all macros by this publisher (you), you will not see security warnings anymore. I have not found out how to do this with a distributed file, however.
Karsten W.
When you call Excel from VBScript, you could include your macro file as command line parameter. This way Excel will surely open your file and make your macros available.
Karsten W.
+1  A: 

Almost anything you can do in VBA to automate excel you can do in VBScript (or any other script/language that supports COM).

Once you have created an instance of Excel.Application you can pretty much drop your VBA into a VBS and go from there.

Alex K.
Thank you, Alex. (I'm not reputable enough to upvote!) I thought VBScript was a strict subset of VBA. In any case, there is more to it than just copying and pasting, right? Would you recommend books or sites that target this specific connection between VBScript and VBA? Cheers.
A: 

If it's the Excel/VBA capability that you're looking to use then you could always start by creating all of the code that will interact with the Excel files you're wanting to work on within an Excel file - a kind of master file that is separated from the regular files, as suggested by Karsten W.

This gives you the freedom to write Excel/VBA.

Then you can call your master workbook (which can be configured to run your code when the book is opened, for example) from a VB script, batch file, Task Scheduler, etc.

If you want to get fancy, you can even use VBA in your master file to create/modify/delete custom macros/VBA modules in any of the target files that you're processing.

The info for just about all of the techniques I'm describing I got from the Excel VBA built-in reference docs, but it certainly helps to be familiar with the specific programming tasks that you're tackling. I'd advise that the best approach is to put together your tasks (eg, make column blue, update/sort data etc) one by one and then worry about the automation at the end.

comrade