How to get body of the email from a shared folder? - excel

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

Related

How to save messages in a local folder?

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.

Trying to move emails in a loop, but not all get moved in the first run

The VBA code does not move all emails with a certain words in the Subject "has been updated" and "Item" from the inbox to the subfolder "Neu". Emails should be already read. After 5-6 iterations , all emails will be moved. But why doesn't it work immediately after the first time of code running? Maybe you have faced the same problem? Out of 46 emails, 26 of them are moved firstly, then 39, then 44 and then 46.
Thank you very much in advance!
Sub Emails_Outlook_Transport()
Dim olApp As Outlook.Application
Set olApp = New Outlook.Application
Dim olNS As Outlook.Namespace
Set olNS = olApp.GetNamespace("MAPI")
Dim olFldr As Outlook.MAPIFolder
Set olFldr = olNS.GetDefaultFolder(olFolderInbox)
Dim Items As Outlook.Items
Set Items = olFldr.Items
Dim newFldr As Outlook.MAPIFolder
Set newFldr = olFldr.Folders("Neu")
Dim msg As Object
Dim olMailItem As MailItem
Dim Found As Boolean
On Error Resume Next
For Each msg In Items
If TypeOf msg Is MailItem And msg.UnRead = False Then
Set olMailItem = msg
If InStr(olMailItem.Subject, "has been updated") > 0 And InStr(olMailItem.Subject, "Item") > 0 Then
olMailItem.Move newFldr
End If
End If
Next
End Sub
No error messages, just not a proper work of the code
Problem:
Apparently when the items are moved around, it messes with the item being referred in the loop in case of For Each loop
Solution:
Work a Loop after counting the Items and Backwards.So that each item is referred by an Index.
Try this:
Sub Emails_Outlook_Transport()
Dim olApp As Outlook.Application
Set olApp = New Outlook.Application
Dim olNS As Outlook.NameSpace
Set olNS = olApp.GetNamespace("MAPI")
Dim olFldr As Outlook.MAPIFolder
Set olFldr = olNS.GetDefaultFolder(olFolderInbox)
Dim Items As Outlook.Items
Set Items = olFldr.Items
Dim newFldr As Outlook.MAPIFolder
Set newFldr = olFldr.Folders("Neu")
Dim msg As Object
Dim olMailItem As MailItem
Dim Found As Boolean
Dim i As Integer
For i = Items.Count To 1 Step -1
If TypeOf Items(i) Is MailItem And Items(i).UnRead = False Then
Set olMailItem = Items(i)
If InStr(olMailItem.Subject, "has been updated") > 0 And InStr(olMailItem.Subject, "Item") > 0 Then
olMailItem.Move newFldr
End If
End If
Next
End Sub

How to get recipient email address from Excel?

