PC Review


Reply
Thread Tools Rate Thread

Automating a Word Macro from C#

 
 
fatima.issawi@gmail.com
Guest
Posts: n/a
 
      3rd Oct 2007
Hello,

I am trying to run a Word Macro from C#. I am getting the error:

Unknown name. (Exception from HRESULT: 0x80020006
(DISP_E_UNKNOWNNAME))

when I get to the line that actually runs the macro. All the other
code runs correctly. Am I calling the macro correctly? The macro is
available in the Normal.dot template. It is available if I open the
file and manually try to run the macro. It just fails when I try to
automate it.

I have attached a code snippet. Any help is appreciated.

Thanks,
Fatima

*********************************************************************************
// Run Macros to fix up the document and save the file as a .DOC file
Type wordType = Type.GetTypeFromProgID("Word.Application");
object wordApplication = Activator.CreateInstance(wordType);

wordType.InvokeMember("Visible",
System.Reflection.BindingFlags.SetProperty, null, wordApplication,
new object[]
{ true });
object wordDocuments = wordType.InvokeMember("Documents",
System.Reflection.BindingFlags.GetProperty,
null,
wordApplication, null);
object wordDocument = wordDocuments.GetType().InvokeMember("Open",
System.Reflection.BindingFlags.InvokeMethod,
null,
wordDocuments, new object[] { exportedFile.ToString() });

// Run Macros on the document to fix it up.
wordDocument.GetType().InvokeMember("Run",
System.Reflection.BindingFlags.InvokeMethod,
null,
wordDocument, new object[] { "Test" });

// Save the document as a .DOC file.
wordDocument.GetType().InvokeMember("SaveAs",
System.Reflection.BindingFlags.InvokeMethod,
null,
wordDocument, new object[] { savedFile, fileFormat });

 
Reply With Quote
 
 
 
 
fatima.issawi@gmail.com
Guest
Posts: n/a
 
      3rd Oct 2007
Hello,

Silly me. I should be calling the macro like this instead:

wordApplication.GetType().InvokeMember("Run",
System.Reflection.BindingFlags.InvokeMethod,
null,
wordApplication, new object[] { "Test" });

Hope this helps someone else.

Regards,
Fatima

> // Run Macros on the document to fix it up.
> wordDocument.GetType().InvokeMember("Run",
> System.Reflection.BindingFlags.InvokeMethod,
> null,
> wordDocument, new object[] { "Test" });
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Automating Word document for different versions of Word" alanbr Microsoft VB .NET 2 3rd Mar 2010 07:25 AM
Automating macro to run macro from Access Cam Microsoft Excel Programming 3 24th Aug 2009 05:41 PM
Word: automating the repetition of a macro run =?Utf-8?B?Y21tbmN0bmc=?= Windows XP General 1 4th Nov 2005 07:09 PM
automating macro =?Utf-8?B?Y2hyaXM=?= Microsoft Excel Programming 2 25th Apr 2005 05:17 PM
Frequently used macro-automating? Ben Kingsley Microsoft Excel Programming 0 17th Sep 2003 05:53 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:54 AM.