A
anders
Hi,
I have made a setup in a word template that is supposed to catch any Print or Document close events.
1. I create a new doc based on the template
2. do some changes
3. press print button (or ctrl + p)
4. the event is not being catched.
Then if I:
1. Save the document
2. close it.
3. reopen
4. press print button (or ctrl + p)
5. the event is now being catched and everything is fine.
What is the reason?
I have tried to move the code into normal.dot, but with the same result.
Link to document template:
https://dl.dropbox.com/u/54847100/TestTemplate.dotm
Further the template is located in a location administered by ADS, and macros are automatically enabled on documents created from templates located in that specific folder. Changing macro settings to "allow all" does not resolve the issue.
The code looks like this:
-- == In "This Document" -- ==
Private Sub CommandButton1_Click()
Call Module1.PreDocSaveCommands
End Sub
Private Sub Document_Open()
Call Register_Event_Handler
End Sub
-- == In "Module1" -- ==
Dim X As New Class1
Public Sub Register_Event_Handler()
Set X.App = Word.Application
End Sub
Public Sub PreDocSaveCommands()
Application.ScreenUpdating = False
'Loop through all "Fields" and update
Dim oStory As Range
Dim oField As Field
Dim rng As Variant
Dim rng2 As Variant
On Error Resume Next
Set rng2 = ActiveDocument.Selected
Set rng = ActiveDocument.StoryRanges
For Each oStory In rng
For Each oField In oStory.Fields
oField.Update
Next oField
Next oStory
On Error GoTo 0
'Correct font issue in footer
WordBasic.ViewFooterOnly
Selection.WholeStory
Selection.Font.Name = "Cambria"
Selection.Font.Size = 8
Selection.Font.Bold = False
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
'Correct font issue in footer
WordBasic.ViewHeaderOnly
Selection.WholeStory
Selection.Font.Name = "Cambria"
Selection.Font.Size = 8
Selection.Font.Bold = False
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
'Update ToC and Bibliography, completely
Selection.Fields.Update
On Error Resume Next
For Each toc In ActiveDocument.TablesOfContents
toc.Update
Next toc
'footer updating
' If Application.PrintPreview = False Then
' ActiveDocument.PrintPreview
' Application.PrintPreview = False
' End If
WordBasic.ViewFooterOnly
Selection.Fields.Update
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
On Error Resume Next
Set rng = ActiveDocument.Bibliography
For Each bib In rng
bib.Update
Next bib
Application.ScreenUpdating = True
rng2.Select
End Sub
-- == In "Class1" == --
Public WithEvents App As Word.Application
Private Sub App_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
Dim bool As Boolean
Call DocCheck(bool)
If bool Then Call UserPrompt
End Sub
Private Sub App_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)
Dim bool As Boolean
Call DocCheck(bool)
If bool Then Call UserPrompt
End Sub
'Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
'Dim bool As Boolean
'Call DocCheck(bool)
'If bool Then Call UserPrompt
'End Sub
Private Sub DocCheck(bool As Boolean)
'this command check if the document has the correct value stored in the "Office custom document property". It serves as a signature that the document is based on the template.
'only correct value in this "office" property will allow the macros to kick
On Error GoTo errHandler
m = ActiveDocument.CustomDocumentProperties("Office").Value
bool = False
If m = "x123" Then bool = True
'this check is only made to prevent macro from running on documents not based on the template
Exit Sub
errHandler:
bool = False
End Sub
Private Sub UserPrompt()
Dim m As Integer
m = MsgBox("Update Table of Contents, footer, and header?", vbYesNo, "BeforeCloseCatch")
If m = 6 Then
Call PreDocSaveCommands
End If
End Sub
I have made a setup in a word template that is supposed to catch any Print or Document close events.
1. I create a new doc based on the template
2. do some changes
3. press print button (or ctrl + p)
4. the event is not being catched.
Then if I:
1. Save the document
2. close it.
3. reopen
4. press print button (or ctrl + p)
5. the event is now being catched and everything is fine.
What is the reason?
I have tried to move the code into normal.dot, but with the same result.
Link to document template:
https://dl.dropbox.com/u/54847100/TestTemplate.dotm
Further the template is located in a location administered by ADS, and macros are automatically enabled on documents created from templates located in that specific folder. Changing macro settings to "allow all" does not resolve the issue.
The code looks like this:
-- == In "This Document" -- ==
Private Sub CommandButton1_Click()
Call Module1.PreDocSaveCommands
End Sub
Private Sub Document_Open()
Call Register_Event_Handler
End Sub
-- == In "Module1" -- ==
Dim X As New Class1
Public Sub Register_Event_Handler()
Set X.App = Word.Application
End Sub
Public Sub PreDocSaveCommands()
Application.ScreenUpdating = False
'Loop through all "Fields" and update
Dim oStory As Range
Dim oField As Field
Dim rng As Variant
Dim rng2 As Variant
On Error Resume Next
Set rng2 = ActiveDocument.Selected
Set rng = ActiveDocument.StoryRanges
For Each oStory In rng
For Each oField In oStory.Fields
oField.Update
Next oField
Next oStory
On Error GoTo 0
'Correct font issue in footer
WordBasic.ViewFooterOnly
Selection.WholeStory
Selection.Font.Name = "Cambria"
Selection.Font.Size = 8
Selection.Font.Bold = False
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
'Correct font issue in footer
WordBasic.ViewHeaderOnly
Selection.WholeStory
Selection.Font.Name = "Cambria"
Selection.Font.Size = 8
Selection.Font.Bold = False
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
'Update ToC and Bibliography, completely
Selection.Fields.Update
On Error Resume Next
For Each toc In ActiveDocument.TablesOfContents
toc.Update
Next toc
'footer updating
' If Application.PrintPreview = False Then
' ActiveDocument.PrintPreview
' Application.PrintPreview = False
' End If
WordBasic.ViewFooterOnly
Selection.Fields.Update
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
On Error Resume Next
Set rng = ActiveDocument.Bibliography
For Each bib In rng
bib.Update
Next bib
Application.ScreenUpdating = True
rng2.Select
End Sub
-- == In "Class1" == --
Public WithEvents App As Word.Application
Private Sub App_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
Dim bool As Boolean
Call DocCheck(bool)
If bool Then Call UserPrompt
End Sub
Private Sub App_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)
Dim bool As Boolean
Call DocCheck(bool)
If bool Then Call UserPrompt
End Sub
'Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
'Dim bool As Boolean
'Call DocCheck(bool)
'If bool Then Call UserPrompt
'End Sub
Private Sub DocCheck(bool As Boolean)
'this command check if the document has the correct value stored in the "Office custom document property". It serves as a signature that the document is based on the template.
'only correct value in this "office" property will allow the macros to kick
On Error GoTo errHandler
m = ActiveDocument.CustomDocumentProperties("Office").Value
bool = False
If m = "x123" Then bool = True
'this check is only made to prevent macro from running on documents not based on the template
Exit Sub
errHandler:
bool = False
End Sub
Private Sub UserPrompt()
Dim m As Integer
m = MsgBox("Update Table of Contents, footer, and header?", vbYesNo, "BeforeCloseCatch")
If m = 6 Then
Call PreDocSaveCommands
End If
End Sub