Run-Time Error 462 - Delete Outlook Appointments from Excel - excel

The code below deletes appointments in a subfolder of the Outlook default calendar. I have commented out the line giving the run-time error 462: "The remote server machine does not exist or is unavailable".
Is there a change I could make to this code to solve this error? Thanks for any guidance.
Public Sub DeleteAppt()
Dim olApp As Object 'Outlook.Application
Dim olNS As Object 'Outlook.Namespace
Dim olAptItemFolder As Object 'Outlook.Folder
Dim olAptItem As Object 'Outlook.AppointmentItem
Dim i As Long
Set olApp = CreateObject("Outlook.Application")
Set olNS = olApp.Session
Set olAptItemFolder = olNS.GetDefaultFolder(olFolderCalendar).Folders("TestCal")
''''For i = olAptItemFolder.Count To 1 Step -1
Set olAptItem = olAptItemFolder.Items(i)
If olAptItem.Subject Like "***" Then
olAptItem.Delete
End If
Next i
Set olAptItem = Nothing
Set olAptItemFolder = Nothing
Set olApp = Nothing
End Sub

olAptItemFolder has no Count property. olAptItemFolder.Items does. Apart from the issues others noted in comments above, try
For i = olAptItemFolder.Items.Count To 1 Step -1
Edited to add: if you are not setting a reference to something's object lirary, you can't use its enums unless you fully qualify each use. It's generally simpler, easier and quicker to find out the numerical value of the enum and use that instead. Then add a comment on the end of the line to remind yoursef, six months from now, that '9 = olFolderCalendar

Related

Error on GetSelectNamesDialog in Excel VBA

So i have been struggling with this issue for about a week now. The below code is the opening portion of a project I'm working on.
The goal here is to open up the address-book pop-up for the Global Address List in outlook. I gathered most of this code from another question or two here on stacked overflow. The issue is, when I get to the final line
Set Dialog = outlookApp.Session.GetSelectNamesDialog
I get a "Run-time error '287': Application-defined or object-defined error"
Yes, Outlook 16.0 reference library is checked off.
I was curious about object security blocking.
I recently updated to GetGlobalAddressList to fix a similair issue.
Sub AddTestConductor() 'inserts information for a new TC from GAL
Dim OutlookApp As Outlook.Application
Dim OutlookNameSpace As Outlook.Namespace
Dim Dialog As Outlook.SelectNamesDialog
Dim GAL As Outlook.AddressList
Dim TCEntry As Outlook.AddressEntry
Dim User As Outlook.ExchangeUser
Dim Alias As String
Dim FirstName As String
Dim LastName As String
Dim Email As String
Dim where As String
Set OutlookApp = GetObject(, "Outlook.Application") 'grabs outlook for use
Set OutlookNameSpace = OutlookApp.GetNamespace("MAPI")
OutlookNameSpace.Logon , , True, False
Set GAL = OutlookApp.Session.GetGlobalAddressList
Set Dialog = OutlookApp.Session.GetSelectNamesDialog
With Dialog
.AllowMultipleSelection = False
.InitialAddressList = GAL
.ShowOnlyInitialAddressList = True
If .Display Then
Alias = Dialog.Recipients.Item(1).Name
Set TCEntry = GAL.AddressEntries(Alias)
Set User = TCEntry.GetExchangeUser
If Not User Is Nothing Then 'populate variable for name and e-mail0
FirstName = User.FirstName
LastName = User.LastName
Email = User.PrimarySmtAddress
End If
End If
End With
Set OutlookApp = Nothing 'Empty objects for next use
Set Dialog = Nothing
Set GAL = Nothing
Set TCEntry = Nothing
Set User = Nothing
In addition to the above, I would like to specify a sub folder withing th GAL, to wean the list down.
Info grabbed from the GAL with be inserted into a spreadsheet, then I will need to create links to calendar dates from their information as well. I haven't gotten that far yet.
I'm currently just trying to gather info about a user as baby step number one.

Set reference to Outlook 365 folder in Excel VBA

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

Updating shared calendar

I am trying to update a shared calendar from an Excel sheet. The code works for the owner of this shared calendar, but it fails for me. The calendar was shared to me and I have full owner permissions.
I can edit the calendar manually, but the idea is that anyone will be able run the macro from this Excel sheet to update the shared calendar.
The relevant code, up to the failure point:
Sub UpdateSched()
Dim olApp As Outlook.Application
Dim olNameSpace As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder
Dim olFldrOwner As Outlook.Recipient
On Error Resume Next
' check if Outlook is running
Set olApp = GetObject("Outlook.Application")
If Err <> 0 Then
'if not running, start it
Set olApp = CreateObject("Outlook.Application")
End If
On Error GoTo 0
Set olNameSpace = olApp.GetNamespace("MAPI")
Set olFldrOwner = olNameSpace.CreateRecipient("ownrAlias")
olFldrOwner.Resolve
Set olFolder = Nothing
If olFldrOwner.Resolved Then
Set olFolder = olNameSpace.GetSharedDefaultFolder(olFldrOwner, olFolderCalendar)
' If olFolder Is Nothing Then
' Debug.Print "Nothing"
' Else
' Debug.Print olFolder.Name '<-Error here if the if-block is run
' End If
'******************************
Set olFolder = olFolder.Folders("Transport Sched") '<-Object Not Found Error
'******************************
End If
'Code below updates appointments on the shared calendar
The full error
'The attempted operation failed. An object could not be found'
For testing, I added the commented out block. This made me think that the error might be in the previous line. When this block is un-commented then the code errors out on the line after Else (same error). So the olFolder object is not nothing, but it can't be found.
It's under "Shared Calendars" I get the error. It updates a calendar I created, that's under "My Calendars."
Is it a problem with finding the correct folder for the shared calendar?
The path to the folder shouldn't change, so I could hard-code it so it works for everyone, is that possible?
I came up with a solution to my problem but in a completely different way than I was trying with the code listed in the question. In case anyone else needs it, here is the solution:
Sub ListCalendars()
Dim olApp As Outlook.Application
Dim olPane As Outlook.NavigationPane
Dim olModule As Outlook.CalendarModule
Dim olGroup As Outlook.NavigationGroup
Dim olNavFolder As Outlook.NavigationFolder
Dim olFolder As Folder
Dim i As Integer, j As Integer
On Error Resume Next
' check if Outlook is running
Set olApp = GetObject("Outlook.Application")
If Err <> 0 Then
'if not running, start it
Set olApp = CreateObject("Outlook.Application")
End If
On Error GoTo 0
Set olPane = olApp.ActiveExplorer.NavigationPane
Set olModule = olPane.Modules.GetNavigationModule(olModuleCalendar)
Set olGroup = olModule.NavigationGroups.GetDefaultNavigationGroup(olMyFoldersGroup)
'Dummy Do loop allows exit from within nested For-Next loops
Do
For i = 1 To olModule.NavigationGroups.Count 'Cycle through all Nav Groups
Set olGroup = olModule.NavigationGroups.Item(i)
Debug.Print olGroup.Name
For j = 1 To olGroup.NavigationFolders.Count 'Cycle through all calendars in group
Set olNavFolder = olGroup.NavigationFolders.Item(j)
Debug.Print " - " & olNavFolder.DisplayName
'Un-comment If-block below if searching for a particular calendar:
'CalendarName is the name of the calendar,as listed in your navigation pane
' If olNavFolder.DisplayName = "CalendarName" Then
' Debug.Print "Found it!"
' Set olFolder = olNavFolder.Folder 'To get folder object from NavigationFolder
' Exit Do
' End If
Next
Next
Exit Do 'To prevent endless loop
Loop While True
'If-block below displays results if looking for matching calendar name
'If olFolder Is Nothing Then
' Debug.Print vbNewLine & "No match found"
'Else
' Debug.Print vbNewLine & "Matching calendar found: " & olFolder.Name
'End If
End Sub
This code was modified from this page here. Basically, accessing someone else's calendar folder object directly gave me issues, even if the calendar had been shared. By using the various navigation objects though, I was able to cycle through all the calendars listed in my navigation pane, including all the shared calendars.
As provided this routine merely lists the main folders and one level of subfolders. If the two if-blocks are uncommented then the routine will search for the calendar with a given name(just replace CalendarName) and will display whether or not a match was found.
The dummy Do loop was one way to break out of nested loops. There's multiple other ways to accomplish this listed in this SO question.
One other tricky thing is that NavigationFolder objects are NOT the same as Folder objects. This seemingly inconsequential line:
Set olFolder = olNavFolder.Folder
is actually very important if you want to make changes to the calendar folder, since these two object types have different properties and methods.

Excel VBA, return GAL value

i have a very simple small app in mind which will help me save time from alt tabbing from excel to outlook. i want to create a small userform that will have a textbox for a exchange user alias and return the exchange user's full name. now the issue i have here is that the guide in msdn is a little vague for a userform: https://msdn.microsoft.com/en-us/library/office/ff869721.aspx and i'm getting some error messages, some got fixed by activating some references. and the code is quite complicated.
so basically i have 2 textboxes and a button. textbox1 will accept the alias, textbox2 will return the username after clicking the button.
there are several examples but most of them will result in dumping the GAL to an excel file which i don't need.
thanks in advanced
This will give you what you want.
Private Function GetFullName(inAlias As String) As String
Dim olApp As Outlook.Application
Dim olNS As Outlook.Namespace
Dim olAdd As Outlook.AddressEntries
Dim olMem As Outlook.AddressEntry
Dim olLst As Outlook.AddressList
Dim olAlias As String
On Error Resume Next
Set olApp = New Outlook.Application
On Error GoTo 0
If olApp Is Nothing Then
GetFullName = "Source not available"
Exit Function
End If
Set olNS = olApp.GetNamespace("MAPI")
Set olLst = olNS.GetGlobalAddressList
Set olAdd = olLst.AddressEntries
For Each olMem In olAdd
On Error Resume Next
olAlias = olMem.GetExchangeUser.Alias
On Error GoTo 0
If olAlias = inAlias Then
GetFullName = olMem.GetExchangeUser.Name
Exit For
Else
GetFullName = "Invalid Alias"
End If
Next
Set olApp = Nothing: Set olNS = Nothing: Set olAdd = Nothing
End Function
The draw back is this may take a while if your GAL is quite large.
I'll check if I can dump the list to an array first and then manipulate from there.
Or if there is another way to get to the name via alias using other method.
But for now, try learning from this first.
This is a function, so to get it to your textbox, you can simply:
TextBox2 = GetFullname(TextBox1)
Note:
I intentionally declared all the objects, you need to know what type of object you're working on. Also I used On Error Resume Next because there are AddressEntry without Alias and that gives error. That's the easiest work around I can think of for now.
Requirement:
You need to reference Microsoft Outlook xx.x Object Library.
xx.x vary depending on the Outlook version installed in your machine.

VBA Runtime Error 13 Type Mismatch

I am trying to generate emails in a chosen Outlook folder from an Excel macro.
In the code below, 'TestDraft1' works, generating an email in the folder chosen by the user via the user the Namespace.PickFolder method.
However, since I want to generate many emails in the same folder, I need the user to be able to pick the folder just once. In 'TestDraft2' below, I try to save the folder as an Object, then pass it to the move function. This fails with "Runtime Error 13, Type Mismatch". The 'folder' variable gives the type "MAPIFolder".
What am I missing here? How is the object I assign to 'folder' different than the one I pass to 'Mailitem.move' ?
Edit Note that I'm using late-binding, which is why I'm defining my variables as Objects. I'd prefer to keep it that way, since I'd like to be able to distribute it without special configuration. What I really want to know is why the first version succeeds while the second version fails. Further details: My employer uses Office 2007.
Edit 2 The problem had nothing to do with what the type of object returned by PickFolder. It had to do with my misuse of VBA syntax. I've answered the question below, and I'm renaming the it to better address what I was really asking. Original title was: Returning an Outlook Folder object in Excel VBA using PickFolder
Sub test()
Call TestDraft1("test#example.com", "test1") ' succeeds
Call TestDraft2("test#example.com", "test2") ' fails
End Sub
Sub TestDraft1(recip As String, subj As String)
Dim OlApp As Object
Dim NS As Object
Dim OlMail As Object
Set OlApp = CreateObject("Outlook.Application")
Set NS = OlApp.GetNamespace("MAPI")
Set OlMail = OlApp.createitem(0) 'olMailitem = 0
OlMail.Subject = subj
OlMail.Recipients.Add (recip)
OlMail.Move (NS.PickFolder) ' pass the results of the folder-picker directly
End Sub
Sub TestDraft2(recip As String, subj As String)
Dim OlApp As Object
Dim NS As Object
Dim folder As Object
Dim OlMail As Object
Set OlApp = CreateObject("Outlook.Application")
Set NS = OlApp.GetNamespace("MAPI")
Set folder = NS.PickFolder ' save the results of the folder-picker...
' MsgBox (TypeName(folder)) ' returns "MAPIFolder
Set OlMail = OlApp.createitem(0) 'olMailitem = 0
OlMail.Subject = subj
OlMail.Recipients.Add (recip)
OlMail.Move (folder) ' ... and use the saved results of the folder-picker -> runtime error
End Sub
This shows how new I am to VBA syntax.
Replacing:
OlMail.Move (folder)
with
OlMail.Move folder
fixed the problem.
This is what I believe is going on: MailItem.Move returns no result, so it uses the non-parentheses syntax.
See: http://msdn.microsoft.com/en-us/library/office/gg278645
Thus, (folder) is interpreted as an expression, and returns the text value of the folder name inside the parentheses. Evidence:
Dim testobj as Object
Set testobj = (folder) ' fails: Object required
Dim testvar as Variant
testvar = (folder)
MsgBox (VarType(testvar) = vbString) ' returns True
So, while folder is an object of the correct type, (folder) is an expression which evaluates to a string.
maybe you just miss the objmail.save before moving it.
and your definitions are a problem for sure:
Change the first lines of your Sub as follows (untested):
Sub TestDraft2(recip As String, subj As String)
Dim olApp As Outlook.Application
Dim NS As Outlook.Namespace
Dim folder As Outlook.MAPIFolder
Dim OlMail As Outlook.mailitem
Set OlApp = CreateObject("Outlook.Application")
Set NS = OlApp.GetNamespace("MAPI")
Set folder = NS.PickFolder
that could help...

Resources