How to save an email's attachment to a specific folder? - vba

I searched the web and wrote the below code.
Getting
Run-time error '-2147221233 (8004010f)': Automation Error
VBA Code:
Sub SaveWklyReports()
Dim ol As Outlook.Application
Dim ns As Outlook.Namespace
Dim fol As Outlook.Folder
Dim p As Object
Dim mi As Outlook.MailItem
Set ol = New Outlook.Application
Set ns = ol.GetNamespace("MAPI")
Set fol = ns.Folders(1).Folders("Test")
For Each p In fol.Items
If p.Class = olMail Then
Set mi = p
Debug.Print mi.SenderName, mi.ReceivedTime
End If
Next p
End Sub

That error means there is no such folder as ns.Folders(1).Folders("Test")
Instead of using the explicit index 1 use the correct folder name e.g.
Set fol = ns.Folders(myFolderName).Folders("Test")
In my case myFolderName is set to my email address for my main account or to the name of the shared mail account for others - for you it will depend on what you are trying to access

Related

Import Outlook contacts from a secondary email using Access VBA

I'm trying to open the Outlook contacts folder from a secondary email account.
This code will display the contacts on my default email account.
I tried .Session.Accounts. Is there a way I can open a folder from the path? If so, how would I get the path for contacts folder?
Private Sub Command12_Click()
Dim xOutlookApp As Outlook.Application
Dim xNameSpace As Outlook.NameSpace
Dim xFolder As Outlook.Folder
'
'On Error Resume Next
Set xOutlookApp = New Outlook.Application
Set xNameSpace = xOutlookApp.Session
Set xFolder = xNameSpace.GetDefaultFolder(10)
xFolder.Display
'
Set xFolder = Nothing
Set xNameSpace = Nothing
Set xOutlookApp = Nothing
Exit Sub
Instead of calling Namespace.GetDefaultFolder, call Store.GetDefaultFolder. A Store object can be retrieved from the Namespace.Stores collection.

Outlook VBA 2013 Access Parent Folders

Okay, I was reading a tutorial on how to access parent folders outside of the Inbox and noticed that it was using "Set". To my knowledge, this command is deprecated and seems so whenever I have tried to use it in my code.
http://blogs.technet.com/b/heyscriptingguy/archive/2006/08/03/how-can-i-get-access-to-a-mail-folder-that-isn-t-a-subfolder-of-my-outlook-inbox.aspx
' Set Outlook parameters
objOutlook = CreateObject("Outlook.Application")
iNameSpace = myOlApp.GetNamespace("MAPI") ' Set current NameSpace
Dim oExp As Outlook.Explorer
Dim oSel As Outlook.Selection
oExp = objOutlook.ActiveExplorer
oSel = oExp.Selection
Dim strFolderName As Object
Dim objInbox As Outlook.Folder
Dim objMailbox As Outlook.Folder
Dim objFolder As Outlook.Folder
Const olFolderInbox = 6
objInbox = iNameSpace.GetDefaultFolder(olFolderInbox)
strFolderName = objInbox.Folders.Parent()
objMailbox = iNameSpace.Folders(strFolderName)
objFolder = objMailbox.Folders("Europe")
When trying the code above, I get a type error: Additional information: Type mismatch, on this line:
objMailbox = iNameSpace.Folders(strFolderName)
When I change this to an "Object", I get the same error.
Any Idea what I am doing wrong?
To access the parent of the Inbox folder, try iNameSpace.GetDefaultFolder(olFolderInbox).Parent
To access a folder on the same level as the Inbox, try iNameSpace.GetDefaultFolder(olFolderInbox).Parent.Folders.Item("The Folder name")
The following code will not work.
strFolderName = objInbox.Folders.Parent()
The Folders collection does't provide the Parent method.
objMailbox = iNameSpace.Folders(strFolderName)
The Folders property doesn't accept an object arguments.

Unable to iterate over other person's appointments with VBA

I am trying to iterate over someone's outlook appointments with VBA. I believe the following fragment should do what I need, however, the line for each appm in cal.items pops up a little message box that says
Laufzeitfehler '-140492795 (f7a04405)':
Automatisierungsfehler
In english, that would probably be run time error .... automation error.
Why do I get this error?
option explicit
sub abcdef()
dim ol as outlook.application
dim ns as outlook.namespace
dim rcpt as outlook.recipient
dim cal as outlook.folder
dim appm as outlook.appointmentItem
set ol = new outlook.application
set ns = ol.GetNamespace("MAPI")
set rcpt = ns.createRecipient("Deere John")
rcpt.resolve
if not rcpt.resolved then
msgBox("Could not resolve recipient")
return
end if
set cal = ns.getSharedDefaultFolder(rcpt, olFolderCalendar)
if cal is nothing then
msgBox ("No Calender!")
return
end if
for each appm in cal.items
' Error occurs in previous line
next appm
end sub
Found some code here: http://www.slipstick.com/developer/copy-new-appointments-to-another-calendar-using-vba/ which would change how you dim appm... Instead of outlook.appointmentItem it has it simply as appointmentItem. So Dim appm as appoointmentItem
Edit: Their syntax also uses Set newCalFolder = GetFolderPath("display name in folder list\Calendar\Test") to define the cal variable, while you are using getSharedDefaultFolder
Even if you can manually add "Deere John" to your list of calendars, the calendar permission is probably not correct for VBA.
Try this to see if Outlook displays "Cannot display the folder. Microsoft Outlook cannot access the specified folder location."
Sub abcdef_CalDisplay()
Dim ol As Outlook.Application
Dim ns As Outlook.Namespace
Dim rcpt As Outlook.Recipient
Dim cal As Outlook.folder
Dim appm As Outlook.AppointmentItem
Set ol = New Outlook.Application
Set ns = ol.GetNamespace("MAPI")
Set rcpt = ns.CreateRecipient("Deere John")
rcpt.Resolve
If Not rcpt.Resolved Then
MsgBox ("Could not resolve recipient")
Return
Else ' <----
Set cal = ns.GetSharedDefaultFolder(rcpt, olFolderCalendar)
cal.Display ' <---
End If
End Sub

