MailItem.Move method for mail that is read - excel

I'm trying to first check if mails in a specific folder ("pending") are read. If the mail is read, move it to another specific folder ("done").
For the remaining mails in "pending", save the attachments and mark them as "read".
When I try to move each read mail from the "pending" folder to "done" with ".Move", drops
"error 424. An object is required".
The problem is OItem.Move OFolderDst.
Sub MoveInbox2Reviewed()
Dim OutlookApp As Outlook.Application
Dim ONameSpace As Object
Dim OItem As Outlook.MailItem
Dim OFolderSrc As Object
Dim OFolderDst As Object
Dim Path As String
Set OutlookApp = New Outlook.Application
Set ONameSpace = OutlookApp.GetNamespace("MAPI")
Set OFolderSrc = ONameSpace.GetDefaultFolder(olFolderInbox).Folders("pending")
Set OFolderDst = ONameSpace.GetDefaultFolder(olFolderInbox).Folders("done")
Path = "C:\Users\..."
For Each OItem In OFolderSrc.Items.Restrict("[Unread] = False")
OItem.Move OFolderDst 'The problem is here
Next
For Each OItem In OFolderSrc.Items
OItem.Attachments.SaveAsFile Path & Attachment.DisplayName
OItem.UnRead = False
Next
End Sub

Each time the Move call is made the collection is decreased by one item. So, I'd recommend using the reverse for loop instead for moving items from a folder. For example:
for i = Items.Count to 1 step -1
Items(i).Move folder
next
So, in the code you may get all unread items (represented by the Items collection) and then call a reverse loop to move items.

Related

How to loop through items in a folder?

CONTEXT: I'm trying to first check if mails in a folder ("pending") are read. If the mail is read, move it to another folder ("done").
Then, for the remaining mails in "pending", save the attachments and mark the mail as "read".
PROBLEM: When I try to loop through the items in the folder it drops
Run-time error 438:
The object doesn't support this property or method
CODE:
Sub MoveInbox2Reviewed()
Dim OutlookApp As Outlook.Application
Dim ONameSpace As Object
Dim OItem As Outlook.MailItem
Dim OFolderSrc As Object
Dim OFolderDst As Object
Dim Path As String
Set OutlookApp = New Outlook.Application
Set ONameSpace = OutlookApp.GetNamespace("MAPI")
Set OFolderSrc = ONameSpace.GetDefaultFolder(olFolderInbox).Folders("pending")
Set OFolderDst = ONameSpace.GetDefaultFolder(olFolderInbox).Folders("done")
Path = "C:\Users\..."
For Each OItem In OFolderSrc
If OItem.UnRead = False Then
OItem.Move OFolderDst
End If
Next
For Each OItem In OFolderSrc
OItem.Attachments.SaveAsFile Path & Attachment.DisplayName
OItem.UnRead = False
Next
End Sub
Firstly, you are looping through the folders. not items. You need OFolderSrc.Items.
Secondly, you should never loop through all items in a folder, use Items.Restrict:
For Each OItem In OFolderSrc.Items.Restrict("[Unread] = true")

Extract pdf files from an email from a specific contact (sender) and different inbox address in outlook

