I am using the following VBA code which checks for any emails with a specific subject heading.
The problem is it checks my default outlook inbox folder when I need it to check the inbox of my other email account.
Can someone please show me how I would do this?
Sub Macro1()
Set olApp = CreateObject("Outlook.Application")
Dim olNs As Outlook.Namespace
Dim Fldr As Outlook.MAPIFolder
Dim myItem As Outlook.MailItem
Dim myAttachment As Outlook.Attachment
Dim I As Long
Dim olMail As Variant
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
Set myTasks = Fldr.Items
Set olMail = myTasks.Find("[Subject] = ""New Supplier Request: Ticket""")
If Not (olMail Is Nothing) Then
For Each myItem In myTasks
If myItem.Attachments.Count <> 0 Then
For Each myAttachment In myItem.Attachments
If InStr(myAttachment.DisplayName, ".txt") Then
I = I + 1
myAttachment.SaveAsFile "\\uksh000-file06\Purchasing\NS\Unactioned\" & myAttachment
End If
Next
End If
Next
For Each myItem In myTasks
myItem.Delete
Next
Call Macro2
Else
MsgBox "There Are No New Supplier Requests."
End If
End Sub
Instead of iterating through all folder items in Outlook:
For Each myItem In myTasks
If myItem.Attachments.Count <> 0 Then
For Each myAttachment In myItem.Attachments
I'd suggest using the Find/FindNext or Restrict methods of the Items class. Also you may consider using using the AdvancedSearch method of the Application class. Take a look at the following articles for the sample code which illustrates how to use them in the code:
How To: Use Restrict method to retrieve Outlook mail items from a folder
How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)
Advanced search in Outlook programmatically: C#, VB.NET
The reason is that you have declared variable myItem as Outlook.MailItem and you use it later to iterate through the collection of items in MAPI folder.
However, MAPI folder contains not only MailItems but also MeetingItems and every time the loop finds an object of MeetingItem type, it throws an Mismatch type error since it expects only objects of MailItem type.
You just need to change declaration of myItem variable to:
Dim myItem as Object
=============================================================
The code below should iterate through the filtered items only:
Sub Work_with_Outlook()
Dim olApp As Outlook.Application
Dim olNs As Outlook.Namespace
Dim Fldr As Outlook.MAPIFolder
Dim myItem As Object
Dim myAttachment As Outlook.Attachment
Dim olMail As Variant
Dim i As Long
Set olApp = CreateObject("Outlook.Application")
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
Set myTasks = Fldr.Items
Set olMail = myTasks.Find("[Subject] = ""test""")
While Not olMail Is Nothing
If olMail.Attachments.Count Then
For Each myAttachment In olMail.Attachments
i = i + 1
myAttachment.SaveAsFile "\\uksh000-file06\Purchasing\Supplier Attachments\test" & i & ".txt"
Next myAttachment
End If
Set olMail = myTasks.FindNext
Wend
MsgBox "Scan Complete."
End Sub
Related
I would like to explore my Outlook messages and if a message comes from email address xx#xxx, save the message in a local folder.
I tried this:
Sub FindIt()
Dim myOlApp As New Outlook.Application
Dim myNameSpace As Outlook.Namespace
Dim myInbox As Outlook.MAPIFolder
Dim myitems As Outlook.Items
Dim myitem As Object
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myitems = myInbox.Items
For Each myitem In myitems
If myitem.SenderEmailAddress = "xx#xx" Then
MsgBox (myitem.Subject)
End If
Next myitem
End Sub
I either get an error or I get nothing.
If this code works I will adjust it by replacing the MsgBox with SaveAs (XXXXX).
Here is a condensed version of your code which I tested in Excel 365. Note that I set a reference to the Microsoft Outlook Object Library (Tools > References).
Sub FindIt()
Dim myNameSpace As Outlook.Namespace
Dim myInbox As Outlook.MAPIFolder
Dim myItem As Object
Dim i As Integer
Set myNameSpace = Outlook.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
MsgBox "There are " & myInbox.Items.Count & " items in my Inbox."
For Each myItem In myInbox.Items
i = i + 1
' If myitem.SenderEmailAddress = "xx#xx" Then
MsgBox (myItem.Subject) & vbCr & myItem.SenderEmailAddress
' End If
If i > 3 Then Exit For
Next myItem
End Sub
This code isn't substantially different from your own except that it's laid out for testing. I hope it will give you an idea of what to look for next.
The following code helps me to get data from my default folder in inbox but I
want to change my folder which is a shared folder and placed in favorite folders
I already tried to change getDefaultFolder with sharedDefaultFolder but it doesn't work.
Dim olApp As Object
Dim olNs As Object
Dim olFldr As Object
Dim olItms As Object
Dim olMail As Object
Set olApp = OutlookApp()
Set olNs = olApp.GetNamespace("MAPI")
Set olFldr = olNs.GetDefaultFolder(6).Folders("impMail")
Set olItms = olFldr.Items
You cannot simply change GetDefaultFolder to GetSharedDefaultFolder, you have to also add Recipient object The owner of the folder.
expression: .GetSharedDefaultFolder(Recipient**, FolderType)
Example With Email address
Option Explicit
Public Sub Example()
Dim olNs As Outlook.NameSpace
Set olNs = Application.GetNamespace("MAPI")
Dim RecipientShareName As Outlook.Recipient
Set RecipientShareName = olNs.CreateRecipient("0m3r#email.com") 'address
RecipientShareName.Resolve
Dim ShareInbox As Outlook.Folder
Set ShareInbox = olNs.GetSharedDefaultFolder(RecipientShareName, _
olFolderInbox) 'Inbox
Dim Items As Outlook.Items
Set Items = ShareInbox.Items
Dim i As Long
Dim Item As Outlook.MailItem
For i = Items.Count To 1 Step -1
If TypeOf Items(i) Is Outlook.MailItem Then
Set Item = Items(i)
Debug.Print Item.Subject '// Print Item to Immediate window
End If
Next
End Sub
Or if your using With just Name, then make sure recipient object is resolved
Example with recipient Name
Option Explicit
Public Sub Example()
Dim olNs As Outlook.NameSpace
Set olNs = Application.GetNamespace("MAPI")
Dim RecipientShareName As Outlook.Recipient
Set RecipientShareName = olNs.CreateRecipient("0m3r") 'address
RecipientShareName.Resolve
If Not RecipientShareName.Resolved Then
MsgBox "Error on Recipient Object"
Exit Sub
Else
Dim ShareInbox As Outlook.Folder
Set ShareInbox = olNs.GetSharedDefaultFolder(RecipientShareName, _
olFolderInbox) 'Inbox
End If
Dim Items As Outlook.Items
Set Items = ShareInbox.Items
Dim i As Long
Dim Item As Outlook.MailItem
For i = Items.Count To 1 Step -1
If TypeOf Items(i) Is Outlook.MailItem Then
Set Item = Items(i)
Debug.Print Item.Subject '// Print Item to Immediate window
End If
Next
End Sub
Does your satement " set olFldr ..." give you the correct folder?
You might check your folders with a statement like:
for each myO in olNs.GetDefaultFolder(6).folders : debug.Print myO.name : next
The code found in this thread works fine for single email results: Excel VBA for searching in mails of Outlook, But it only returns the latest email.
Is it possible to adjust the code to display more than 1 result?
The code that I have from the thread is:
Option Explicit
Public Sub search_outlook()
Dim outlookapp
Dim olNs As Outlook.Namespace
Dim Fldr As Outlook.MAPIFolder
Dim olMail As Variant
Dim myTasks
Dim projIDsearch As String
projIDsearch = ActiveCell.Cells(1, 4)
Set outlookapp = CreateObject("Outlook.Application")
'Set outlookapp = New Outlook.Application
Set olNs = outlookapp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox).Folders("ExemptionReview")
Set myTasks = Fldr.Items
For Each olMail In myTasks
If (InStr(1, olMail.Subject, projIDsearch, vbTextCompare) > 0) Then
olMail.Display
Exit For
End If
Next
End Sub
Remove the Exit For. This drops out of the loop when a match is found:
olMail.Display
Exit For '<Remove this
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
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).
I am using the following vba code which checks for any emails with a specific subject heading.
The problem is it checks my default outlook inbox folder when I need it to check the inbox of my other email account NewSuppliers#Hewden.co.uk
Can someone please show me how I would do this? Thanks in advance
Sub Macro1() Set olApp = CreateObject("Outlook.Application")
Dim olNs As Outlook.Namespace
Dim Fldr As Outlook.MAPIFolder
Dim myItem As Outlook.MailItem
Dim myAttachment As Outlook.Attachment
Dim I As Long
Dim olMail As Variant
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
Set myTasks = Fldr.Items
Set olMail = myTasks.Find("[Subject] = ""New Supplier Request: Ticket""")
If Not (olMail Is Nothing) Then
For Each myItem In myTasks
If myItem.Attachments.Count <> 0 Then
For Each myAttachment In myItem.Attachments
If InStr(myAttachment.DisplayName, ".txt") Then
I = I + 1
myAttachment.SaveAsFile "\\uksh000-file06\Purchasing\NS\Unactioned\" & myAttachment
End If
Next
End If
Next
For Each myItem In myTasks
myItem.Delete
Next
Call Macro2
Else
MsgBox "There Are No New Supplier Requests."
End If
End Sub
outlook folder structure:
account1#hewden.co.uk
Inbox
Drafts
Sent
NewSuppliers#hewden.co.uk
Inbox
Drafts
Sent
You need to use the following, assuming that the folder you want is at the same level in the folder hierarchy
Set Items = Session.GetDefaultFolder(olFolderCalendar).Parent.Folders("YouFolderName").Items
See here for more details ... http://www.slipstick.com/developer/working-vba-nondefault-outlook-folders/
Have you tried the following function from the above link ...
Function GetFolderPath(ByVal FolderPath As String) As Outlook.Folder
Dim oFolder As Outlook.Folder
Dim FoldersArray As Variant
Dim i As Integer
On Error GoTo GetFolderPath_Error
If Left(FolderPath, 2) = "\\" Then
FolderPath = Right(FolderPath, Len(FolderPath) - 2)
End If
'Convert folderpath to array
FoldersArray = Split(FolderPath, "\")
Set oFolder = Application.Session.Folders.item(FoldersArray(0))
If Not oFolder Is Nothing Then
For i = 1 To UBound(FoldersArray, 1)
Dim SubFolders As Outlook.Folders
Set SubFolders = oFolder.Folders
Set oFolder = SubFolders.item(FoldersArray(i))
If oFolder Is Nothing Then
Set GetFolderPath = Nothing
End If
Next
End If
'Return the oFolder
Set GetFolderPath = oFolder
Exit Function
GetFolderPath_Error:
Set GetFolderPath = Nothing
Exit Function
End Function
You may need to use the technique here first to find out the actual folder name ... https://msdn.microsoft.com/en-us/library/office/ff184607.aspx
In the image below, the Drafts, Clients, Outbox folders are all on the same level (they share the same parent folder james#...com), but the ChildFolder folder is not (it's parent is Drafts).
You should be able to just specify the name of your 2nd mailbox as a Folder.
Set Fldr = olNs.Folders("My 2nd mailbox").Folders("Sub Folder")
If you want the main inbox of the 2nd account, then you specify this as the sub folder of the account.
Set Fldr = olNs.Folders("My 2nd Inbox").Folders("Inbox")
Note that it's the name of the mailbox you use, not the e-mail address. Let me know how you get on.