davis1118
12-06-2017, 08:51 AM
I have ran into an issue once again. I have a macro that runs before save to update footers, print to pdf, and create a backup document. The macro works when it is ran on a single document as just a macro, and it also works on a single document if it is loaded as a template. The problem is when I place the macro as a template in the startup folder. I don't see it load the add-in in the word startup pop up box, and nothing happens when I save a file. It shows up in the Active Application Add-In list on the add-in menu, and it is checked in the add-in prompt screen under Global Templates and Add-Ins. It also shows up in the project window in the VBA editor. I'm not sure if I am missing something in the code or if somehow the security settings at work are blocking it from running? But the startup folder is a trusted location, so I'm not sure how it would be blocked. There are hundreds of files that I want this code to run on, so I would like to have a global update rather than load the template to each file individually. Below it the code that I have.
ThisDocument
Private Sub Document_New()
Call Register_Event_Handler
End Sub
Private Sub Document_Open()
Call Register_Event_Handler
End Sub
Module (mdlEventConnect)
Dim X As New EventClassModule
Public Sub Register_Event_Handler()
Set X.App = Word.Application
End Sub
Class Module (EventClassModule)
Public WithEvents App As Word.Application
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
If SaveAsUI Then
GoTo lbl_End
Else
Dim myForm As frmSaveMethod
Dim i As Long, bAdd As Boolean: bAdd = True
Dim sTab As String
Dim oSection As Section
Dim oHF As HeaderFooter
Dim myName As String
Dim ext As String
Dim myPath As String
Dim T As String
Dim fso As Object
myName = Left(ActiveDocument.Name, (InStrRev(ActiveDocument.Name, ".") - 1))
ext = Right(ActiveDocument.Name, Len(ActiveDocument.Name) - InStrRev(ActiveDocument.Name, "."))
myPath = ActiveDocument.Path & "\"
T = Format(Now, "DDMMMyy")
On Error GoTo lbl_Error
'SAVE & UPDATE MESSAGE BOX
Set myForm = New frmSaveMethod
myForm.Show
Select Case myForm.Tag
Case 1
'FOOTER SECTION
With ActiveDocument.Sections.First.Footers(wdHeaderFooterPrimary).Range
For i = 1 To .Fields.Count
If .Fields(i).Type = wdFieldPage Then
bAdd = False: Exit For
End If
Next
sTab = vbTab & " "
If bAdd = True Then
.Fields.Add .Characters.Last, wdFieldEmpty, "FILENAME", False
.InsertAfter "_"
.Fields.Add .Characters.Last, wdFieldEmpty, "DATE \@ ""DDMMMyy""", False
.InsertAfter sTab
.Fields.Add .Characters.Last, wdFieldEmpty, "PAGE", False
.InsertAfter " of "
.Fields.Add .Characters.Last, wdFieldEmpty, "NUMPAGES", False
End If
End With
For Each oSection In ActiveDocument.Sections
For Each oHF In oSection.Footers
oHF.Range.Font.Size = 8
Next
Next
'-SAVE & BACKUP SECTION-
If ActiveDocument.Path = "" Then GoTo lbl_Exit
'SAVE AS PDF
ActiveDocument.ExportAsFixedFormat _
OutputFileName:=myPath & myName & ".pdf", _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=True
'SAVE IN ARCHIVE FOLDER
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(ActiveDocument.Path & "\Archive") Then
fso.CreateFolder (ActiveDocument.Path & "\Archive")
End If
fso.CopyFile ActiveDocument.FullName, ActiveDocument.Path & "\Archive\" & myName & "_" & T & "." & ext
ActiveDocument.Save
Case 2
'MORMAL SAVE - NO MACRO
ActiveDocument.Save
Case 3
'CANCEL BUTTON
Cancel = True
End Select
Unload myForm
Set myForm = Nothing
lbl_Exit:
Set fso = Nothing
End If
lbl_End:
Exit Sub
lbl_Error:
MsgBox "The following error occurred: " & Err.Description
Unload myForm
Set myForm = Nothing
End Sub
Thank you for the help. -David
ThisDocument
Private Sub Document_New()
Call Register_Event_Handler
End Sub
Private Sub Document_Open()
Call Register_Event_Handler
End Sub
Module (mdlEventConnect)
Dim X As New EventClassModule
Public Sub Register_Event_Handler()
Set X.App = Word.Application
End Sub
Class Module (EventClassModule)
Public WithEvents App As Word.Application
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
If SaveAsUI Then
GoTo lbl_End
Else
Dim myForm As frmSaveMethod
Dim i As Long, bAdd As Boolean: bAdd = True
Dim sTab As String
Dim oSection As Section
Dim oHF As HeaderFooter
Dim myName As String
Dim ext As String
Dim myPath As String
Dim T As String
Dim fso As Object
myName = Left(ActiveDocument.Name, (InStrRev(ActiveDocument.Name, ".") - 1))
ext = Right(ActiveDocument.Name, Len(ActiveDocument.Name) - InStrRev(ActiveDocument.Name, "."))
myPath = ActiveDocument.Path & "\"
T = Format(Now, "DDMMMyy")
On Error GoTo lbl_Error
'SAVE & UPDATE MESSAGE BOX
Set myForm = New frmSaveMethod
myForm.Show
Select Case myForm.Tag
Case 1
'FOOTER SECTION
With ActiveDocument.Sections.First.Footers(wdHeaderFooterPrimary).Range
For i = 1 To .Fields.Count
If .Fields(i).Type = wdFieldPage Then
bAdd = False: Exit For
End If
Next
sTab = vbTab & " "
If bAdd = True Then
.Fields.Add .Characters.Last, wdFieldEmpty, "FILENAME", False
.InsertAfter "_"
.Fields.Add .Characters.Last, wdFieldEmpty, "DATE \@ ""DDMMMyy""", False
.InsertAfter sTab
.Fields.Add .Characters.Last, wdFieldEmpty, "PAGE", False
.InsertAfter " of "
.Fields.Add .Characters.Last, wdFieldEmpty, "NUMPAGES", False
End If
End With
For Each oSection In ActiveDocument.Sections
For Each oHF In oSection.Footers
oHF.Range.Font.Size = 8
Next
Next
'-SAVE & BACKUP SECTION-
If ActiveDocument.Path = "" Then GoTo lbl_Exit
'SAVE AS PDF
ActiveDocument.ExportAsFixedFormat _
OutputFileName:=myPath & myName & ".pdf", _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=True
'SAVE IN ARCHIVE FOLDER
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(ActiveDocument.Path & "\Archive") Then
fso.CreateFolder (ActiveDocument.Path & "\Archive")
End If
fso.CopyFile ActiveDocument.FullName, ActiveDocument.Path & "\Archive\" & myName & "_" & T & "." & ext
ActiveDocument.Save
Case 2
'MORMAL SAVE - NO MACRO
ActiveDocument.Save
Case 3
'CANCEL BUTTON
Cancel = True
End Select
Unload myForm
Set myForm = Nothing
lbl_Exit:
Set fso = Nothing
End If
lbl_End:
Exit Sub
lbl_Error:
MsgBox "The following error occurred: " & Err.Description
Unload myForm
Set myForm = Nothing
End Sub
Thank you for the help. -David