Dynamically load scripts from file to Microsoft Word

R

RealCat

I'm accessing MS Word using .NET interop. I posted a question at the
C# news group but I got no reply. (My original post was
http://groups.google.com/group/micr...p/browse_thread/thread/b509664c6a3a5043?hl=en)
Maybe I posted it to a wrong group.

I have read VBA documentation but the description was enough,and there
was no sample code, and the funtion names were so general that I
cannot find results from Google.

Isn't Document.Scripts.Add() for adding script (functions or subs)
dynamically? I added a simple sub (with just one msgbox statement),
then tried to run it with Word.Application.Run(), but an exception
occurred saying "Unable to run the macro." If you know any hints,
please let me know. Thank you.

PS : Basically what I'm trying to do is:
script = LoadMacroFromVBSFile(file name)
For each doc in many .doc documents
{
apply the script to the doc
}
 
C

Cindy M.

Hi RealCat,
I have read VBA documentation but the description was enough,and there
was no sample code, and the funtion names were so general that I
cannot find results from Google.

Isn't Document.Scripts.Add() for adding script (functions or subs)
dynamically?

No. This would add a Script to an HTML document.

What you're looking for is part of the VB Extensions type library and is
common to all Office applications. If you go to the Word macro editor,
press Ctrl+G to bring up the immediate window, then type:
ActiveDocument.VBProject

You'll come into that part of the object model. F1 should bring up the
Help topic, and using the Object Browser (F2) should let you see what
objects, methods and properties are available.

The documentation isn't great and the object model isn't particularly
intuitive, but based on this you should be able to turn up information
and code samples when searching the Internet. Very roughly, you need
VBProject.VBComponents
.Add to create a new code module
.Item[index].CodeFile.AddFromString to "type" code into a module
.Item[index].CodeFile.AddFromFile to import code from a file
...I added a simple sub (with just one msgbox statement),
then tried to run it with Word.Application.Run(), but an exception
occurred saying "Unable to run the macro." If you know any hints,
please let me know. Thank you.

PS : Basically what I'm trying to do is:
script = LoadMacroFromVBSFile(file name)
For each doc in many .doc documents
{
apply the script to the doc
}

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top