How to view Outlook calendar appointments using VB.NET

I'm writing an application that displays a calendar; when you click a date on the calendar, it should display a window listing the appointments for that day.
How can this be done in VB.Net? I can find code to add appointments to a calendar, but not to do this.
Here is a snippet of code using the Outlook Interop API.
This piece of code reaches out to the application and fetches all calendar items. If you wish to restrict based on a date, you can call the clfFolder.Items.Restrict method and pass in a restriction filter. Please note, I call the clear recurrence items in this method to formalize entries as opposed to listing a single item with recurrence.
Dim appOutlook As Outlook.Application = Me.OutlookFormRegion.Application
Dim mpnNamespace As Outlook.NameSpace = appOutlook.GetNamespace("MAPI")
Dim clfFolder As Outlook.Folder = _
mpnNamespace.GetDefaultFolder(OlDefaultFolders.olFolderCalendar)
Dim itmItems As Outlook.Items
Dim oaiAppointmentItem As Outlook.AppointmentItem
clfFolder.Items.IncludeRecurrences = False
For Each oaiAppointmentItem In clfFolder.Items
oaiAppointmentItem.ClearRecurrencePattern()
Next
I used this to find the data. not sure if will help
Try
Dim olApp As Outlook.Application
olApp = CreateObject("Outlook.Application")
Dim mpnNamespace As Outlook.NameSpace = olApp.GetNamespace("MAPI")
Dim oCalendar As Outlook.MAPIFolder = mpnNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar)
Dim oItems As Outlook.Items = oCalendar.Items
oItems.Sort("Start")
oItems.IncludeRecurrences = True
Dim oAppt As Outlook.AppointmentItem = oItems.Find("[Subject] = 'Upgrade/Issue reported via eData - Ref:2'")
oAppt.Subject = "Updated"
oAppt.Save()
olApp = Nothing
Catch ex As Exception
MsgBox(ex.ToString)
End Try

VBA, Outlook, Seeing 'People's Calendars

I am tring to programmatically (with VBA) to access calendars others share with me. They are listed in my Outlook under 'People's Calendars.' I have searched the Web for this and all the suggestions have done little more than confuse me. How can I get a listing of all the calendars shared to me, and then one calendar in specific, from among the 'People's Calendars'?
Check out the returned values from the following code. It searches for a person by name, same way as when you are typing a recipient into a new email, and then grabs that persons shared calendar and enumerates all shared appointments.
Dim _namespace As Outlook.NameSpace
Dim _recipient As Outlook.Recipient
Dim calendarFolder As Outlook.Folder
Set _namespace = Application.GetNamespace("MAPI")
Set _recipient = _namespace.CreateRecipient(name)
_recipient.Resolve
If _recipient.Resolved Then
Set calendarFolder = _namespace.GetSharedDefaultFolder(_recipient, olFolderCalendar)
'This would display the calendar on the screen:
'calendarFolder.Display
Dim oItems As Outlook.Items
Set oItems = calendarFolder.Items
'oItems is now a set of all appointments in that person's calendar
'Play on
End if
I think this gets closer. It came from Sue Mosher's outstanding Microsoft Outlook 2007 Programming: Jumpstart for Power Users and Administrators. I hope she doesn't mind.
Sub ShowOtherUserCalFolders()
Dim objOL As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim objExpCal As Outlook.Explorer
Dim objNavMod As Outlook.CalendarModule
Dim objNavGroup As Outlook.NavigationGroup
Dim objNavFolder As Outlook.NavigationFolder
Dim objFolder As Outlook.Folder
Dim colExpl As Outlook.Explorers
Dim objExpl As Outlook.Explorer
Set objOL = Application
Set objNS = objOL.Session
Set colExpl = objOL.Explorers
Set objExpCal = _
objNS.GetDefaultFolder(olFolderCalendar).GetExplorer
Set objNavMod = objExpCal.NavigationPane.Modules. _
GetNavigationModule(olModuleCalendar)
Set objNavGroup = objNavMod.NavigationGroups. _
GetDefaultNavigationGroup(olPeopleFoldersGroup)
For Each objNavFolder In objNavGroup.NavigationFolders
Set objFolder = objNavFolder.Folder
Set objExpl = _
colExpl.Add(objFolder, olFolderDisplayNormal)
objExpl.Activate
objExpl.WindowState = olMaximized
objExpl.WindowState = olMinimized
Next
Set objOL = Nothing
Set objNS = Nothing
Set objNavMod = Nothing
Set objNavGroup = Nothing
Set objNavFolder = Nothing
Set objFolder = Nothing
Set colExpl = Nothing
Set objExpl = Nothing
End Sub
Just a suggestion to help people who may be trying to use the ShowOtherUserCalFolders() code posted here. This code will create multiple hidden instances of outlook which if run many times can eventual bog down your machine. Instead of creating a new Outlook.application you can call the current open one (outlook must be open for this to work).
To do this replace Dim objOL As Outlook.Application with Dim objOL as Object and Set objOL = Application with Set myOlApp = GetObject(, "Outlook.Application")
Also make sure you close the objExpCal Explorer as this will also create a hidden instance of outlook, add objExpCal.Close to the end of your code.