从代码调用outlook宏

时间:2016-05-15 19:46:56

标签: vb.net outlook-vba

运行下面的InvokeMember方法时出错。错误是UNKNOWN NAME。我检查了拼写,这是正确的。在Outlook我在信任中心Enable Macros。为了让这个工作有什么我可能会失踪的吗?感谢

VB代码:

olApp.GetType().InvokeMember("Run", Reflection.BindingFlags.Default Or 
                                    Reflection.BindingFlags.InvokeMember, 
                                    Nothing, olApp, New Object() {"nameOfMacro"})

Research

1 个答案:

答案 0 :(得分:0)

好吧,似乎InvokeMember无效。答案是像在宏中一样制作代码并运行它。例如:

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