Excel talking to Outlook throws 462 errors

JeffGrant

Well-known Member
Joined
Apr 7, 2021
Messages
558
Office Version
  1. 365
Platform
  1. Windows
Hi All,
in my model I have several subs that use the following code to send info from excel to various outlook calendars. Generally, it is not an issue. However, this week i have programmed something new that is sending multiple bits of information in short succession to outlook calendars in a short period of time. What I think is happening is that vba is throwing 462, server not available errors because there is confusion between creating a new outlook instance and using a current outlook exitance.


when clicking on Debug, the line Set olNs = olApp.GetNamespace("MAPI") is where the debugger goes.

If I am correct about the vb confusion between creating an outlook instance using the current outlook instance, i assume then I would have thought the If olApp test would have handled that and the olApp.Getnamespace would be fine. Clearly I am wrong,otherwise I would be getting the 462 error.

Can somebody please help me on this one.

Thank in advance.


This code is in 4 subs, although, I do suspect that one high volume sub is the culprit.

Dim olApp As Outlook.Application
Dim olAppt As Outlook.AppointmentItem
Dim blnCreated As Boolean
Dim olNs As Outlook.Namespace
Dim CalFolder As Outlook.MAPIFolder
Dim subFolder As Outlook.MAPIFolder
Dim arrCal As String

On Error Resume Next

Set olApp = Outlook.Application

If olApp Is Nothing Then
Set olApp = Outlook.Application
blnCreated = True
Err.Clear
Else
blnCreated = False
End If

On Error GoTo 0

Set olNs = olApp.GetNamespace("MAPI")
Set CalFolder = olNs.GetDefaultFolder(olFolderCalendar)
Set subFolder = CalFolder.Folders(arrCal)
Set olAppt = subFolder.Items.Add(olAppointmentItem)
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hi All,

my research has encouraged me to add this coded to the sub causing the issue:

Dim OutlookApp As Object
Dim OutlookStarted As Boolean
OutlookStarted = False

On Error Resume Next

' Try to get an existing Outlook instance
Set OutlookApp = GetObject(, "Outlook.Application")

' Check if Outlook is already running
If Err.Number = 0 Then
OutlookStarted = True
Else

' Outlook is not running, so create a new instance
On Error GoTo 0
Set OutlookApp = CreateObject("Outlook.Application")
OutlookStarted = True
End If

I have added above this line, Set olNs = olApp.GetNamespace("MAPI"), which is where the debugger always stops.

What I don't know is:

1. by doing this, am I explicitly refering to the outlook instance which seems to be "assumed" in my first post?
2. Am I providing a second set of code that actually achieves the same result?

thanks
 
Upvote 0
Your variable is OutlookApp not olApp
 
Upvote 0
Thanks mate. Fixed. Hoping tomorrow will give an error free day :-)
 
Upvote 0
Hi All,
my model sends data from excel to various calendars in outlook.
The code below appears in 4 subs, each sub has different criteria for each calendar

VBA Code:
Dim olApp As Outlook.Application
Dim olAppt As Outlook.AppointmentItem
Dim olNs As Outlook.Namespace
Dim CalFolder As Outlook.MAPIFolder
Dim subFolder As Outlook.MAPIFolder

On Error Resume Next
If olApp Is Nothing Then
Set olApp = CreateObject("Outlook.Application")
....
Else
Set olApp = GetObject("Outlook.Application")
....
End If

On Error GoTo 0
Set olNs = olApp.GetNamespace("MAPI")
Set CalFolder = olNs.GetDefaultFolder(olFolderCalendar)
Set subFolder = CalFolder.Folders(arrCal)
Set olAppt = subFolder.Items.Add(olAppointmentItem)

All of these objects are set to
VBA Code:
Nothing
at the end of each sub

Occasionally, I get a 462 Server Not available message always on this line:
VBA Code:
Set olNs = olApp.GetNamespace("MAPI")
and always from the same sub.

Originally I thought this was a timing issue between subs, consequently there is an Application.Wait statment for 5 seconds at the end of every sub. This Wait statement did dramatically reduce the amount of times the error was occuring.

Because this type of coding is far more advanced than I, I am not sure if the Wait statement is the best solution.

When this 462 error does pop up, my model stops. Because this is an automation model, I need to ensure that errors like this do not pop up.

As always, any and all assistance is greatly appreciated.
 
Upvote 0
with this code, which I pinched off a site somewhere, can somebody please explain what is the point of the If Statement if the Set statement is directly above it.

to my understanding the If statement is redundant because it will never be set to nothing. Is that correct?

VBA Code:
On Error Resume Next
        Set olApp = Outlook.Application
        
        If olApp Is Nothing Then
            Set olApp = Outlook.Application
             blnCreated = True
            Err.Clear
        Else
            blnCreated = False
        End If
        
        On Error GoTo 0
 
Upvote 0
That code should only be used in Outlook, in which case the If is completely pointless.
 
Upvote 0
Solution
Hi Rory, that's what I thought. It wasn't making sense. Just goes to show can't believe everything I read on the internet hey :-)
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,080
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top