add/remove store at runtime

  • Thread starter Thread starter Muechel
  • Start date Start date
M

Muechel

hi ng,

i want to open a pst file from the network, if the connection exists. to open
the file works fine, but i can not remove the file in application_quit

problem:
in the application_quit i get no access to the mapi folders. error message:
80004005 automation error

code:
Private Const szProjects = "<path to pst file>" ' like \\server\share\file.pst

Private Sub Application_Startup()

On Error Resume Next

Dim nsProjects As Outlook.NameSpace
Set nsProjects = Application.GetNamespace("MAPI")
If fso.FileExists(szProjects) Then
nsProjects.AddStore szProjects
Else
Dim fldProjects As MAPIFolder

If Not MAPIFolderExists(nsProjects.Folders, "Projects") Then Exit Sub

Set fldProjects = nsProjects.Folders.Item("Projects")

nsProjects.RemoveStore fldProjects

Set fldProjects = Nothing
End If
Set nsProjects = Nothing

End Sub

Private Sub Application_Quit()

Dim nsProjects As Outlook.NameSpace
Set nsProjects = Application.GetNamespace("MAPI")

' automation error occurs
If Not fld(2).Name = "<folder name>" Then Exit Sub

Dim fldProjects As Outlook.MAPIFolder
Set fldProjects = nsProjects.Folders.Item("Projects")
nsProjects.RemoveStore fldProjects

Set fldProjects = Nothing
Set nsProjects = Nothing

End Sub

i've also tried to create a new instance of the application object. i there any
solution for this problem.

can anybody help me?
 
Application_Quit is too late. You need to remove it when the last Explorer
window closes.
 
hi ng,

i've tried to use the explorer object like this:

Private WithEvents expActive As Explorer

Private Sub Application_Startup()
Set expActive = ActiveExplorer
End Sub

Private Sub Application_Quit()
Set expActive = Nothing
End Sub

Private Sub expActive_Close()
Debug.Assert False
End Sub

the close event will never occur. where is my mistake? other events of the
explorer like activate, deactivate, and so on ... are working fine
 
Try using Application.ActiveExplorer instead of ActiveExplorer. You also
need to handle the case where the user might have opened other Explorer
windows and closes the first one:

Private Sub expActive_Close()
If Application.Explorers.Count > 0 Then
Set expActive = Application.ActiveExplorer
Else
Set expActive = Nothing
End If
End Sub

You can omit the Application_Quit code completely.
 
hi sue,

thx a lot for your support.

i think you missunderstand my problem. the close event of the explorer or
application.activeexplorer will never occur, becouse of i don't want to open a
new explorer. i want to add a store (an other pst file) from a network drive.
this will work fine if i do it in the startup. the problem is that i also want
to remove the store if outlook will be closed. next time when outlook starts i
don't know if the network drive is online. the store that i've added will be
appear in the current explorer like:

+ Outlook <-- default personal folder
+ inbox
+ outbox
+ and so on ....
+ Projects <-- this is my store from the network

is there any way to remove this store when outlook will be closed?
 
No new Explorer is necessary. Instantiate the Explorer that's active when
Outlook starts and sink its Close event to handle your removal of the extra
stores with Namespace.RemoveStore when Outlook shuts down, i.e. when the
last Explorer closes.
 
hi sue

i've tried your suggestion in all my imaginable possibilities. i don't know why,
but the close event will only occur when i open a new instance of the explorer
by hand (right klick to any item and open in new window). in any other way only
the application_quit event will occur on closing outlook. all the other events
of the explorer object are working fine.

please execute these code lines, if you get the time for it, in your outlook and
tell me if it will work on your system:

--- snip -------------------------------------------------------------------
Option Explicit

Private WithEvents expActive As Outlook.Explorer

Private Sub Application_Quit()
Debug.Assert False
Debug.Print "Application_Quit()"
End Sub

Private Sub Application_Startup()
Set expActive = Application.ActiveExplorer
Debug.Print "Application_Startup()"
End Sub

Private Sub expActive_Close()
Debug.Print "expActive_Close()"
Debug.Assert False
End Sub
--- snap -------------------------------------------------------------------

i'm working with "outlook 2002 sp3" and the "api viewer 2004" and the "personal
folder backup" (pfbackup) plugins on "windows xp professional sp2". for testing
this, i've also deactivated the plugins. thx a lot for spending your valuable
time.
 
Back
Top