ThisOutlookSession - code runs only once

R

Rafael1119

All, I'm trying to use the code below as explained on this page:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnolbk02/html/odc_ch11olevents.asp

However, the code runs and it works fine when Outlook first runs but it does
not changes my view when I switch folders.

Any thoughts?

'ThisOutlookSession code
Private Sub Application_Startup()
Dim m_explevents As New ExplEvents
m_objExplorer_FolderSwitch
End Sub

Listing 11.7 Enforcing a Default Folder View
'ExplEvents class module
Private WithEvents m_colExplorers As Outlook.Explorers
Private WithEvents m_objExplorer As Outlook.Explorer

Sub Class_Terminate()
Call DeRefExplorers
End Sub

Public Sub InitExplorers(objApp As Outlook.Application)
Set m_colExplorers = objApp.Explorers
If m_colExplorers.Count > 0 Then
Set m_objExplorer = objApp.ActiveExplorer
End If
End Sub

Public Sub DeRefExplorers()
Set m_colExplorers = Nothing
Set m_objExplorer = Nothing
End Sub

Private Sub Application_Startup()
Dim m_explevents As New ExplEvents
m_explevents.InitExplorers Application
m_explevents.m_objExplorer_FolderSwitch
End Sub

Public Sub m_objExplorer_FolderSwitch()
Set myOlApp = CreateObject("Outlook.Application")
Dim olns As Outlook.NameSpace
Set olns = myOlApp.GetNamespace("MAPI")
Dim SearchFolder As Outlook.MAPIFolder
Dim myOlExp As Outlook.Explorer
Dim vw As Outlook.View
Set myOlExp = myOlApp.ActiveExplorer
Set SearchFolder = myOlExp.CurrentFolder
myType = SearchFolder.DefaultItemType
Set vw = SearchFolder.CurrentView
If myType = 0 Then
MsgBox myType
'Set current view to "By Company"
If Not vw.Name = "MyView" Then
myOlExp.CurrentView = "MyView"
End If
End If
Set myOlApp = Nothing
Set olns = Nothing
Set SearchFolder = Nothing
Set myOlExp = Nothing
Set vw = Nothing
Set myType = Nothing
End Sub
 
M

Michael Bauer

Am Wed, 12 Jul 2006 23:08:48 -0400 schrieb Rafael1119:

Application_Startup is usefull only in ThisOutlookSession. So move the code
from the second Application_Startup to the first one.

Additionally please replace
Set myOlApp = CreateObject("Outlook.Application")
by
Set myOlApp = Application
 
R

Rafael1119

What do you mean by "move the code from the second Application_Starup to the
first one"? I know it must be simple but I don't see what you mean.

Rafael
 
M

Michael Bauer

Am Thu, 13 Jul 2006 19:40:09 -0400 schrieb Rafael1119:

Please look at your code: There´re two Application_Startup procedures. Only
the first one, in ThisOutlookSession, will be called by Outlook.
 
R

Rafael1119

Well, here what my code looks like now in the "ThisOutlookSession" but still
no joy. It fires during OL startup only.

Private WithEvents m_colExplorers As Outlook.Explorers
Private WithEvents m_objExplorer As Outlook.Explorer

Private Sub Application_Startup()
Dim m_explevents As New ExplEvents
m_explevents.InitExplorers Application
m_explevents.m_objExplorer_FolderSwitch
End Sub


Sub Class_Terminate()
Call DeRefExplorers
End Sub

Public Sub InitExplorers(objApp As Outlook.Application)
Set m_colExplorers = objApp.Explorers
If m_colExplorers.Count > 0 Then
Set m_objExplorer = objApp.ActiveExplorer
End If
End Sub

Public Sub DeRefExplorers()
Set m_colExplorers = Nothing
Set m_objExplorer = Nothing
End Sub

Private Sub m_colExplorers_NewExplorer _
(ByVal Explorer As Explorer)
Set m_objExplorer = Explorer
End Sub

Public Sub m_objExplorer_FolderSwitch()
Set myOlApp = Application
Dim olns As Outlook.NameSpace
Set olns = myOlApp.GetNamespace("MAPI")
Dim SearchFolder As Outlook.MAPIFolder
Dim myOlExp As Outlook.Explorer
Dim vw As Outlook.View
Set myOlExp = myOlApp.ActiveExplorer
Set SearchFolder = myOlExp.CurrentFolder
myType = SearchFolder.DefaultItemType
Set vw = SearchFolder.CurrentView
MsgBox myType
If myType = 0 Then
'Set current view to "By Company"
If Not vw.Name = "MyView" Then
myOlExp.CurrentView = "MyView"
End If
End If
Set myOlApp = Nothing
Set olns = Nothing
Set SearchFolder = Nothing
Set myOlExp = Nothing
Set vw = Nothing
Set myType = Nothing
End Sub
 
M

Michael Bauer

Am Fri, 14 Jul 2006 19:41:12 -0400 schrieb Rafael1119:

From what I can see, only Application_Startup should be in
ThisOutlookSession, the rest in the ExplEvents class.

If that´s the case, please set a breakpoint (F9) on the

Public Sub m_objExplorer_FolderSwitch()

line (which is Private by default, not Public) and see is the execution
stops there. As long as you do not switch explorers, but folders, it should
do so.

For being able to switch also between explorers you need to have a class
module for each explorer.
 
R

Rafael1119

Michael,

I moved all my code out of ThisOutlookSession except for the event handler
below:

Private Sub Application_Startup()
Dim m_explevents As New ExplEvents
m_explevents.InitExplorers Application
'm_explevents.m_objExplorer_FolderSwitch
End Sub

I removed the folder Switch event handler because as you mentioned, it's
typically private on the explevents class moduel which I changed back to
"Private".

The problem I have when setup this way is that this event does not fire at
all.

I'm going to try this with VSTO so will ping the VSTO newsgroup unless
you/others have some more detail instructions (if you have it working on
your end) on how to do this.

Thanks much for your efforts.

Rafael
 
M

Michael Bauer

Am Sat, 15 Jul 2006 10:03:43 -0400 schrieb Rafael1119:

I didn´t ask you to remove the FolderSwitch event procedure. So this is what
you need to have at least and where it should be located:

Now I see another error: Because you declare m_explevents within
Application_Startup it terminates as soon as the execution leaves that
procedure. Due to that you don´t have any event handlers.

Instead declare that variable also on the module level as shown:

<ThisOutlookSession>
Private m_explevents As ExplEvents

Private Sub Application_Startup()
Set m_explevents = New ExplEvents
End Sub
</ThisOutlookSession>

<ExplEvents>
Private WithEvents m_colExplorers As Outlook.Explorers
Private WithEvents m_objExplorer As Outlook.Explorer

Public Sub InitExplorers(objApp As Outlook.Application)
End Sub

Public Sub DeRefExplorers()
End Sub

Private Sub m_colExplorers_NewExplorer _
(ByVal Explorer As Explorer)
End Sub

Public Sub m_objExplorer_FolderSwitch()
End Sub
</ExplEvents>
 
R

Rafael1119

By Michael I think I've got it... Thanks Michael, it works like a charm.

Regards,

Rafael
 

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