I am having issues with the Microsoft Outlook Object Library, when working with two different Office versions (2007 and 2016).
I already found out that late binding is a solution here, but I don't know how to late bind other objects than Outlook.Application. Especially "Outlook.Folder" and "Outlook.Selection" is what I need for late binding.
I tried creating objects just like I did for my Outlook.Application called "appoutlook" but the code below keeps giving me the error message that the object creation via ActiveX components is not possible...
1st class:
Public Type Email_Attributes
olApp As Object
olFolder As Object
olSelection As Object
End Type
2nd class:
Dim olMail As Object
Dim appoutlook As Object
Set appoutlook = CreateObject("Outlook.Application")
Const olMailItem As Long = 0
Set olMail = appoutlook.CreateItem(olMailItem)
Dim strAttName As String
Dim new_mail As Email_Attributes
Application.ScreenUpdating = False
Set new_mail.olApp = CreateObject("Outlook.Application")
Set new_mail.olFolder = CreateObject("Outlook.Folder")
Set new_mail.olSelection = CreateObject("Outlook.Selection")
Set new_mail.olFolder = GetObject("ActiveExplorer.CurrentFolder")
Set new_mail.olSelection = GetObject("ActiveExplorer.Selection")
Any help is much appreciated!
Related
The first sub routine runs fine when called upon application startup, while the second does not bring up Outlook and instead takes a minute or two, culminating in an error. The only difference is that in the "set" statement, I put the word "New". I am aware that this means it is trying to open up a new instance of the Outlook client, while one is already running, but why wouldn't there be a way for the Outlook to deal with the request for a new application while one is already running, or use the new instance instead of the existing instance?
Sub SaveAttachment1_Initialize()
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olItems = olNS.GetDefaultFolder(olFolderInbox)
End Sub
Sub SaveAttachment1_Initialize()
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Set olApp = New Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olItems = olNS.GetDefaultFolder(olFolderInbox)
End Sub
If you are running in Outlook VBA, that line must be
Set olApp = Application
If not, the second one is correct. The first one makes no sense - you are assigning a variable to type (?).
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.
I'm developing a drag and drop of a MailItem from Outlook (I know it's a MailItem and not any other type) into an Access memo field. Trying to call SaveAs on a MailItem object.
I get
Error 287 - Application-defined or object-defined error.
I've tried using the namespace, not using the namespace, using .Item, etc.
Here's my current code:
Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")
Dim olNs As Outlook.NameSpace
Set olNs = olApp.GetNamespace("MAPI")
Dim olMail As Outlook.MailItem
Set olMail = olNs.GetItemFromID(olNs.Application.ActiveExplorer.Selection(1).EntryID)
olMail.SaveAs strPathAndFile, Outlook.OlSaveAsType.olMSG
Access 2010, Outlook 2010 both 32 bit. Win 7 machine is 64 bit.
Tried it on an all 32-bit machine, same error.
Tried Dmitry's code below, same error.
Finally got this working using Dmitry Streblechenko's awesome Redemption library. Thanks!
Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")
Dim olNs As Outlook.NameSpace
Set olNs = olApp.GetNamespace("MAPI")
Dim oRDOSession As Redemption.RDOSession
Set oRDOSession = CreateObject("Redemption.RDOSession")
oRDOSession.MAPIOBJECT = olNs.Application.Session.MAPIOBJECT
If Not oRDOSession.LoggedOn Then oRDOSession.Logon
Dim oMsgItem As Redemption.RDOMail
Set oMsgItem = oRDOSession.GetMessageFromID(olNs.Application.ActiveExplorer.Selection(1).EntryID)
strPathAndFile = "some\path\" & UniqueValueStr(Now()) & ".msg"
oMsgItem.SaveAs strPathAndFile
Why do you need to use Redemption?
I don't see any difference between using the OOM and Redempton.
Why do you need to use the GetItemFromID method in the code?
olNs.GetItemFromID(olNs.Application.ActiveExplorer.Selection(1).EntryID)
You have already got a reference to the selected object in the explorer window:
olNs.Application.ActiveExplorer.Selection(1)
Also I'd suggest breaking the chaing of calls and declaring each property or method call on a separate line.
I want to change the time zone of all items in an Outlook 2010 calendar.
I am confused as to how one would work with the items of a collection as they are iterated in the loop. My main background is in Java, and as I understand loops there a single variable is used as a dummy variable that will take the value of all items in the collection, in turn. No special assignment is usually required for such FOR loops. Do you need to manually advance the variable in some way so as to keep the loop going?
Here is my code:
Public Sub TZFix()
Dim oAppointmentItem As Outlook.AppointmentItem
Dim tzs As Outlook.TimeZones
Dim tzCentral As Outlook.TimeZone
Dim oAppointments As Object
Dim oNS As Outlook.NameSpace
Set oNS = oOutlook.GetNamespace("MAPI")
Set oAppointments = oNS.GetDefaultFolder(olFolderCalendar)
Set tzs = Application.TimeZones
Set tzCentral = tzs("Central Standard Time")
For Each oAppointmentItem In oAppointments.Items
Set oAppointmentItem.StartTimeZone = tzCentral
Set oAppointmentItem.EndTimeZone = tzCentral
Next
End Sub
I believe that there is an issue with variable assignment within the loop, as I get an Error 91: Object Variable or With block variable not set error whenever I run it.
oOutlook is never assigned to and is therefore Nothing. You probably meant to set it to Application.
Also, setting local variables to Nothing in the end is redundant, remove that.
I also had this problem in my Script. For me the solution was setting the Macro-Security-Settings to the lowest and ran it again and it worked.
Maybe it's worth giving it a try!
I made the code working with few changes of your code. This is worked:
Public Sub TZ_change_to_Hawaii()
''''Changing the selected appointments' time zones to Hawaii
Dim tzs As Outlook.TimeZones
Dim tzCentral As Outlook.TimeZone
Set tzs = Application.TimeZones
Set tzCentral = tzs("Hawaiian Standard Time")
Dim objOL As Outlook.Application
Dim objSelection As Outlook.Selection
Dim objItem As Object
Set objOL = Outlook.Application
Set objSelection = objOL.ActiveExplorer.Selection
For Each objItem In objSelection
Set objItem.StartTimeZone = tzCentral
Set objItem.EndTimeZone = tzCentral
objItem.Save
Next
End Sub
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.