首页 文章

从R运行Outlook VBA代码

提问于
浏览
8

我知道一个潜在的shell / vbs脚本解决方案 . 但是,我正在寻找一个包含 library(RDCOMClient) 包的解决方案 .

我调查了一下:

Outlook中的一些尝试(在 ThisOutlookSession 中给出 Public Sub dss() ):

library(RDCOMClient)
> OutApp <- COMCreate("Outlook.Application")
> oa<-OutApp[["Session"]][["Accounts"]]
> OutApp$dss()
Error in .COM(x, name, ...) : 
  Cannot locate 0 name(s) dss in COM object (status = -2147352570)
> OutApp$Application$dss()
Error in OutApp$Application$dss : 
  object of type 'closure' is not subsettable
> OutApp$Run("dss")
Error in .COM(x, name, ...) : 
  Cannot locate 0 name(s) Run in COM object (status = -2147352570)

宏可以简单地说:

Public Sub dss()
    Dim excApp As Object
    Dim excWkb As Object
    Dim excWks As Object

    Set excApp = CreateObject("Excel.Application")
    Set excWkb = excApp.Workbooks.Add()

    excWkb.SaveAs "AXX.xlsx"
    excWkb.Close
End Sub

2 回答

  • 4

    据我所知,从其他应用程序运行存储在Outlook中的宏是不可能的 . Outlook缺少Word和Excel中提供的Application.Run方法 .

    https://social.msdn.microsoft.com/Forums/office/en-US/5a6396c4-ad24-42a4-b711-101e24254334/how-to-fire-outlook-macro-through-excel-vba?forum=exceldev

    所以在这里留下R问题,我可以保证你甚至无法从Outlook宏编辑器本身执行此操作,从顶级Application对象开始

    我已经尝试了我能想到的与outlook宏的每一个组合,我担心现在不可能 . 它可能是2007年左右的旧版本,但现在还没有

    Not working

    如果你可以在VBScript或outlook中使用它,它也可以在 R 中工作 . 但我担心你无法在任何地方工作

    您可以做的可能的解决方法

    • 使用触发点定义规则并将宏与相关联 . 尝试模拟触发器而不是直接调用宏 .

    • 在工具栏中创建一个自定义按钮,看看是否可以从外部vba调用命令(这将很难破解)

  • 3

    两种方式

    1.在Outlook应用程序中

    Outlook.Application没有可见属性,请看这篇文章 - > http://www.vbaexpress.com/forum/archive/index.php/t-8287.html您将使用文件夹或邮件项目显示方法来显示Outlook要显示Outlook窗口,请将以下代码添加到您的'dss'宏 ThisOutlookSession.ActiveExplorer.Display

    此外,你需要将你的公共'dss'宏放在outlookOutLookSession中,以便能够将它从outlook中调出 .

    然后以这种方式尝试你的R代码

    library(RDCOMClient)
        OutApp <- COMCreate("Outlook.Application")
        OutApp$Run("dss")
    

    2.在Excel应用程序中

    如果您的宏只是创建一个Excel工作簿,为什么要在Outlook中执行它 . 应用程序使用Excel.Application . 使用模块中的宏创建excel工作簿,然后执行如下操作

    library(RDCOMClient)
        #Open a specific workbook in Excel:
        xlApp <- COMCreate("Excel.Application")
        xlWbk <- xlApp$Workbooks()$Open("C:\\Temp\\macro_template.xlsm")
    
        # this line of code might be necessary if you want to see your spreadsheet:
        xlApp[['Visible']] <- TRUE 
    
        # Run the macro called "dss":
        xlApp$Run("dss")
    
        # Close the workbook and quit the app:
        xlWbk$Close(FALSE)
        xlApp$Quit()`
    

    以上是https://stackoverflow.com/a/43222477/5871610的轻微修改代码

相关问题