OL wont close after VB app runs

Not open for further replies.


Senior Member
Outlook version
Email Account
Exchange Server
I've got a VB app that imports data into Outlook. The import goes much faster when OL is offline.

If OL is not running my code works perfectly, however if OL is currently running after my code execution OL can be closed by the user, but it only closes the OL window. The process continues to run in the background and has to be terminated via Task Manager.

I have narrowed the problem down to this line:

Set objCBCtl = myNS.folders.GetFirst.GetExplorer.CommandBars.FindControl(, 5613)

After that line executes OL wont completely close anymore.

Set mySession = CreateObject ("",Outlook.application)

' Also tried GetObject(, "Outlook.Application")

Set myNS = mySession.GetNamespace("MAPI")

Set objCBCtl = myNS.folders.GetFirst.GetExplorer.CommandBars.FindControl(, 5613)

'objCBCtl.execute ' Take Outlook offline




'objCBCtl.execute ' Take Outlook online

Set objCBCtl = Nothing

Set MyFolder = Nothing

Set myNS = Nothing

Set mySession = Nothing

Having the same problem on WinXP and Win7 OL2003 & OL 2010
How quickly are you closing after going online? I think the problem is that Outlook is trying to connect when you immediately close it. Online state should be controlled by a reg key - I'd probably look into setting the reg key to online, so its online when outlook starts up.

See Read and change a registry key using VBA - Slipstick Systems - the sample is long but you only need the save function. Remove all of the IF statements and it's about 3 lines long + the RegKeySave function.

According to Regshot these keys were changed but I'm not sure what all they do and will need to investigate with a profile that has one account and by making the changes with Outlook closed as the changes may not take effect until Outlook is reopened.

(your profile keys are under HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows messaging subsystem)


Name: 00030398

Values: 01 00 00 00 or 02 00 00 00


Name: 00036609

Value: 40 00 00 00 or 00 00 00 00

These belong to 2 of my 4 exchange accounts, 3 of which are on the same server, so it could be 1 key per exchange server.


Value: 00036609

Data: 00 00 00 00 or 40 00 00 00


Value: 00036609

Data: 00 00 00 00 or 40 00 00 00

I'm not sure this has any effect - the number changes often.


Name: LastChangeVer

Value: 2A 01 00 00 00 00 00 00 or 2E 01 00 00 00 00 00 00

HKCU\Software\Microsoft\Office\15.0\Outlook\Profiles\Outlook\9375CFF0413111d3B88A00104B2A6676\0000000x\ (where x = a number, one for each account)

Name: XP Status

Value: 0x00000001 or 0x00000003

Name: Identity Eid

it's the path to the data file - not sure what changed.
Thanks for your reply.

OL appears to look at the values when it is started, but changing them while it is running doenst appear to have any effect.

If I run my current current code it is working fine if OL is not running, so changing the registry would probably work for that scenario, but not if it is already running (which is when it is hanging).

It doesnt appear to matter when I try to close OL after running my code. Even if I dont actually change any data and OL says "all folders are up to date", I still have tried waiting several minutes but OL wont close normally.
AH, I thought the problem was that Outlook wouldn't exit when you set it back online. If so, you could set it online after outlook exits.
The problem is if Ol is online because the user has it open and is using it throughout the day.Then they want to import the new data (as I said I can do it with OL online, but it is much slower). My VBA code takes OL offline for a minute, adds the data and then takes it back online.

Everything looks good until the user tries to close OL at the end of the day and it hangs unless you kill it with TaskMan.
Did you try

Set mySession = CreateObject ("Outlook.application")


Set mySession = GetObject("", "Outlook.Application")

If mySession Is Nothing Then

Set mySession = CreateObject("Outlook.Application")

End If
The getobject/createobject is actually the code I have been using.

I actually tested further and found it is not the OFFLINE that is causing OL to hang, the mere reference to GetExplorer.CommandBars.Find... even reading the .state cause the hang.

I came up with what I though was a great workaround, but have not been able to get it to work.

Use the property application.GetNamespace ("MAPI").offline which unfortunately is read only to get the state.

