Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Office interop: load a script dynamically.

2 views
Skip to first unread message

Sin Jeong-hun

unread,
Jan 6, 2009, 7:32:52 PM1/6/09
to
This is a sample VBA script that shows the content of the Word
document.
Sub Test()
MsgBox Me.Content.Text
End Sub

I would like to load a script like above from a file and apply it to a
Word document dynamically. To do that, I was writing some simple code.

Word.Application app = new Microsoft.Office.Interop.Word.Application
();
object fileName = (object)"C:\\hello.doc";
object Missing = System.Reflection.Missing.Value;

Word.Document d = app.Documents.Open(ref fileName, ref Missing, ref
Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref
Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref
Missing, ref Missing, ref Missing, ref Missing);
d.Scripts.Add(Missing,
Microsoft.Office.Core.MsoScriptLocation.msoScriptLocationInBody,

Microsoft.Office.Core.MsoScriptLanguage.msoScriptLanguageVisualBasic,
null,
"Test2","MsgBox Me.Content.Text");
app.Run("Test2", ref Missing, ref Missing, ref Missing, ref
Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref
Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref
Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref
Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref
Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref
Missing, ref Missing);
d.Close(ref Missing, ref Missing, ref Missing);

But an exception occurred saying "Unable to run the macro." How can I
achieve what I'm trying to do? Could you give me a hint? Thank you.

0 new messages