本文介绍了仅在对象模块中有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误:

仅在对象模块中有效

当我尝试在 VBA Outlook 2016 上运行下面的脚本时.

when I'm trying to run the script below on VBA outlook 2016.

Private WithEvents myItem As outlook.MailItem

Private Sub myItem_AttachmentRead(ByVal myAttachment As Outlook.Attachment)

 If myAttachment.Type = olByValue Then

 MsgBox "If you change this file, also save your changes to the original file."

 End If

End Sub


Public Sub TestAttachRead()

 Dim atts As Outlook.Attachments

 Dim myAttachment As Outlook.Attachment

 Set myItem = Application.ActiveExplorer.CurrentFolder.Items("Test")

 Set atts = myItem.Attachments

 myItem.Display

End Sub

推荐答案

以下vba代码必须在ThisOutlookSession模块下

Private WithEvents myItem As outlook.MailItem
Private Sub myItem_AttachmentRead(ByVal myAttachment As Outlook.Attachment)

 If myAttachment.Type = olByValue Then

 MsgBox "If you change this file, also save your changes to the original file."

 End If

End Sub

下面的 vba 代码可以在常规模块中

Public Sub TestAttachRead()

 Dim atts As Outlook.Attachments

 Dim myAttachment As Outlook.Attachment

 Set myItem = Application.ActiveExplorer.CurrentFolder.Items("Test")

 Set atts = myItem.Attachments

 myItem.Display

End Sub

这篇关于仅在对象模块中有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 19:03