The follow MS Office 2010 VBA excerpt loops through emails in Outlook inbox only shows emails that have been received before a certain date( more than a week ago).
For Each obj In olFolder.Items
If TypeName(obj) = "MailItem" Then
Debug.Print obj.Subject & Chr(10) & obj.SenderEmailAddress & Chr(10) & obj.ReceivedTime
End If
Next
Many more emails show in the very same account and very same folder in Outlook itself.
What can this issue be?
Are you sure the script does not raise an error? This will happen if you have an item other than MailItem (e.g. ReportItem or MeetingRequest) and you try to access a property not exposed by that object.
Related
I need some help with VBA to count emails from 7 different subfolders of Outlook.
The result must show the number of emails in these subfolders and the date of the last email.
This subfolder is added as an extension to my Outlook only for processing data and is not my actual Outlook email. It has further subfolders inside of it which needs to be counted.
I hope someone can help me with this.
Thanks in advance
It seems you need to iterate over all folder recursively because the nesting level is unknown. The following code iterates over all folders in Outlook recursively:
sub ProcessFolders(Folders)
for each folder in Folders
if Folder.DefaultItemType = olMailItem Then
Debug.Print "--------- " & folder.Name
End If
ProcessFolders(folder.Folders)
next
end sub
Here is how you could invoke it:
ProcessFolders(Application.Session.Folders)
The result must show the number of emails in these subfolders and the date of the last email.
The Items.Count property returns a long indicating the count of objects in the specified collection of folder. For example:
folderInstance.Items.Count
To find the date of last email (I suppose the last received one) you need to sort the collection using the Items.Sort method which orts the collection of items by the specified property. The index for the collection is reset to 1 upon completion of this method.
Set myItems = myFolder.Items
myItems.Sort "[ReceivedTime]", True
MsgBox myItems(1).ReceivedTime
Here is the scenario:
My Outlook Inbox has a sub-folder called Notice. Every day, I will check and forward notices to my colleague if there is any automated notice in this folder. I would like to run a vba so that it will go into this folder, check inside, and if there is an email then forward, otherwise stop.
I would seek for your assistance on this scenario as I'm quite new to visual basic on outlook. Thank you very much.
Tony
You could create a macro rule when the folder received an email then forward this email.
Please refer to the below code:
Sub ForwardEmail(Item As Outlook.MailItem)
// Determine if it’s an email
If TypeName(Item) = "MailItem" Then
With Item.Forward
.Subject = ("ITS - ") & Item.Subject
.Recipients.Add "backup#email.com"
' You need to overwrite the Body or HTMLBody to get rid of the auto signature
.HTMLBody = Item.HTMLBody ' <-- Or use .Body for Plain Text
'.Display ' <-- For Debug
.Send ' <-- Put break here to Debug
End With
End If
End Sub
For more information, please refer to these links:
Otlook vba and rule to forward email message and change subject
VBA Copy sent mail to folder based on key words in subject
I want to send a Skype meeting invite through excel macros. Is there a way to do it? Currently I can only create a normal meeting invite.
Dim olApp As Outlook.Application
Dim ol_Meeting As Outlook.AppointmentItem
Set olApp = New Outlook.Application
Set ol_Meeting = olApp.CreateItem(olAppointmentitem)
ol_Meeting.MeetingStatus = olMeeting
With ol_Meeting
.Display
.Attachments.Add ThisWorkbook.Path & "\" & ThisWorkbook.Name
End With
Assuming you're using Skype for Business and assume the macro is for you to use you can just create an outlook meeting invite and adding the automatic Skype link, right click on skype meeting link, edit hyperlink, copy it and add to your macro's email body.
This will populate your email with the link to access any Skype Meeting that you usually send. It works for me.
I have adjusted a routine found on the internet in Outlook VBA that moves all emails from a conversation in inbox to a specific folder.
I move my emails by getting them like:
olItem As MailItem 'Put email from conversation in olItem
DestFolder As Outlook.Folder 'Destination folder where i want to send my email
olItem.Move DestFolder
Problem is: in this conversation I have sometimes older emails that have already been moved to the destination folder earlier: they appear in my inbox because of the way conversation mode works.
If I try to move it with olItem.Move DestFolder, the code fails as the email is already in DestFolder.
How to detect if an email is already in the destination folder and move it to ONLY if it's not there already
Thank you in advance for your help
A simple method that may suffice.
On Error Resume Next
olItem.Move DestFolder
' Turn error bypass off once the purpose for it has been served
On Error GoTo 0
I have VBA code to download attachment immediately after mail comes into Outlook.
How do I invoke this code or is there another way to download attachments automatically?
Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim saveFolder As String
saveFolder = "D:\outlook\"
For Each objAtt In itm.Attachments
MsgBox objAtt
objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
MsgBox saveFolder & "\" & objAtt.DisplayName
Set objAtt = Nothing
Next
End Sub
It looks like you are trying to create a rule which triggers a VBA script for saving attachments on the disk. If so, I'd recommend removing any MsgBox statements from the code. It may stop the code from running. Instead, you may use the Debug.Print statements in the code. See Where does VBA Debug.Print log to? for more information.
Instead of using rules you may consider handling the NewMailEx event of the Application class. The 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.
Finally, you may find the Getting Started with VBA in Outlook 2010 article helpful.