从R运行Outlook VBA代码

Tha*_*uys 8 outlook vba r

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

我调查了一下:

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

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)
Run Code Online (Sandbox Code Playgroud)

宏可以简单地说:

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
Run Code Online (Sandbox Code Playgroud)

小智 3

两种方式

1. 在 Outlook 应用程序中

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

此外,您还需要将公共“dss”宏放入 Outlook ThisOutLookSession 中,以便能够从 Outlook 中调用它。

然后这样尝试你的 R 代码

    library(RDCOMClient)
    OutApp <- COMCreate("Outlook.Application")
    OutApp$Run("dss")
Run Code Online (Sandbox Code Playgroud)

2. 在Excel应用程序中

如果您的宏只是为了创建 Excel 工作簿,为什么要在 Outlook.Application 中执行此操作,请使用 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()`
Run Code Online (Sandbox Code Playgroud)

上面是稍微修改过的代码,来自/sf/answers/3025573421/