Good morning friends,
Please, I need your help, I have 2 problems:
1.- I wanted to be able to extract pdf files but an email from a specific contact (sender)
2.- I have several inboxes, how could I set another inbox, but not the one that comes by default - here I tried the following "Set Inbox = olNs.GetDefaultFolder (onothermail#gmail.com)" but it did not work for me
Thank you very much in advance
Option Explicit
Public Sub Example()
'// Declare your Variables
Dim olNs As Outlook.NameSpace
Dim Inbox As Outlook.MAPIFolder
Dim Items As Outlook.Items
Dim Item As Outlook.MailItem
Dim Atmt As Attachment
Dim Filter As String
Dim FilePath As String
Dim AtmtName As String
Dim i As Long
Dim objOwner As Outlook.Recipient
'// Set Inbox Reference
Set olNs = Application.GetNamespace("MAPI")
Set objOwner = olNs.CreateRecipient("secondMail#gmail.com")
Set Inbox = olNs.GetSharedDefaultFolder(objOwner)
FilePath = "C:\Users\Unity\Desktop\adjuntos\"
Filter = "[Unread] = True"
Set Items = Inbox.Items.Restrict(Filter)
'// Loop through backwards
For i = Items.Count To 1 Step -1
Set Item = Items(i)
DoEvents
If Item.Class = olMail Then
If Item.SenderEmailAddress = "senderx#gmail.com" Then
For Each Atmt In Item.Attachments
AtmtName = FilePath & Atmt.FileName
If ((InStr(Atmt.DisplayName, ".jpg") Or InStr(Atmt.DisplayName, ".zip") Or InStr(Atmt.DisplayName, ".PDF") Or InStr(Atmt.DisplayName, ".pdf"))) Then
Atmt.SaveAsFile FilePath & "\" & Atmt.DisplayName
End If
Item.UnRead = False
Next
End If
End If
Next
Set Inbox = Nothing
Set Items = Nothing
Set Item = Nothing
Set Atmt = Nothing
Set olNs = Nothing
End Sub
It seems additionally you need to check the sender's email address of the item. The MailItem.SenderEmailAddress property returns a string that represents the email address of the sender of the Outlook item.
Sub SetFlagIcon()
Dim mpfInbox As Outlook.Folder
Dim obj As Outlook.MailItem
Dim i As Integer
Set mpfInbox = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Folders("Test")
' Loop all items in the Inbox\Test Folder
For i = 1 To mpfInbox.Items.Count
If mpfInbox.Items(i).Class = olMail Then
Set obj = mpfInbox.Items.Item(i)
If obj.SenderEmailAddress = "someone#example.com" Then
'Set the yellow flag icon
obj.FlagIcon = olYellowFlagIcon
obj.Save
End If
End If
Next
End Sub
However, iterating over all items in the folder is not really a good idea. Use the Find/FindNext or Restrict methods of the Items class. Read more about these methods in the following articles:
How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)
How To: Use Restrict method to retrieve Outlook mail items from a folder
Also you may use the AdvancedSearch method of the Application class helpful. See Advanced search in Outlook programmatically: C#, VB.NET for more information.
Use the Store.GetDefaultFolder method instead. It returns a Folder object that represents the default folder in the store and that is of the type specified by the FolderType argument. This method is similar to the GetDefaultFolder method of the NameSpace object. The difference is that this method gets the default folder on the delivery store that is associated with the account, whereas NameSpace.GetDefaultFolder returns the default folder on the default store for the current profile.

Excel/Outlook VBA - select folders in different Mailboxes

