我正在尝试阅读电子邮件主题,并将其存储为变量。
出于测试目的,我将其设置为用作文本文件的文件名。
我的代码返回一个空字符串。
ThisOutlookSession
中存在以下代码的电子邮件地址Topic (923832)
为什么str = oMail.Subject
无法读取任何内容?
这是我的代码:([On Error Resume Next
现在已删除。)
Option Explicit
Private WithEvents olInboxItems As Items
Private Sub Application_Startup()
Dim objNS As NameSpace
Set objNS = Application.Session
' instantiate objects declared WithEvents
Set olInboxItems = objNS.GetDefaultFolder(olFolderInbox).Items
Set objNS = Nothing
End Sub
Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
'On Error Resume Next
Dim oMail As Outlook.MailItem
Dim str As String
Dim openPos As Integer
Dim closePos As Integer
Dim midBit As String
Dim strFolderPath
Dim fso As Object
Dim oFile As Object
Set oFile = fso.CreateTextFile(strFolderPath & midBit & ".txt")
Set fso = CreateObject("Scripting.FileSystemObject")
strFolderPath = "C:\temp\Attachments\"
str = oMail.Subject
openPos = InStr(str, "(")
closePos = InStr(str, ")")
If oMail.SenderEmailAddress = "[email protected]" Then
If Item.Attachments.Count > 0 Then
midBit = Mid(str, openPos + 1, closePos - openPos - 1)
oFile.WriteLine midBit
oFile.Close
End If
End If
Set fso = Nothing
Set oFile = Nothing
End Sub
使用ByVal Item As Object
而不是oMail As Outlook.MailItem
Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
If TypeOf Item Is Outlook.MailItem Then
Debug.Print Item.Subject
End If
End Sub