If I call "folders.GetFirst.GetExplorer.CommandBars.Find Control(, 5613)" from within OL it doesnt cause the hang.

So I created a Public procedure in "This OL Session" to handle this. According to this article I should be able to call this OL procedure from VB/VBS using application.MyProcedure, but I havent be able to get this to work. I get an error "438 object doesnt support this property or method".

Any ideas?
That error is a variable error. is myNS declared public? Did you try Outlook.Application.folders.GetFirst.GetExplorer.CommandBars.Find Control(, 5613)

Have you considered using redemption to hit the setting without the commandbar? That might avoid the hang, but it will require redemption be installed.

RDO - Session - offline section

set Session = CreateObject("Redemption.RDOSession")

Session.MAPIOBJECT = Application.Session.MAPIOBJECT

Session.Offline = true
>That error is a variable error. is myNS declared public? Did you try Outlook.Application.folders.GetFirst.GetExplorer.C >ommandBars.Find Control(, 5613)

I think you are misunderstanding. I can get the OL procedure to run from within OL, it is calling it from VB that is producing the error.

(Actually my test code was a public procedure in OL/This OL session with just a message box).

(I can call application.MyProdedure from within OL, but not from my VP program)
Right. It works if you run it within Outlook VBA because Outlook can find the variable. When you call it from outside Outlook, the outsider needs to know what the variable is. You need to declare the variables in a module's General Declarations. It's not so much that it needs to be a public procedure but that the variable needs to be public
One of us is not understanding (probably me). Let me start over and take out all the extraneous info.

I want to have a procedure in Outlook VBA (and assume I should put it in "this outlook session")

Public Sub Test()
Msgbox ("Test")

End Sub

I want to call test from a VB app.

Private Sub Command1_Click()
Set mysession=createobject("outlook.application")
call mysession.test ' <--------------Trying to call OL Sub
Set mysession = Nothing

End sub

Throwing error 438. even though I wasnt clear about what you were saying I did try in the VB module declaration

Global mysession as object, Global mysession, dim mysession.

you keep referring to the "variable". What variable? what am i missing?

Thanks and sorry to be so dense!
This is the variable: mysession - but since you are declaring it in the app it's ok (for some reason, I thought it was in Outlook's code). It you use a variable in Outlook's code, it may need to be public.

It needs to be a public sub or public function in ThisOutlookSession.

This format should work - but it's unsupported and is a bit iffy - it works for some but not for others. It's not working in Outlook 2013.
Ok... switched to a VM running Outlook 2003 - and this works in it.
In ThisOutlookSession:

Public Sub MyProc(strParam)
MsgBox strParam

End Sub

In a vbs:

Dim olApp, strParam

Set olApp = CreateObject("Outlook.Application")

strParam = "This is a test"

olApp.MyProc (strParam)

Removing the string from the VBS to Outlook also works:

Public Sub MyProc()
MsgBox "This is a test"

End Sub


Dim olApp

Set olApp = CreateObject("Outlook.Application")


Just to make sure it works with more complicated code, I replaced the msgbox with code that actually does something (Create a new Outlook message using VBA - Slipstick Systems, if it matters) and it worked.
Oh, BTW, if outlook is not running, the call to the macro will fail. You'll need to call this only if outlook is not running.