I'd like to collect some email count data from Outlook using Excel VBA. In addition to my personal email, I also have access to other mailboxes account/stores(?). The label on the Outlook mailbox/accounts are HR,Marketing, Accounting. I'd like to loop through the folders in these mailboxes/stores and perform some task.
When I write a for loop through Sessions.Accounts, it only accesses my default mailbox (i.e. not HR,Marketing,Accounting). It looks like I can access my non-default mailboxes if I loop through Session.Stores:
Sub test()
Dim olApp As Outlook.Application
Set olApp = Outlook.Application
For Each acct In olApp.Session.Stores
MsgBox acct
'how can I access folders and emails within these Stores?
End Sub
This returns message boxes with HR, Marketing, Accounting. I am not able to select the inbox or other folders within these non-default mailboxes though.
For my default mailbox, I was able to get it:
Sub default_email_count()
Dim olApp As Outlook.Application
Dim objNS As Outlook.Namespace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set olFolder = olbNS.GetDefaultFolder(olFolderInbox)
Set olFolder = olFolder.Folders("sample_folder")
Count = olFolder.Items.Count
Msgbox Count
'this code successfully outputs the count of emails in my default mailbox within the folder "sample folder"
How can I extend the above code to work across other Stores and not just my default mailbox?
Thanks in advance.
The following code shows how to enumerate all folders on all stores for an Outlook session.
The code sample begins by getting all the stores for the current session using the NameSpace.Stores property of the current session, Application.Session.
For each store of this session, it uses Store.GetRootFolder to obtain the folder at the root of the store.
For the root folder of each store, it iteratively calls the EnumerateFolders procedure until it has visited and displayed the name of each folder in that tree.
Sub EnumerateFoldersInStores()
Dim colStores As Outlook.Stores
Dim oStore As Outlook.Store
Dim oRoot As Outlook.Folder
On Error Resume Next
Set colStores = Application.Session.Stores
For Each oStore In colStores
Set oRoot = oStore.GetRootFolder
Debug.Print (oRoot.FolderPath)
EnumerateFolders oRoot
Next
End Sub
Private Sub EnumerateFolders(ByVal oFolder As Outlook.Folder)
Dim folders As Outlook.folders
Dim Folder As Outlook.Folder
Dim foldercount As Integer
On Error Resume Next
Set folders = oFolder.folders
foldercount = folders.Count
'Check if there are any folders below oFolder
If foldercount Then
For Each Folder In folders
Debug.Print (Folder.FolderPath)
EnumerateFolders Folder
Next
End If
End Sub
If the mailboxes are already in your profile, use the Namespace.Stores collection. Default folders can be accessed using Store.GetDefaultFolder (instead of Namespace.GetDefaultFolder).
You can also use Namespace.GetSharedDefaultFolder - it takes Recipient object as one of its parameters (you can get it from Namespace.CreateRecipient).

How do I save Excel attachments in new mail based on both time received and subject line?

I'm fairly new to coding. I would like to know how to save particular Excel attachments in Outlook inbox ("Morning Emails") using subject line keywords and the times received.
I receive five new emails each day, with Excel attachments to save to the same drive folder.
The time received can be either last night or early this morning.
The names of all five files and times received are different.
The inbox does not empty. I don't want to save what I saved yesterday or 2 weeks ago.
Sub SaveAttachments()
Dim ol As Outlook.Application
Dim ns As Outlook.Namespace
Dim fol As Outlook.Folder
Dim i As Object
Dim mi As Outlook.MailItem
Dim at As Outlook.Attachment
Set ol = New Outlook.Application
Set ns = ol.GetNamespace("MAPI")
Set fol = ns.Folders(1).Folders("Morning Emails")
For Each i In fol.Items
If i.Class = olMail Then
Set mi = i
If mi.Attachments.Count > 0 Then
For Each at In mi.Attachments
at.SaveAsFile "C:\Users\nader\OneDrive\Documents\" & _
at.Filename & Format(mi.ReceivedTime, " MM-DD-YYYY")
Next at
End If
End If
Next i
End Sub
"Attempted operation failed; object could not be found" for line: Set fol = ns.Folders(1).Folders("Morning Emails"), even though I have created that sub-folder under my Outlook inbox.
Set ol = New Outlook.Application
There is no need to create a new Outlook Application instance in the code. Use the Application property to get the host application instance in Outlook VBA macros.
I'd recommend handling the NewMailEx event of the Application class. This event fires once for every received item that is processed by Microsoft Outlook. The item can be one of several different item types, for example, MailItem, MeetingItem, or SharingItem. The EntryIDsCollection string contains the Entry ID that corresponds to that item.
The NewMailEx event fires when a new message arrives in the Inbox and before client rule processing occurs. You can use the Entry ID returned in the EntryIDCollection array to call the NameSpace.GetItemFromID method and process the item.
Private Sub outApp_NewMailEx(ByVal EntryIDCollection As String)
Dim itm as Object
Set itm = NS.GetItemFromID(EntryIDCollection)
Debug.Print "mail received"
If itm.Class = olMail Then
Dim it as Outlook.MailItem
Set it = itm
if it.Subject = "your subject" then
If it.Attachments.Count > 0 Then
For Each at In mi.Attachments
at.SaveAsFile "C:\Users\nader\OneDrive\Documents\" & _
at.Filename & Format(mi.ReceivedTime, " MM-DD-YYYY")
Next at
End If
End If
End If
End Sub

