【问题标题】:Running an Excel macro from Outlook从 Outlook 运行 Excel 宏
【发布时间】:2019-10-01 17:22:26
【问题描述】:

我正在尝试在 Excel 中设置一个自动数据库,当用户在预期到期日之后退出设备时,它会发送一封电子邮件。

我还想实现一个功能,用户可以使用关键字“延长”回复电子邮件,以将他们的退出日期延长 7 天。

我的电子邮件发送正确。我想在 Outlook 中创建一个脚本,将他们的回复链接到打开的 Excel 工作表。 Outlook 和 Excel 将在专用 PC 上同时打开。我不想每次运行 Outlook 宏时都打开一个新的 Excel 文件。

这是 Outlook 宏:

Sub Exc_macro(Item As Outlook.MailItem)
Dim ExApp As Workbook
Dim gageID As String
Dim cap As String

If Left(Item.Body, 6) = "extend" Then   'Check for keyword in body
    gageID = Mid(Item.Subject, 23)  'Get equipment ID number
    Set ExApp = Excel.ActiveWorkbook
    Call ExApp.Application.Run("Module2.increase", gageID)
End If

End Sub

我想在这里将gageID 参数传递给 Excel 宏:

Sub increase(gageID As String)
Set Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))     
For Each cell In Rng
    If cell.Value = gageID Then
        cell.Offset(0, 9).Value = cell.Offset(0, 9).Value + 7
    End If
Next

End Sub

如何在 Outlook 中引用打开的工作簿并随后运行 Excel 宏?

【问题讨论】:

    标签: excel vba outlook


    【解决方案1】:

    像这样:

    Sub Exc_macro(Item As Outlook.MailItem)
    
        Dim ExApp As Object
        Dim gageID As String
        Dim cap As String
    
        If Left(Item.Body, 6) = "extend" Then   'Check for keyword in body
            gageID = Mid(Item.Subject, 23)  'Get equipment ID number
            Set ExApp = GetObject(,"Excel.Application")
            ExApp.Run "'my ExcelWB.xlsm'!increase", gageID
        End If
    
    End Sub
    

    让您的increase Sub 在常规代码模块中

    http://www.rondebruin.nl/win/s9/win001.htm

    【讨论】:

      猜你喜欢
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-16
      • 1970-01-01
      • 2010-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多