class module from template works only once

  • Thread starter Rob via OfficeKB.com
  • Start date
R

Rob via OfficeKB.com

Have a paragraph style applied a macrobutton field on a dot template. There
is a class module -my first- that changes the style to hidden so I can
print the doc without the button showing. After a delay I change the style
back so the button re-appears onscreen.

If I open a doc from template and print it the button doesn't get hidden.
But with the template also open on the desktop it will work, but most times
only once. If I go back into the template and run the
'register_Event_Handler' again it will work again - but mostly only once.

Can I get it to reliably run (everytime) and not have to have the template
open. It is Word 2003 and I not experienced with code or its theory. Given
that can you help?


'This is in the class module...

Public WithEvents App As Word.Application

Private Sub App_DocumentBeforePrint(ByVal Doc As Document, Cancel As
Boolean)
' hide ZZZ_Button Style so does not print
' see if style exists
Dim myStyle1 As Style
For Each myStyle1 In ActiveDocument.Styles
If myStyle1.NameLocal = "ZZZ_BUTTON" Then
With ActiveDocument.Styles("ZZZ_BUTTON").Font
.Hidden = True
End With
Else
'do nothing
End If
Next

Application.OnTime Now + TimeSerial(0, 0, 12), "AfterPrint"

End Sub

--------------------------

'This is in a general module by itself...
Option Explicit
Sub AfterPrint()
'unhide ZZZ_Button Style so viewable
' see if style exists
Dim myStyle As Style
For Each myStyle In ActiveDocument.Styles
If myStyle.NameLocal = "ZZZ_BUTTON" Then
With ActiveDocument.Styles("ZZZ_BUTTON").Font
.Hidden = False
End With
Else
'do nothing
End If
Next
End Sub

--------------------------------

'This is by itself in a General Module...

'registerclassmodule
Dim X As New StyleHide
Sub Register_Event_Handler()
Set X.App = Word.Application
End Sub
 
H

Howard Kaikow

sounds like the class is not being instantiated/terminated properly.
how to fix may vary depending on details of your code.
 

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