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. ...

+ Reply to Thread
Results 1 to 2 of 2

Automating a Word Macro from C#

  1. Default 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 });


  2. Default 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" });
    >




+ Reply to Thread

Similar Threads

  1. Replies: 2
    Last Post: 10-30-2007, 06:01 PM
  2. Word vba macro
    By Application Development in forum ADO DAO RDO RDS
    Replies: 0
    Last Post: 09-27-2007, 05:46 PM
  3. using ADO to grab SQL data into Word VBA macro
    By Application Development in forum ADO DAO RDO RDS
    Replies: 1
    Last Post: 08-28-2007, 10:32 AM
  4. Word: cannot open macro storage
    By Application Development in forum DOTNET
    Replies: 0
    Last Post: 07-09-2007, 09:23 AM
  5. Running a word macro from a windows service...
    By Application Development in forum CSharp
    Replies: 0
    Last Post: 03-29-2007, 07:48 PM