excel - 如何关闭特定工作簿以响应接收具有特定标题的 Outlook 邮件?

标签 excel vba outlook

我正在尝试允许同事保存和关闭共享工作表,而无需他们知道我的计算机登录信息。
该文件保持打开状态,以防他们需要该文件而不是“只读”版本。

只有在工作簿打开时才会触发,这一点很重要。如果可能,它还会结束从工作簿运行的所有宏实例。

我想添加一个 Outlook VBA 触发器,在收到具有特定主题的邮件时保存并关闭它(已存在于 Excel 中)。
Excel 上的所有代码都可以正常工作。 (保存并关闭宏在特定时间触发并确认有效)。

在 Outlook 端,我将我认为是事件监听器代码添加到 ThisOutlookSession,它调用一个模块,该模块应触发 Excel 中的关闭子。

ThisOutlookSession 中的代码

Option Explicit
Private WithEvents inboxItems As Outlook.Items
Private Sub Application_Startup()
    Dim outlookApp As Outlook.Application
    Dim objectNS As Outlook.NameSpace
      
    Set outlookApp = Outlook.Application
    Set objectNS = outlookApp.GetNamespace("MAPI")
    Set inboxItems = objectNS.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub inboxItems_ItemAdd(ByVal Item As Object)
    On Error GoTo ErrorHandler
    Dim Msg As Outlook.MailItem
    If TypeName(Item) = "MailItem" Then
        Call Excel_Closer.Close_Excel
    End If
ExitNewItem:
    Exit Sub
ErrorHandler:
    MsgBox Err.Number & " - " & Err.Description
    Resume ExitNewItem
End Sub

模块中的代码 (Excel_Closer)

保存和关闭的Excel宏是“mCloser.EmailClose”

“Nordic_Market_Monitor_2019.xlsm”是打开后激活的工作簿。

Option Explicit
Sub Close_Excel(MyMail As MailItem)
    On Error GoTo Error_Handler
    Dim xlApp As Excel.Application
    Dim xlBook As Workbook
    Dim strSubject As String

    strSubject = MyMail.Subject

    If strSubject = "Close Excel" Then
        On Error GoTo Error_Handler
        
        Set xlApp = GetObject(, "Excel.Application")
        Set xlBook = xlApp.Workbooks("Nordic_Market_Monitor_2019.xlsm").Activate
        
        xlApp.Visible = True

        xlBook.Application.Run "mCloser.EmailClose"

        Set xlApp = Nothing
        Set xlBook = Nothing
        
    End If
   
Error_Handler:
    Exit Sub
End Sub

没有错误消息被触发,也没有发生任何其他事情。

最佳答案

如果您引用 Excel 或工作簿时出现错误,则无法打开。

Sub Close_Excel(MyMail As MailItem)

    ' Remove in development phase to highlight the line with the error
    'On Error GoTo Error_Handler

    Dim xlApp As Excel.Application
    Dim xlBook As Workbook
    Dim strSubject As String

    strSubject = MyMail.Subject

    If strSubject = "Close Excel" Then

        ' "On Error Resume Next" is rarely beneficial
        '  It is here for a specific purpose

        On Error Resume Next ' bypass error if Excel is not open
        Set xlApp = GetObject(, "Excel.Application")
        On Error GoTo 0 ' Remove error bypass as soon as the purpose is served

        If Not xlApp Is Nothing Then

            'Excel is open
            On Error Resume Next ' bypass error if workbook is not open
            Set xlBook = xlApp.Workbooks("Nordic_Market_Monitor_2019.xlsm")
            On Error GoTo 0 ' Remove error bypass as soon as the purpose is served

            If Not xlBook Is Nothing Then
                ' Workbook is open
                xlApp.Visible = True
                xlBook.Application.Run "mCloser.EmailClose"

            Else
                Debug.Print "Workbook not open."

            End If

        Else
            Debug.Print "Excel not open."

        End If

    End If

exitRoutine:
    Set xlApp = Nothing
    Set xlBook = Nothing
    Exit Sub

'Error_Handler:
'    MsgBox Err.Number & " - " & Err.Description
'    Resume exitRoutine

End Sub

关于excel - 如何关闭特定工作簿以响应接收具有特定标题的 Outlook 邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55647602/

相关文章:

calendar - php-ews:为什么日历事件的主体不能像其他属性(例如主题/开始时间)一样从事件中访问?

image - Outlook HTML 电子邮件中的 VML 表格单元格背景图像仅在单击或调整大小时可见

excel - 绝对值排序范围

excel - 最简单的 Powershell 导入 Excel 问题

c# - 让 Outlook 识别我的数字签名电子邮件

excel - VB - 插入空白行

vba - 如何获取表单的最后一条记录ID?

excel - 将多个范围复制到下一个可用行

arrays - 使用数组和循环删除字符实例

ms-access - 错误代码 3021 bof 或 eof 为真或当前记录已被删除