Bad news... toggling the offline state and closing outlook leaves outlook running here. :(
Unfortunately I still cant call the macro externally using OL2010.

I just tried something else that appears to work, do you think this is a bad idea?

In the ThisOutlookSession - Application - Quit event I added "application.quit" and now OL appears to close "correctly" after executing my VB offline code.

Is this dangerous or a bad idea in any way?
I thought about using quit this morning but it quits outlook... not good if you want to keep it open.

BTW, apparently, using the command button from VBA opens the VBA editor, which is why Outlook doesn't close after using it.
Yes, but I am using application.quit in the CLOSE EVENT of OUTLOOK which does negate the hang.

Functionally it works great, but wondering if you think this has any hidden danger or negative side effects?
To the best of my knowledge, it should be fine. The only problem may be that outlook might take longer to reboot because it might need to check the data file because of the forced closure. That's not a big deal though and happens often, for many other reasons.
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
J GoDaddy migrated to Office365 - Outlook Wont Add Account Exchange Server Administration 21
M Emails Wont Upload to from MS Outlook 2010 to Server Using Outlook.com accounts in Outlook 9
P Vba script including macro appears in rules but wont run Outlook VBA and Custom Forms 6
M Outlook wont send emails but receives Using Outlook 1
F Auto retrieve wont work Using Outlook 1
D Outlook 2013 wont send to over 3 addresses Using Outlook 1
B Outlook contact, calendars, and tasks wont set up in icloud Using Outlook 4
K outlook calendar wont sync iphone 4 Using Outlook 1
Fozzie Bear Outlook 2010 wont remember Exchange Credentials on log off /shutdown of Win7 Exchange Server Administration 2
P Calender in Outlook 2007 wont open Using Outlook 4
M Outlook wont start Using Outlook 1
R Outlook Connector wont sync with iPhone/iPad Using Outlook.com accounts in Outlook 1
K Wont send or recieve now with new server keeps asking for password when trying Using Outlook 1
T Email wont send some of the time Using Outlook 3
C Business Contact manager wont open BCM (Business Contact Manager) 1
R Outlook wont .quit Outlook VBA and Custom Forms 4
macdotcom Outlook 365 Outlook folder export to PST archival tool - close, but need a nudge Outlook VBA and Custom Forms 2
S Why do I have to close and reopen Outlook for macros to work? Outlook VBA and Custom Forms 2
O Outlook 365 - Toolbar - Close all items - missing? Using Outlook 3
K Working with Explorer.Close event Outlook VBA and Custom Forms 3
Diane Poremsky How to Compact the Data File When you Close Outlook Using Outlook 0
Diane Poremsky Close a Meeting When the Room is Full Using Outlook 0
L Gmail POP, Android IMAP, Outlook 2013 setup close Using Outlook 5
D Close Oulook after sending emails via vba without outbox getting stuck. Outlook VBA and Custom Forms 1
D help with Item/Inspector close event Outlook VBA and Custom Forms 1
F Automatically close email after selecting mark unread Using Outlook 1
S Pop up reminder while i close outlook Outlook VBA and Custom Forms 5
Y (Shared Folder) Using the preview pane to open and close a fax, without opening the email itself Using Outlook 1
C Open/Close Outlook - Via Excel VBA Using Outlook 2
Q Looking for Outlook 2010 Close Event Id Exchange Server Administration 1
R Can't close Personal Folders after switching to Exchange 2010 Using Outlook 2
R Outlook and google calendar stuck if I close outlook Using Outlook 1
J Messages close themselves - Outlook 2007 Using Outlook 1
P Minimize button causes Outlook to close 2007 Using Outlook 1
D Cannot Close pst file Using Outlook 1
K The file c:\...\Outlook1.pst cannot be accessed because another workstation has modified it. Close a Using Outlook 2
R Outlook 2003 - Close parent form before opening child form Outlook VBA and Custom Forms 4
S Error 287 on Inspector.Close() Outlook VBA and Custom Forms 4
D Macro to Close Open Messages Outlook VBA and Custom Forms 3
E How to know if a Form.Close Event is the result of canceling Outlook VBA and Custom Forms 3
Q Why is BCM causing Outlook to close? BCM (Business Contact Manager) 2
V Move email using the Close event Outlook VBA and Custom Forms 11
R Close item Outlook VBA and Custom Forms 4
U Re: MailItem.Close() Outlook VBA and Custom Forms 1
H Outlook Does Not Really Close When I Close It BCM (Business Contact Manager) 1
R Outlook 365 How to integrate a third-party app with Outlook to track email and sms? Using Outlook 2
D Gmail mail is being delivered to a different email inbox in Outlook App 2021 Using Outlook 2
D Outlook app 2021 & iCloud PST issues Using Outlook 2
C Outlook (desktop app for Microsoft365) restarts every time I save my VBA? Using Outlook 1

Similar threads