从代码调用outlook宏 [英] Call outlook macro from code

查看:169
本文介绍了从代码调用outlook宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面运行`InvokeMember`方法时遇到错误。错误是未知名称。我检查了拼写,这是正确的。在`Outlook`中,我在信任中心中启用了启用宏。为了让这个工作有什么我可能会失踪的吗?谢谢



VB代码:



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





我尝试过:



看这里:http://support.microsoft.com/kb/306683/

解决方案

好吧,你显然没有深入阅读链接的知识库文章。



您不会将宏的名称传递给 InvokeMember 方法作为调用方法!您传入Run作为方法名称,并将宏名称作为参数之一传递:

  private   void  RunMacro( object  oApp, object  [] oRunArgs)
{
oApp.GetType()。InvokeMember( 运行
System.Reflection.BindingFlags.Default |
System.Reflection.BindingFlags.InvokeMethod,
null ,oApp, oRunArgs);
}

RunMacro(olApp, new 对象 [] { nameOfMacro});


< blockquote>好吧,似乎InvokeMember不起作用。答案是像在宏中一样制作代码并运行它。示例:



 私有  Sub  MoveAttachmentToFolder()
Dim olNs = olApp.GetNamespace( < span class =code-string> MAPI)
Dim subFolderA = olNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)。
Parent.Folders( subFolderA
Dim subFolderB = olNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)。
Parent.Folders( subFolderB
对于 每个 mi 作为 Outlook.MailItem subFolderA.Items
如果 mi.Attachments.Count = 1 然后
' 记住interops使用1为基础数组不为零
Dim fileName As 字符串 = 某个路径& mi.Attachments( 1 )。FileName
mi.Attachments( 1 )。SaveAsFile(fileName )
mi.Move(subFolderB)
结束 如果
< span class =code-keyword>下一步
结束 Sub


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:

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



What I have tried:

Looked here: http://support.microsoft.com/kb/306683/

解决方案

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:

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


Well, it seems InvokeMember will not work. The answer is to make the code as you would in the macro and run that. Example:

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


这篇关于从代码调用outlook宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