Click here to Skip to main content
15,887,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I get an error when I run the `InvokeMember` method below. The error is `UNKNOWN NAME`. I have checked the spelling and it is correct. In `Outlook` I have `Enable Macros` in the trust center. Is there anything I might be missing to get this working? Thanks

VB code:

VB
olApp.GetType().InvokeMember("nameOfMacro", Reflection.BindingFlags.InvokeMember, Nothing, olApp, {})


What I have tried:

Looked here: http://support.microsoft.com/kb/306683/
Posted
Updated 15-May-16 12:09pm

Well, you clearly didn't read the linked KB article in any depth.

You don't pass the name of the macro to the InvokeMember method as the method to invoke! You pass in "Run" as the method name, and the macro name as one of the arguments:
C#
private void RunMacro(object oApp, object[] oRunArgs)
{
    oApp.GetType().InvokeMember("Run",
        System.Reflection.BindingFlags.Default |
        System.Reflection.BindingFlags.InvokeMethod,
        null, oApp, oRunArgs);
}

RunMacro(olApp, new Object[]{ "nameOfMacro" });
 
Share this answer
 
Comments
Idle_Force 12-May-16 12:18pm    
Thank you very much. I will try this and get back as soon as I can.
Idle_Force 15-May-16 15:22pm    
I did it that way and it has the same error.
Well, it seems InvokeMember will not work. The answer is to make the code as you would in the macro and run that. Example:

VB
Private Sub MoveAttachmentToFolder()
  Dim olNs = olApp.GetNamespace("MAPI")
  Dim subFolderA = olNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox).
                                         Parent.Folders("subFolderA")
  Dim subFolderB = olNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox).
                                         Parent.Folders("subFolderB")
  For Each mi As Outlook.MailItem In subFolderA.Items
    If mi.Attachments.Count = 1 Then
      'remember interops use 1 based arrays not zero
      Dim fileName As String = "some path" & mi.Attachments(1).FileName
      mi.Attachments(1).SaveAsFile(fileName)
      mi.Move(subFolderB)
    End If
  Next
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900