dimanche 23 juin 2019

How to use If-Then statement in VBA to open one of saved two .MSG files?

I want an if-then conditional to detect whether a cell has a certain value and open an already saved .msg Outlook email on my drive

I believe my conditionals structure is correct , but I can't seem to open .msg files as I would Workbooks

Sub OpenMail()

Workbooks("MyBook").Sheets("Sheet1").Activate

Dim MyItem1 As Outlook.MailItem
Dim MyItem2 As Outlook.MailItem


If Range("A1").Value > 0 Then 
      Set MyItem1.Open = "C:\Users\jeff\OneDrive\Documents\Email #1.msg"
      MyItem1.Display

Else If Range("A1").Value < 0 Then
      Set MyItem2.Open = "C:\Users\jeff\OneDrive\Documents\Email #2.msg"
      MyItem2.Display
Else 
      MsgBox("No items to open")

End If

End Sub

When I run it, it gives me a Run-time error 91

Object variable or With block variable not set

I connected Outlook in Reference Library (under tools)

Any thoughts? Thanks!

Aucun commentaire:

Enregistrer un commentaire