R
RB Smissaert
Word 2003.
Try to setup some code that will trigger some action
on opening a document. This is either directly from a Word file (when Word
is not running yet)
or when opening a docement from Word. The code is in a global .dot file.
All this is done via AutoExec, which will either directly run the actions or
initialize a class that
picks up the DocumentOpen event. Problem is with the second one in that it
often (not always)
doesn't run.
This is the relevant, simplified code:
In a normal module:
---------------------------->
Public oAppClass As ThisApplication
Function ActiveDocumentPresent() As Boolean
On Error Resume Next
ActiveDocumentPresent = Len(ActiveDocument.Name) > 0
End Function
Sub SetThisApplication(strCallingProc As String)
Dim strSeconds As String
On Error GoTo ERROROUT
If oAppClass Is Nothing Then
If lWaitSecondsForInitializingThisApplicationClass = -1 Then
LogCode "SetThisApplication from " & strCallingProc & " (directly)"
InitThisApplication
Else
If lWaitSecondsForInitializingThisApplicationClass < 10 Then
strSeconds = "0" & lWaitSecondsForInitializingThisApplicationClass
Else
strSeconds = lWaitSecondsForInitializingThisApplicationClass
End If
If bAddDoEvents7 Then DoEvents
Set oAppClass = New ThisApplication
Set oAppClass.oWordApp = Word.Application
LogCode "SetThisApplication from " & strCallingProc & " (directly)"
If oAppClass Is Nothing Then
LogCode "SetThisApplication from " & strCallingProc & " (with
OnTime)"
Application.OnTime _
When:=Now + TimeValue("00:00:" & strSeconds), _
Name:="InitThisApplication"
End If
End If
End If
Exit Sub
ERROROUT:
MsgBox Err.Description, , _
"SetThisApplication - error at line: " & Erl
End Sub
Sub InitThisApplication()
If oAppClass Is Nothing Then
Set oAppClass = New ThisApplication
Set oAppClass.oWordApp = Word.Application
End If
End Sub
Sub AutoExec()
SetThisApplication "AutoExec"
If ActiveDocumentPresent() Then
RunAutoReplace "AutoExec"
Else
'1 second wait seems to be enough usually, unless it is a very large
document
'----------------------------------------------------------------------------
If lWaitSecondsForAutoReplace < 10 Then
strSeconds = "0" & lWaitSecondsForAutoReplace
Else
strSeconds = lWaitSecondsForAutoReplace
End If
Application.OnTime _
When:=Now + TimeValue("00:00:" & strSeconds), _
Name:="RunAutoReplaceAutoExec"
End If
End Sub
In a class module
---------------------->
Public WithEvents oWordApp As Word.Application
Private Sub oWordApp_DocumentOpen(ByVal Doc As Document)
RunAutoReplace "ThisApplication oWordApp_DocumentOpen"
End Sub
I have a lot of logging code and the class gets initialized fine and
triggers the DocumentOpen
when the default Document1 is loaded, but then fails to work after that, so
it doesn't pick up anymore
the DocumentOpen event.
This problem is intermittent and DoEvents can make a difference, but this is
unpredictable.
Any idea what the problem might be here.
Happy to post the .dot file that has all the code, it is about 0.5 Mb when
zipped.
Thanks for any assistance, RBS
Try to setup some code that will trigger some action
on opening a document. This is either directly from a Word file (when Word
is not running yet)
or when opening a docement from Word. The code is in a global .dot file.
All this is done via AutoExec, which will either directly run the actions or
initialize a class that
picks up the DocumentOpen event. Problem is with the second one in that it
often (not always)
doesn't run.
This is the relevant, simplified code:
In a normal module:
---------------------------->
Public oAppClass As ThisApplication
Function ActiveDocumentPresent() As Boolean
On Error Resume Next
ActiveDocumentPresent = Len(ActiveDocument.Name) > 0
End Function
Sub SetThisApplication(strCallingProc As String)
Dim strSeconds As String
On Error GoTo ERROROUT
If oAppClass Is Nothing Then
If lWaitSecondsForInitializingThisApplicationClass = -1 Then
LogCode "SetThisApplication from " & strCallingProc & " (directly)"
InitThisApplication
Else
If lWaitSecondsForInitializingThisApplicationClass < 10 Then
strSeconds = "0" & lWaitSecondsForInitializingThisApplicationClass
Else
strSeconds = lWaitSecondsForInitializingThisApplicationClass
End If
If bAddDoEvents7 Then DoEvents
Set oAppClass = New ThisApplication
Set oAppClass.oWordApp = Word.Application
LogCode "SetThisApplication from " & strCallingProc & " (directly)"
If oAppClass Is Nothing Then
LogCode "SetThisApplication from " & strCallingProc & " (with
OnTime)"
Application.OnTime _
When:=Now + TimeValue("00:00:" & strSeconds), _
Name:="InitThisApplication"
End If
End If
End If
Exit Sub
ERROROUT:
MsgBox Err.Description, , _
"SetThisApplication - error at line: " & Erl
End Sub
Sub InitThisApplication()
If oAppClass Is Nothing Then
Set oAppClass = New ThisApplication
Set oAppClass.oWordApp = Word.Application
End If
End Sub
Sub AutoExec()
SetThisApplication "AutoExec"
If ActiveDocumentPresent() Then
RunAutoReplace "AutoExec"
Else
'1 second wait seems to be enough usually, unless it is a very large
document
'----------------------------------------------------------------------------
If lWaitSecondsForAutoReplace < 10 Then
strSeconds = "0" & lWaitSecondsForAutoReplace
Else
strSeconds = lWaitSecondsForAutoReplace
End If
Application.OnTime _
When:=Now + TimeValue("00:00:" & strSeconds), _
Name:="RunAutoReplaceAutoExec"
End If
End Sub
In a class module
---------------------->
Public WithEvents oWordApp As Word.Application
Private Sub oWordApp_DocumentOpen(ByVal Doc As Document)
RunAutoReplace "ThisApplication oWordApp_DocumentOpen"
End Sub
I have a lot of logging code and the class gets initialized fine and
triggers the DocumentOpen
when the default Document1 is loaded, but then fails to work after that, so
it doesn't pick up anymore
the DocumentOpen event.
This problem is intermittent and DoEvents can make a difference, but this is
unpredictable.
Any idea what the problem might be here.
Happy to post the .dot file that has all the code, it is about 0.5 Mb when
zipped.
Thanks for any assistance, RBS