如何返回通过参数传递的对象的属性?

问题描述 投票:0回答:1

我正在尝试阅读电子邮件主题,并将其存储为变量。

出于测试目的,我将其设置为用作文本文件的文件名。

我的代码返回一个空字符串。

  1. 我将电子邮件发送到ThisOutlookSession中存在以下代码的电子邮件地址
  2. 电子邮件主题为Topic (923832)
  3. 代码执行没有错误
  4. 文本文件名为空

为什么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
vba outlook outlook-vba
1个回答
1
投票

使用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
© www.soinside.com 2019 - 2024. All rights reserved.