I'm trying to get the .To email address from my sent box using Excel-VBA. However, To only returns the name not the email address. After some search found that the recipient should be what I'm looking for. Tried by following the msdn guide, but the code does not seem to work.
Sub test()
Dim objoutlook As Object
Dim objNamespace As Object
Dim olFolder As Object
Dim OutlookMail As outlook.MailItem
Set objoutlook = CreateObject("Outlook.Application")
Set objNamespace = objoutlook.GetNamespace("MAPI")
Set olFolder = objNamespace.GetDefaultFolder(olFolderSentMail)
Set OutlookMail = objoutlook.CreateItem(olMailItem)
Dim recips As outlook.Recipients
Dim recip As outlook.Recipient
Dim pa As outlook.PropertyAccessor
Const PR_SMTP_ADDRESS As String = _
"http://schemas.microsoft.com/mapi/proptag/0x39FE001E"
Set recips = OutlookMail.Recipients
For Each recip In recips 'Something is wrong here
Set pa = recip.PropertyAccessor
Debug.Print recip.Name & " SMTP=" & pa.GetProperty(PR_SMTP_ADDRESS)
Next
Set olFolder = Nothing
Set objNamespace = Nothing
Set objoutlook = Nothing
End Sub
I'm not really familiar with VBA, please guide along.
You can try this:
Private Sub GetRecipientSMTP(objAllRecip As Outlook.Recipients)
Dim objRecip As Outlook.Recipient
Dim objExUser As Outlook.ExchangeUser
Dim objExDisUser As Outlook.ExchangeDistributionList
For Each objRecip In objAllRecip
Select Case objRecip.AddressEntry.AddressEntryUserType
Case 0, 10
Set objExUser = objRecip.AddressEntry.GetExchangeUser
If Not objExUser Is Nothing Then _
Debug.Print objExUser.PrimarySmtpAddress '/* or copy somewhere */
Case 1
Set objExDisUser = objRecip.AddressEntry.GetExchangeDistributionList
If Not objExDisUser Is Nothing Then _
Debug.Print objExDisUser.PrimarySmtpAddress '/* or copy somewhere */
Case Else
'/* Do nothing, recipient not recognized */
End Select
Next
End Sub
You can run it in your sub like below using recips from your code (or see sample usage).
GetRecipientSMTP recips
Basically, this will check on the each Recipient on Recipients you supplied. Then will check if it is an ExchangeUser type or ExchangeDistributionList before returning the PrimartSMTPAddress. HTH.
Sample Usage:
Sub marine()
Dim olApp As Outlook.Application
Dim olNs As Outlook.Namespace
Dim olFolder As Outlook.Folder
Dim olMail As Outlook.MailItem
Dim i As Integer
Set olApp = GetObject(, "Outlook.Application") '/* assuming OL is running */
Set olNs = olApp.GetNamespace("MAPI")
Set olFolder = olNs.GetDefaultFolder(olFolderInbox)
With olFolder
For i = .Items.Count To 1 Step -1
If TypeOf .Items(i) Is MailItem Then
Set olMail = .Items(i)
GetRecipientSMTP olMail.Recipients
End If
Exit For '/* I just want to process the first mail */
Next
End With
End Sub
Note: I used early binding and set reference to Outlook Object Library.
Quick Example
Option Explicit
Public Sub Example()
Dim OUTLOOK_APP As Outlook.Application
Dim olNs As Outlook.Namespace
Dim SENT_FLDR As MAPIFolder
Dim Items As Outlook.Items
Dim olRecip As Outlook.Recipient
Dim olRecipAddress As String
Dim i As Long
Set OUTLOOK_APP = New Outlook.Application
Set olNs = OUTLOOK_APP.GetNamespace("MAPI")
Set SENT_FLDR = olNs.GetDefaultFolder(olFolderSentMail)
Set Items = SENT_FLDR.Items
For i = Items.Count To 1 Step -1
DoEvents
If Items(i).Class = olMail Then
For Each olRecip In Items(i).Recipients
olRecipAddress = olRecip.Address
Debug.Print olRecipAddress
Next
End If
Next
End Sub
this is my way of getting Recipient email Address. I hope it would help you.
Sub CopyCurrentContact()
Dim objRcp As Outlook.Recipient
Dim objRcpS As Outlook.Recipients
Dim rcpStr As String
Set outLookObj = CreateObject("Outlook.Application")
Set InspectorObj = outLookObj.ActiveInspector
Set ItemObj = InspectorObj.CurrentItem
Set objRcpS = ItemObj.Recipients
For Each objRcp In objRcpS
rcpStr = objRcp.Address & "; " & rcpStr
Debug.Print rcpStr
Next objRcp
End Sub

Counting the number of emails in an Outlook Drafts folder

I am trying to count the number of emails in my Outlook "Drafts" folder from Excel VBA.
I have not programmed using the Outlook object hierarchy so I am having some trouble.
Below is one of the variations I've tried.
Dim objOutlook As Object
Set objOutlook = CreateObject("Outlook.Application")
Dim objNameSpace As Object
Set objNameSpace = objOutlook.GetNamespace("MAPI")
Set items = objNameSpace.GetDefaultFolder(olFolderDrafts).items
MsgBox items.count
When I run this code I receive the error:
Run-time error 5 Invalid procedure call or argument
I was able to answer my own question. Firstly, I did not have the MS Outlook Object library clicked. Secondly, I modified my code to the following:
Dim objOutlook As Object, objNameSpace As Object, objFolder As Object
Set objOutlook = CreateObject("Outlook.Application")
Set objNameSpace = objOutlook.GetNamespace("MAPI")
Set objFolder = objNameSpace.GetDefaultFolder(olFolderDrafts)
MsgBox objFolder.items.count
You could also do This
Option Explicit
Public Const olFolderInbox As Long = 6
Public Const olFolderDrafts As Long = 16
Public Const olFolderContacts As Long = 10
Public Const olFolderDeletedItems As Long = 3
Public Sub Count_MailItems()
Dim olApp As Object ' Outlook.Application
Dim olNs As Object ' Outlook.Namespace
Dim Folder As Object ' Outlook.MAPIFolder
Dim Items As Object ' Outlook.Items
Dim Msg As String
Set olApp = CreateObject("Outlook.Application")
Set olNs = olApp.GetNamespace("MAPI")
' Set Folder = olNs.Session.PickFolder
'or
' Set Folder = olApp.ActiveExplorer.CurrentFolder
'or
Set Folder = olNs.GetDefaultFolder(olFolderInbox)
' olFolderDrafts
' olFolderContacts
' olFolderDeletedItems
Set Items = Folder.Items
Msg = Items.Count & " Items in " & Folder.Name & " Folder"
MsgBox (Msg)
End Sub

VBA: Search email in non default outlook inbox?

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

Resources