Automating a Word Macro from C# - CSharp
This is a discussion on Automating a Word Macro from C# - CSharp ; 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. ...
-
Automating a Word Macro from C#
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 });
-
Re: Automating a Word Macro from C#
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" });
>
Similar Threads
-
By Application Development in forum lisp
Replies: 2
Last Post: 10-30-2007, 06:01 PM
-
By Application Development in forum ADO DAO RDO RDS
Replies: 0
Last Post: 09-27-2007, 05:46 PM
-
By Application Development in forum ADO DAO RDO RDS
Replies: 1
Last Post: 08-28-2007, 10:32 AM
-
By Application Development in forum DOTNET
Replies: 0
Last Post: 07-09-2007, 09:23 AM
-
By Application Development in forum CSharp
Replies: 0
Last Post: 03-29-2007, 07:48 PM