Edited by TroyWells on 13-Aug-02 14:29.
I followed the instructions in online help (Topic “Using Events with the Application Object”) for using a class module to access the Windows_Activate event. But I’m having a couple of problems:
1) The class module is located in a global template file. When the file is loaded, I would think it is considered to be open. In fact, I have an AutoOpen macro that I have confirmed does fire (I see a msgbox). However, only when I have the global template file open (by going to the file menu and then open) does the event seem to work. I run a procedure during the AutoOpen to initialize the declared object.
2) When it does fire (when I have the global template file open using the File menu and then Open), the action I am performing is the installing and uninstalling of addins, each of which has a custom menu. It seems to be uninstalling/installing correctly, but the menus don’t reflect that until I go to a different document. If I am uninstalling some addins, Application.ScreenRefresh seems to work. But if I am reinstalling addins. Nothing I’ve tried seems to work.
Any ideas for what I am doing wrong?
Thanks for the help!!
Troy
P.S. OK I was lazy. here is the code:
Class Module:
Public WithEvents App As Word.Application
Public GlobalTemplatePath As String
Private Sub App_WindowActivate(ByVal Doc As Document, ByVal Wn As Window)
‘ClassModuleProcedures.UnloadInappropriateAddins
Dim ArrayAddins()
Dim I, J, AddinsCount As Integer
Dim AddinPath, AddinName As String
Dim PropExists As Boolean
PropExists = False
ReDim ArrayAddins(2, 0)
AddinsCount = AddIns.Count
For I = 1 To ActiveDocument.CustomDocumentProperties.Count
If ActiveDocument.CustomDocumentProperties.Item(I).Name = “Document Type” Then
PropExists = True
Exit For
End If
Next I
If PropExists = False Then
With ActiveDocument.CustomDocumentProperties
.Add Name:=”Document Type”, _
LinkToContent:=False, _
Type:=msoPropertyTypeString, _
Value:=””
End With
End If
If ActiveDocument.CustomDocumentProperties(“Document Type”).Value = “Tech Cover” Or _
ActiveDocument.CustomDocumentProperties(“Document Type”).Value = “Tech Chapter” Or _
ActiveDocument.CustomDocumentProperties(“Document Type”).Value = “Tech CoverE” Or _
ActiveDocument.CustomDocumentProperties(“Document Type”).Value = “Tech TOC” Or _
ActiveDocument.CustomDocumentProperties(“Document Type”).Value = “Tech Chapter” Or _
ActiveDocument.CustomDocumentProperties(“Document Type”).Value = “Tech Index” Or _
ActiveDocument.CustomDocumentProperties(“Document Type”).Value = “Tech Glossary” Or _
ActiveDocument.CustomDocumentProperties(“Document Type”).Value = “Tech All” Then
For I = 1 To AddinsCount
ReDim Preserve ArrayAddins(2, UBound(ArrayAddins, 2) + 1)
ArrayAddins(2, UBound(ArrayAddins, 2)) = AddIns.Item(I)
ArrayAddins(1, UBound(ArrayAddins, 2)) = AddIns.Item(I).Path & “”
Next I
‘ AddIns.Unload RemoveFromList:=False
For J = 1 To AddIns.Count
If AddIns.Item(J) “TechTemplate.dot” Then AddIns.Item(J).Installed = False
Next J
For I = 1 To UBound(ArrayAddins, 2)
If ArrayAddins(2, I) “FormatDocuments.dot” And _
ArrayAddins(2, I) “2WebTemplate.dot” And _
ArrayAddins(2, I) “TGPChap.dot” And _
ArrayAddins(2, I) “BizDoc.dot” And _
ArrayAddins(2, I) “UserDoc.dot” Then
On Error GoTo StartErrorHandler1
AddIns.Add FileName:=ArrayAddins(2, I), Install:=True
GoTo StopErrorHandler1
StartErrorHandler1:
If Err.Number = 4160 Then
AddinPath = ArrayAddins(1, I)
AddIns.Add FileName:=AddinPath & ArrayAddins(2, I), Install:=True
Else
MsgBox Err.Number
Exit Sub
End If
StopErrorHandler1:
On Error GoTo 0
End If
Next I
Else
EElse:
For J = 1 To AddIns.Count
AddIns.Item(J).Installed = True
Next J
End If
Application.ScreenRefresh
End Sub
Registering Sub:
Dim X As New EventClassModule
Sub Register_Event_Handler()
Set X.App = Word.Application
End Sub