Reputation: 543
I am trying to search for an Outlook email with a specific subject from Excel:
Sub Work_with_Outlook()
Set olApp = CreateObject("Outlook.Application")
Dim olNs As Outlook.Namespace
Dim Fldr As Outlook.MAPIFolder
Dim olMail As Variant
Dim sir() As String
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
Set myTasks = Fldr.Items
Set olMail = myTasks.Find("[Subject] = ""*desired subject*""")
If Not (olMail Is Nothing) Then
sir = Split(olMail.Body, vbCrLf)
For i = 1 To UBound(sir)
ActiveWorkbook.Sheets("Sheet1").Cells(i, 1).Value = sir(i)
Next i
olMail.Delete
End If
End Sub
I get a error in Excel saying
user defined type not defined
on this line:
Dim olNs As Outlook.Namespace
Upvotes: 2
Views: 6402
Reputation: 3940
You need to add reference to Outlook in your VBA Project.
In VBA editor menu bar click Tools -> References and check Microsoft Outlook 14.0 Object Library (version number can be different than 14.0, depending on your version of MS Office).
Upvotes: 2