I copied the code from Youtube and made the necessary changes to reflect what I need it to do.
It works fine in importing emails to Excel, however, it only works with the emails received in my Inbox.
I don't want to import all the emails I receive but instead, I only have to choose which ones to import. My work around was to move the emails that I want to import to another folder (Inbox subfolder called "OnBoarding").
This is where I am stuck. I couldn't make it work to import emails in the Inbox subfolder "OnBoarding"
Here is the code that I use:
Private Sub Application_Startup()
Dim outlookapp As Outlook.Application
Dim objectNS As Outlook.NameSpace
Set outlookapp = Outlook.Application
Set objectNS = outlookapp.GetNamespace("MAPI")
Set inboxItems = objectNS.GetDefaultFolder(olFolderInbox).Items
I tried to change the path to this:
Private Sub Application_Startup()
Dim outlookapp As Outlook.Application
Dim objectNS As Outlook.NameSpace
Set outlookapp = Outlook.Application
Set objectNS = outlookapp.GetNamespace("MAPI")
Set inboxItems = objectNS.GetDefaultFolder(olFolderInbox).Folders("OnBoarding")
But it is not working.
Please help on what I am doing wrong.
Thank you!
You are trying to set an instance of the Items class to a Folders one. Instead, you need to get a folder instance and then retrieve a corresponding Items object:
Dim outlookapp As Outlook.Application
Dim objectNS As Outlook.NameSpace
Dim subfolder As Outlook.Folders
Dim onBoardingFolderItems as Outlook.Items
Set objectNS = Application.GetNamespace("MAPI")
Set subfolder = objectNS.GetDefaultFolder(olFolderInbox).Folders("OnBoarding")
Set onBoardingFolderItems = subfolder.Items
Related
On my laptop it works, however for the team the code works only sometimes. They get below error message.
It is supposed to connect to a specific Outlook folder.
When I reduce code to
Set Folder = Ns.GetSharedDefaultFolder(olShareName, olFolderInbox) '// works fine
then there is no issue on their side, however when variable is extended to specific folder it works 1 out of 10 times.
References are set in Excel, folders names are correct because it works on my laptop.
in line
`
Sub macro()
'declare variable
Dim olApp As Outlook.Application
Dim Ns As Outlook.Namespace
Dim Folder As Outlook.MAPIFolder
Dim olShareName As Outlook.Recipient
Dim olMailItem As Outlook.MailItem
'clear objects
Set olApp = Nothing
Set Ns = Nothing
Set olShareName = Nothing
'set outlook variable
Set olApp = New Outlook.Application
Set Ns = olApp.GetNamespace("MAPI")
Set olShareName = Ns.CreateRecipient("xxx#xx.com") /// Owner's email address
Set Folder = Ns.GetSharedDefaultFolder(olShareName, olFolderInbox).Folders("SHAREPOINT COO").Folders("COO") '// doesn't work
'Set Folder = Ns.GetSharedDefaultFolder(olShareName, olFolderInbox) '// works fine
Set Items = Folder.Items
end sub
The error means the folder does not exist. Keep in mind that Namespace.Folders returns the top level folders of all stores in the profile. Unless you have a mailbox (not a folder) named "Inbox", that line will fail.
If you want the Inbox folder, use Namespace.GetDEfaultFolder(olFolderInbox) instead.
Use the Recipient.Resolve method which attempts to resolve a Recipient object against the Address Book before calling the NameSpace.GetSharedDefaultFolder method.
Set olShareName = Ns.CreateRecipient("xxx#xx.com") /// Owner's email address
olShareName.Resolve
Set Folder = Ns.GetSharedDefaultFolder(olShareName, olFolderInbox)
Also I'd recommend breaking the chain of property or method calls by declaring each property or method call on a separate line of code. Thus, you will be able to find out which property or method fails.
I have VBA code in Excel to select the main Outlook inbox. I would like to select any folder or subfolder in that inbox.
For example, I would like to select the subfolder ALD in this screenshot of my main inbox:
I have another email address in Outlook with folders and subfolders. I would like to select any folder or subfolder of this other email address. For example, I have another email address called xxxx#yyyy.com and a folder aaaa and inside a subfolder bbbb. How would I select the subfolder bbbb?
Sub OpenOutlookFolder()
Dim xOutlookApp As Outlook.Application
Dim xNameSpace As Outlook.Namespace
Dim xFolder As Outlook.Folder
Dim xFolderType As OlDefaultFolders
On Error Resume Next
Set xOutlookApp = New Outlook.Application
Set xNameSpace = xOutlookApp.Session
Set xFolder = xNameSpace.GetDefaultFolder(olFolderInbox
xFolder.Display
Set xFolder = Nothing
Set xNameSpace = Nothing
Set xOutlookApp = Nothing
Exit Sub
End Sub
Something along the lines of:
Dim ThisNamespace As Outlook.NameSpace: Set ThisNamespace = Application.GetNamespace("MAPI")
Dim Inbox As Outlook.MAPIFolder: Set Inbox = ThisNamespace.GetDefaultFolder(olFolderInbox)
Dim BaseFolder As Outlook.MAPIFolder: Set BaseFolder = Inbox '.Folders("SubFolder1\SubFolder2...")
For direct subfolder access, uncomment within the last line and update the path
If you want to create a folder structure which is searchable/editable then my answer in this question may be of interest: How can one iterate through the subfolders of a subfolder of a shared mail inbox folder?
Finally by checking again the link Get reference to additional Inbox, I managed to modify my macro in order it works and does as I want. Please find the code below:
Sub OpenOutlookFolderworks()
Dim xOutlookApp As Outlook.Application
Dim xNameSpace As Outlook.Namespace
Dim xFolder As Outlook.Folder
Dim vRecipient As Outlook.MAPIFolder
Dim xFolderType As OlDefaultFolders
On Error Resume Next
Set xOutlookApp = New Outlook.Application
Set xNameSpace = xOutlookApp.Session
Set xFolder = xNameSpace.GetDefaultFolder(olFolderInbox)
Set xFolder = xFolder.Folders("payment office")
Set xFolder = xFolder.Folders("JIRA")
xFolder.display
Set xFolder = Nothing
Set xNameSpace = Nothing
Exit Sub
End Sub
My company is transferring from Outlook 16 to Outlook 365, and the Excel VBA script below now needs to refer to a mailbox in Outlook 365.
It errors on the line
Set oMapi = oApp.GetNamespace("MAPI").Folders(strMail).Folders("inbox")
with the error
The attempted operation failed. An object could not be found.
Is it possible the mailbox needs to be added in a different way? Or is there a different way to do this function in Outlook 365?
I am not seeing much on a different way to do this with Outlook 365.
Sub Import_Email_Preferences()
Const strMail As String = "borrowerservicesshiftbid#glhec.org"
Dim oApp As Outlook.Application
Dim oMapi As Outlook.MAPIFolder
Dim oMail As Outlook.MailItem
Dim strEmailAddress As String
Dim strSenderName As String
Dim strSubject As String
Dim intRow As Integer
Dim i As Long
Dim tbl As ListObject
Dim ltblRow As Long
Set tbl = ThisWorkbook.Worksheets("Preferences").ListObjects(1)
ltblRow = tbl.DataBodyRange.Rows.Count
On Error Resume Next
Set oApp = GetObject(, "OUTLOOK.APPLICATION")
If (oApp Is Nothing) Then Set oApp = CreateObject("OUTLOOK.APPLICATION")
On Error GoTo 0
'Getting Error Here
Set oMapi = oApp.GetNamespace("MAPI").Folders(strMail).Folders("inbox")
For i = oMapi.Items.Count To 1 Step -1
Set oMail = oMapi.Items(i)
If TypeOf oMail Is Outlook.MailItem Then
MsgBox = "Blue"
End If
Next i
Set oApp = Nothing
Set oMapi = Nothing
Set oMail = Nothing
Set oHTML = Nothing
Set oElColl = Nothing
End Sub
Please try to reference the inbox by GetDefaultFolder:
On Error Resume Next
If oApp Is Nothing Then
Set oApp = GetObject(, "Outlook.Application")
If oApp Is Nothing Then
Set oApp = New Outlook.Application
End If
End If
On Error GoTo 0
Set oMapi = oApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
If the default folder ist not accessible, be aware that folder names are language dependent, e. g. Namespace.Folders("Posteingang") in German.
The trouble with Set oMapi = oApp.GetNamespace("MAPI").Folders(strMail).Folders("inbox") is you are stringing references together. This is fine if it works but you do not know what has failed if it does not.
Set oMapi = oApp.GetNamespace("MAPI") will fail if oApp is not an object.
Set oMapi = oApp.GetNamespace("MAPI").Folders(strMail) will fail if oApp.GetNamespace("MAPI") is not an object.
Set oMapi = oApp.GetNamespace("MAPI").Folders(strMail).Folders("inbox") will fail if oApp.GetNamespace("MAPI").Folders(strMail) is not an object.
I suggest trying the subroutine below to identify which step is failing. This subroutine does everything in single steps. It also tries different ways of achieving the same effect. If one Set fails, comment it out and see if the next one works.
I have suggested some possible causes of failures but I am sure there are other possibilities. Once you know the step that fails, come back if you need further help.
You may wish to try Asger suggestion of GetDefaultFolder as well. It does not work on my system because the default Inbox in not used. I have to name the store containing the Inbox I wish to access.
Sub TestGetInbox()
Dim olApp As Outlook.Application
Dim olInbox1 As Outlook.MAPIFolder
Dim olInbox2 As Outlook.Folder
Dim olInbox3 As Outlook.MAPIFolder
Dim olInbox4 As Outlook.Folder
Dim olNs As Outlook.Namespace
Dim olSession As Outlook.Namespace
Dim olStore1 As Outlook.Folder
Dim olStore2 As Outlook.Folder
' If execution stops her, you have a problem accessing Outlook
Set olApp = CreateObject("Outlook.Application")
' If execution stops here, you have a problem accessing Outlook's namespace
' using this method. Comment out statements down to "Set olSession = olApp.Session"
Set olNs = olApp.GetNamespace("MAPI")
' If execution stops here, look at your folder pane. Is "borrowerservicesshiftbid#glhec.org"
' the name of a store? Case does not seem to matter but a single letter change in the name
' does.
Set olStore1 = olNs.Folders("borrowerservicesshiftbid#glhec.org")
Debug.Print olStore1.Name
Set olInbox1 = olStore1.Folders("Inbox")
Debug.Print olInbox1.Name
Set olInbox2 = olStore1.Folders("Inbox")
Debug.Print olInbox1.Name
' If execution stops her, you have a problem accessing Outlook's namespace
' This is a different methods of accessing Outlook's namespace. The documentation
' says the two method are identical but I once had the olApp.Namespace("MAPI")
' fail so I now always use the olApp.Session method which has never failed for me.
Set olSession = olApp.Session
Set olStore2 = olSession.Folders("borrowerservicesshiftbid#glhec.org")
Debug.Print olStore2.Name
Set olInbox3 = olStore1.Folders("Inbox")
Debug.Print olInbox1.Name
Set olInbox4 = olStore1.Folders("Inbox")
Debug.Print olInbox1.Name
Set olInbox1 = Nothing
Set olInbox2 = Nothing
Set olStore1 = Nothing
Set olNs = Nothing
Set olInbox3 = Nothing
Set olInbox4 = Nothing
Set olStore2 = Nothing
Set olSession = Nothing
Set olApp = Nothing
End Sub
How do I move an Outlook subfolder (and all items in it) to a different parent folder?
Example:
Subfolder to move: Example Event 2017
EXISTING:
Outlook\Personal Folders\Audits\Example Event 2017
AFTER CODE RUNS:
Outlook\Personal Folders\Audits\Past Events\Example Event 2017
Thank you!
In Excel, this will move the subfolder "test" from its parent folder "new" to make it a subfolder of "processed". (needs reference to Outlook)
Sub moveSubFolderToNewFolder()
Dim objOutlook As Outlook.Application
Dim objNamespace As Outlook.Namespace
Dim objSourceFolder As Outlook.MAPIFolder
Dim objDestFolder As Outlook.MAPIFolder
Dim objFolder As Folder
Set objOutlook = Outlook.Application
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objSourceFolder = objNamespace.GetDefaultFolder(olFolderInbox)
Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox).Folders("new").Folders("test")
Set objDestFolder = objNamespace.GetDefaultFolder(olFolderInbox).Folders("processed")
objFolder.MoveTo objDestFolder
Set objDestFolder = Nothing
End Sub
I am using MS Office 2010.
My Outlook is configured with two email IDs. One is client another one is company.
I have a code in Excel to check an Outlook distribution list and import member details into Excel.
I am able to import the details from a distribution list in the GAL for a default mail ID.
I am unable to select the distribution list in the GAL for other email.
It searches for the DL in the default account's GAL and selects the wrong name.
Dim olApp As Outlook.Application
Dim olNS As Outlook.Namespace
Dim olAL As Outlook.AddressList
Dim olEntry As Outlook.AddressEntry
Dim olMember As Outlook.AddressEntry
Dim lMemberCount As Long
Dim objMail As Outlook.MailItem
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olAL = olNS.AddressLists("Global Address List")
Set objMail = olApp.CreateItem(olMailItem)
' enter the list name
Set olEntry = olAL.AddressEntries(".DL IS Support")
' get count of dist list members
lMemberCount = olEntry.Members.Count