Hey Tony, glad to see you here!
I'm wondering if you might help me a bit with getting Gary an answer. I've tried to adapt what I would do for Excel, but Word works a little differently, of course.
My thought was to create a custom document property to hold the opening signature count, then check against that count upon closing. If the count has increased (I'm not sure if you can have more than one signature, but I assume so) then email the file.
Here's the code I mocked up:
Code:
Public Property Let SigCount(lng As Long)
'Author : Ken Puls (www.excelguru.ca)
'Macro Purpose: Sets the SigCount property to requested value
Dim DocProps As DocumentProperties
'Save Version value in a customdocument property
On Error Resume Next
ThisDocument.CustomDocumentProperties("SigCount") = lng
'If customdocument property did not exist, create it
If Err.Number <> 0 Then
Set DocProps = ThisDocument.CustomDocumentProperties
DocProps.Add Name:="SigCount", _
LinkToContent:=False, _
Type:=msoPropertyTypeNumber, _
Value:=lng
End If
On Error GoTo 0
End Property
Public Property Get SigCount() As Long
'Author : Ken Puls (www.excelguru.ca)
'Macro Purpose: Return Version from customdocument property
Dim DocProps As DocumentProperties
'Check the opening signature count
On Error Resume Next
SigCount = ActiveDocument.CustomDocumentProperties("SigCount")
'If customdocument property did not exist, create it
If Err.Number <> 0 Then
Set DocProps = ThisDocument.CustomDocumentProperties
DocProps.Add Name:="SigCount", _
LinkToContent:=False, _
Type:=msoPropertyTypeNumber, _
Value:=0
End If
On Error GoTo 0
End Property
Private Sub Document_Close()
'Author : Ken Puls (www.excelguru.ca)
'Macro Purpose: Sets the SigCount property to requested value
Dim oEmail As New clsOutlookEmail
With ThisDocument
If .SigCount < .Signatures.Count Then
'Update signature count
.SigCount = .Signatures.Count
With oEmail
'Add a recipient
.AddToRecipient = "To_someone@somedomain.com"
.AddToRecipient = "To_someoneelse@somedomain.com"
'Set the subject
.Subject = "File has been signed"
'Set the body
.Body = "Hi there" & vbNewLine & vbNewLine & _
"It looks like this file was signed."
'Attach the file
'Not sure if this will work as document has not yet been saved...
.AttachFile = ThisDocument.FullName
'Preview the email (or use .Send to send it)
.Preview
End With
End If
End With
'Release the email object
Set oEmail = Nothing
End Sub
It does also require pulling in my Easy Outlook Email Integration class module for the email part too.
Now, the questions...
- Word has such a lack of events! All I could find to trigger this would be a Document_Close event (really wanted a Document_BeforeSave)
- How do you force the file created from the template to be a docm file so that it contains the code so that it will fire next time it's opened?
- Excel has a SaveCopyAs method, which allows you to save a temp copy to attach to the email, then you can kill it later. Do you think just adding the current document will work, or... do we need to force a SaveAs to a temp path, then back to the original path, then clean up? Sounds a little hairy... especially if the document hasn't yet been saved.
Gary, can you confirm, will a document ever get more than one signature?
Bookmarks