VBA Runtime Error 13 Type Mismatch

I am trying to generate emails in a chosen Outlook folder from an Excel macro.
In the code below, 'TestDraft1' works, generating an email in the folder chosen by the user via the user the Namespace.PickFolder method.
However, since I want to generate many emails in the same folder, I need the user to be able to pick the folder just once. In 'TestDraft2' below, I try to save the folder as an Object, then pass it to the move function. This fails with "Runtime Error 13, Type Mismatch". The 'folder' variable gives the type "MAPIFolder".
What am I missing here? How is the object I assign to 'folder' different than the one I pass to 'Mailitem.move' ?
Edit Note that I'm using late-binding, which is why I'm defining my variables as Objects. I'd prefer to keep it that way, since I'd like to be able to distribute it without special configuration. What I really want to know is why the first version succeeds while the second version fails. Further details: My employer uses Office 2007.
Edit 2 The problem had nothing to do with what the type of object returned by PickFolder. It had to do with my misuse of VBA syntax. I've answered the question below, and I'm renaming the it to better address what I was really asking. Original title was: Returning an Outlook Folder object in Excel VBA using PickFolder
Sub test()
Call TestDraft1("test#example.com", "test1") ' succeeds
Call TestDraft2("test#example.com", "test2") ' fails
End Sub
Sub TestDraft1(recip As String, subj As String)
Dim OlApp As Object
Dim NS As Object
Dim OlMail As Object
Set OlApp = CreateObject("Outlook.Application")
Set NS = OlApp.GetNamespace("MAPI")
Set OlMail = OlApp.createitem(0) 'olMailitem = 0
OlMail.Subject = subj
OlMail.Recipients.Add (recip)
OlMail.Move (NS.PickFolder) ' pass the results of the folder-picker directly
End Sub
Sub TestDraft2(recip As String, subj As String)
Dim OlApp As Object
Dim NS As Object
Dim folder As Object
Dim OlMail As Object
Set OlApp = CreateObject("Outlook.Application")
Set NS = OlApp.GetNamespace("MAPI")
Set folder = NS.PickFolder ' save the results of the folder-picker...
' MsgBox (TypeName(folder)) ' returns "MAPIFolder
Set OlMail = OlApp.createitem(0) 'olMailitem = 0
OlMail.Subject = subj
OlMail.Recipients.Add (recip)
OlMail.Move (folder) ' ... and use the saved results of the folder-picker -> runtime error
End Sub
This shows how new I am to VBA syntax.
Replacing:
OlMail.Move (folder)
with
OlMail.Move folder
fixed the problem.
This is what I believe is going on: MailItem.Move returns no result, so it uses the non-parentheses syntax.
See: http://msdn.microsoft.com/en-us/library/office/gg278645
Thus, (folder) is interpreted as an expression, and returns the text value of the folder name inside the parentheses. Evidence:
Dim testobj as Object
Set testobj = (folder) ' fails: Object required
Dim testvar as Variant
testvar = (folder)
MsgBox (VarType(testvar) = vbString) ' returns True
So, while folder is an object of the correct type, (folder) is an expression which evaluates to a string.
maybe you just miss the objmail.save before moving it.
and your definitions are a problem for sure:
Change the first lines of your Sub as follows (untested):
Sub TestDraft2(recip As String, subj As String)
Dim olApp As Outlook.Application
Dim NS As Outlook.Namespace
Dim folder As Outlook.MAPIFolder
Dim OlMail As Outlook.mailitem
Set OlApp = CreateObject("Outlook.Application")
Set NS = OlApp.GetNamespace("MAPI")
Set folder = NS.PickFolder
that could help...

Resources