OL wont close after VB app runs

Status
Not open for further replies.

JackPollack

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
 

Forum Admin

Senior Member
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)

HKCU\Software\Microsoft\Office\15.0\Outlook\Profiles\Outlook\0a0d020000000000c000000000000046\

Name: 00030398

Values: 01 00 00 00 or 02 00 00 00

HKCU\Software\Microsoft\Office\15.0\Outlook\Profiles\Outlook\2c93f60f5051ae4aaa8cd2a0fbd7d84b\

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.

HKCU\Software\Microsoft\Office\15.0\Outlook\Profiles\Outlook\bb458a320a51a94ea608900fd2e7fef0\

Value: 00036609

Data: 00 00 00 00 or 40 00 00 00

HKCU\Software\Microsoft\Office\15.0\Outlook\Profiles\Outlook\d3ff91d069323f40903b9ab5c614f38e\

Value: 00036609

Data: 00 00 00 00 or 40 00 00 00

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

HKCU\Software\Microsoft\Office\15.0\Outlook\Profiles\Outlook\9375CFF0413111d3B88A00104B2A6676\

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.
 

JackPollack

Senior Member
Outlook version
Email Account
Exchange Server
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.
 

Forum Admin

Senior Member
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.
 

JackPollack

Senior Member
Outlook version
Email Account
Exchange Server
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.
 

JackPollack

Senior Member
Outlook version
Email Account
Exchange Server
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?
 

Forum Admin

Senior Member
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
 

JackPollack

Senior Member
Outlook version
Email Account
Exchange Server
>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)
 

Forum Admin

Senior Member
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
 

JackPollack

Senior Member
Outlook version
Email Account
Exchange Server
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!
 

Forum Admin

Senior Member
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.
mysession.macro_name()
 

Forum Admin

Senior Member
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

vbs:

Dim olApp

Set olApp = CreateObject("Outlook.Application")

olApp.MyProc

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.
 

Forum Admin

Senior Member
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. :(
 

JackPollack

Senior Member
Outlook version
Email Account
Exchange Server
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?
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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.
 

JackPollack

Senior Member
Outlook version
Email Account
Exchange Server
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?
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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.
 
Status
Not open for further replies.
Top