CreateObject("Outlook.Application") Not Working With Add-in

M

Marc

One of my VB6 Outlook COM add-ins is wreaking havoc on a new VBScript I'm
creating.

The script has something like this:

'----------------------------------------
' Get Outlook if it's running
Set oOutlookApp = GetObject("", "Outlook.Application")
If oOutlookApp Is Nothing Then
' Outlook wasn't running, start it from code
' !!THIS IS FAILING BECAUSE OF MY ADD-IN !!
Set oOutlookApp = CreateObject("Outlook.Application")
End If
'----------------------------------------

This causes my error trapper in my COM add-in to launch my custom error
message, but then Outlook doesn't start. It just dies.

Here's my add-in code that's evidently not robust enough to handle external
apps launching Outlook:

'----------------------------------------
Private Sub IDTExtensibility2_OnConnection( _
ByVal oApplication As Object, _
ByVal iConnectMode As ext_ConnectMode, _
ByVal oAddinInst As Object, _
vCustom() As Variant)
On Error GoTo Catch
Set Application = oApplication
Set moInspectors = Application.Inspectors
Set moButtons = New AddinButtons
Set moButtons.Parent = Me
Call
moButtons.Add(CreateButton(CreateCommandBar(Application.Explorers.Item(1).CommandBars)))
Exit Sub
Catch:
MsgBox "Addin.IDTExtensibility2_OnConnection 0x" & Hex(Err.Number) & "
-- " & Err.Description
End Sub

Private Function CreateCommandBar(ByVal oCommandBars As CommandBars) As
CommandBar
' See if the toolbar is already there.
Set CreateCommandBar = FindCommandBar(oCommandBars, "XyzCorp")
' If it isn't make it.
If CreateCommandBar Is Nothing Then _
Set CreateCommandBar = oCommandBars.Add("XyzCorp", msoBarTop, False,
True)
With CreateCommandBar
.RowIndex = 2
.Left = 10
.Visible = True
End With
End Function

Private Function CreateButton(ByVal oCommandBar As CommandBar) As
CommandBarButton
Const BUTTON_CAPTION As String = "&Cool Button"
Dim oControl As CommandBarControl
For Each oControl In oCommandBar.Controls
If oControl.Caption = BUTTON_CAPTION Then oControl.Delete
Next
Set oControl = oCommandBar.Controls.Add(Type:=msoControlButton,
Temporary:=True)
oControl.Style = msoButtonCaption
oControl.Caption = BUTTON_CAPTION
oControl.Style = msoButtonIconAndCaption
oControl.Picture = LoadResPicture("BUTTON_PICTURE", vbResBitmap)
oControl.Mask = LoadResPicture("BUTTON_MASK", vbResBitmap)
Set CreateButton = oControl
End Function
'----------------------------------------

The error is "Addin.IDTExtensibility2_OnConnection 0x84120009 -- Array index
out of bounds.". I'm confused because this error never occurs when Outlook
is started normally by its shortcut. Please help.
 
K

Ken Slovak - [MVP - Outlook]

You aren't checking to see whether the Explorers collection has any
Explorers in it (Explorers.Count > 0). You really should do that and set
your initial Explorer object to ActiveExplorer and if that fails or .Count =
0 then gracefully exit from your addin, or keep running it but not
initializing it until the NewExplorer event fires and you actually have a
UI.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top