如何从Exchange和普通电子邮件中获取发件人

时间:2019-05-22 12:24:44

标签: vba outlook outlook-vba

嗨,我对宏几乎是菜鸟,对不起,如果我遗漏了一些东西。所以请多多包涵我。

我正在尝试编写宏,目的是在工作时收到邮件时发送邮件。然后,宏应将邮件发送到短信服务,该服务将邮件转换为短信并将其发送到我的手机。该消息将包含一条短消息,邮件发件人地址以及发送和接收时间。

我整理了两个宏。我通过搜索互联网发现的

第一条代码位于此链接tachytelic.net上 和我在这里找到的折扣 stackoverflow.com

这是失败的代码部分。

'variable for select case
Dim EmailFrom As String
Dim OldMessage As Outlook.MailItem

Set OldMessage = Application.ActiveInspector.CurrentItem

'Puts sender mail address in variable both ordinary mail and Exchange emails.
Select Case OldMessage.SenderEmailType
    Case "EX"
       EmailFrom = OldMessage.Sender.GetExchangeUser.PrimarySmtpAddress
    Case Else
       EmailFrom = OldMessage.SenderEmailAddress
End Select

'Sends E-mail to sms service.
If TypeName(Item) = "MailItem" Then
    With olEmail
        .BodyFormat = olFormatPlain
        .To = "some@mail.com"
        .Subject = "You got a new E-mail!"
        .Body = EmailFrom & vbCrLf & "Sendt: " & Item.SentOn & vbCrLf & "Modtaget: " & Item.ReceivedTime
        .Send
    End With
End If

运行此代码时,出现运行时错误91-未设置对象变量或With块变量。

我尝试使用F8键,但这是不可能的,我不知道为什么。

然后,我获取原始代码并将其粘贴到模块中。然后,我可以使用F8键浏览代码。当我到达这一行时,就会出现错误。

Set OldMessage = Application.ActiveInspector.CurrentItem

谁能告诉我我做错了什么以及我可以做些什么来解决呢? THX


这是整个代码

Option Explicit
Private WithEvents inboxItems As Outlook.Items
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

Sub inboxItems_ItemAdd(ByVal Item As Object)

On Error GoTo ErrorHandler

'variable for if statments
Dim olApp As Outlook.Application
Dim olEmail As Outlook.MailItem

Set olApp = New Outlook.Application
Set olEmail = olApp.CreateItem(olMailItem)

'variable for select case
Dim EmailFrom As String
Dim OldMessage As Outlook.MailItem

'Deletes sms status mails I recieve when I mail to sms service
If InStr(Item.Subject, "SMS status") > 0 Then
    Item.UnRead = False
    Item.Save
    Item.Delete
    End
End If

'Puts sender mail address in variable both ordinary mail and Exchange emails.
Select Case OldMessage.SenderEmailType
    Case "EX"
       EmailFrom = OldMessage.Sender.GetExchangeUser.PrimarySmtpAddress
    Case Else
       EmailFrom = OldMessage.SenderEmailAddress
End Select

'Sends E-mail to sms service.
If TypeName(Item) = "MailItem" Then
    With olEmail
        .BodyFormat = olFormatPlain
        .To = "some@mail.com"
        .Subject = "You got a new E-mail!"
        .Body = EmailFrom & vbCrLf & "Sendt: " & Item.SentOn & vbCrLf & "Modtaget: " & Item.ReceivedTime
        .Send
    End With
End If

ExitNewItem:
    Exit Sub
ErrorHandler:
    MsgBox Err.Number & " - " & Err.Description
    Resume ExitNewItem
End Sub 

然后,更容易获得概述。

那么我如何让ActiveInspector来处理收到的邮件并将其保存在OldMessage中?那就是我认为应该做的。但是,如果我错了,请纠正我。

1 个答案:

答案 0 :(得分:0)

如果没有任何打开的项目窗口,则ActiveInspector将为Nothing。另外,根据您提供的代码示例,未在任何地方声明或设置Item变量,因此您可能还会在此行上得到错误:

If TypeName(Item) = "MailItem" Then