Object variable or With block variable not set

M

MikeC

I'm running automation code in AXP that sends an email via
Outlook XP (while using Outlook Redemption). I have a PST
file and POP3/SMTP on the local machine where the code is
executing. Everything works perfectly until the "Set Btn
= ..." line (at the bottom) is executed. The below error
displays:

"Error #91 - Object variable or With block variable not
set"

Can anyone tell me what I'm doing wrong?

(I'm cross-posting
to "microsoft.public.access.modulesdaovba"
and "microsoft.public.outlook.program_vba".)

Here's the code:
===================================================
Dim SafeItem As Object
Dim oItem As Object
Dim objOL As Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim Btn As Control
Dim strProfile As String
Dim strPwd As String
Dim strEmailAddrTo As String
Dim strSubject As String
Dim strMsg As String
Dim strPath As String

strEmailAddrTo = "(e-mail address removed)"

strSubject = "Subject of email"

strMsg = "MyMessage"

Set objOL = CreateObject("Outlook.Application")

Set myNameSpace = objOL.GetNamespace("MAPI")

strProfile = "My Profile Name"
strPwd = "MyPassword"

'Log into email account.
myNameSpace.Logon strProfile, strPwd, False, True

'Create an instance of Redemption.SafeMailItem
Set SafeItem = CreateObject("Redemption.SafeMailItem")

'Create a new message
Set oItem = objOL.CreateItem(0)

'set Item property
SafeItem.Item = oItem

SafeItem.Recipients.Add strEmailAddrTo
SafeItem.Recipients.ResolveAll
SafeItem.Subject = strSubject
SafeItem.Body = strMsg
SafeItem.Send

'vvvvv ERROR OCCURS ON NEXT LINE. vvvvvvvv
Set Btn = objOL.ActiveExplorer.CommandBars.FindControl
(msoControlButton, 5488)
Btn.Execute 'Emulates clicking on Send/Receive button
in Outlook XP.
===================================================
 
S

Sue Mosher [MVP-Outlook]

What is "AXP"?

Did you try omitting the first parameter in FindControl:

Set Btn = objOL.ActiveExplorer.CommandBars.FindControl(, 5488)

You might also want to test for an Explorer window first:

If Not objOL.ActiveExplorer Is Nothing Then
 
M

MikeC

Thanks Sue. That isolated the problem. Actually, there
were two problems:

1) The "If Not objOL.ActiveExplorer Is Nothing Then"
helped me to notice that Outlook was not visible. If it's
not visible, then the controls don't want to work.

2) Once problem #1 was solved, I got a runtime error
complaining about an object type mismatch. I changed
the "Btn" variable from "Control" to "Object" to make the
error go away.

Now it's working. Thanks again.
 
D

Dirk Goldgar

[...]
2) Once problem #1 was solved, I got a runtime error
complaining about an object type mismatch. I changed
the "Btn" variable from "Control" to "Object" to make the
error go away.

That makes sense, because your code, running in Access, would assume
that

Dim Btn As Control

was declaring an Access Control, not an Outlook Control (whatever that
may be; I know little about the Outlook object model).
 

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