Public Sub AddDigSig()
'Declare the object and data type that will be used.
Dim bAddSig As Boolean
'*** note that this should be defined fully, [i]possibly[/i] as Word.Signature
Dim sig As Signature
Dim wordapp As Word.Application
Dim worddoc as Word.Document
'When there is an error, go to Error_Handler for error handling.
On Error GoTo Error_Handler
Set wordapp = CreateObject("Word.Application")
Set worddoc = wordapp.Documents.Open ("c:\word\hola.doc")
wordapp.Visible = True
Set sig = worddoc.Signatures.Add
'Check the validity of the digital certificate used for signing.
'If it hasn't expired AND not revoked, then set bAddSig to True.
If sig.IsCertificateExpired = False And _
sig.IsCertificateRevoked = False Then
bAddSig = True
Else
'If it isn't valid, delete the signature on the document.
sig.Delete
bAddSig = False
End If
'Commit the signature. Until the Commit method is executed,
'none of the changes to the SignatureSet collection are saved.
worddoc.Signatures.Commit
'Clean up by destroying the sig object now that it isn't needed anymore.
Set sig = Nothing
'*** save and close the document
worddoc.Close SaveChanges := True
Set worddoc = Nothing
wordapp.Quit
Set wordapp = Nothing
Exit Sub
Error_Handler:
MsgBox "Document signing action has been cancelled."
Set sig = Nothing
'*** close (but dont save) the document
worddoc.Close SaveChanges := False
wordapp.Quit
Set wordapp = Nothing
End Sub
Public Sub DigSigValid()
'Declare the objects and data type that will be used.
Dim sSigValid As String
Dim objSignature As Signature
Dim wordapp As Word.Application
Dim worddoc as Word.Document
'When there is an error, go to Error_Handler for error handling.
On Error GoTo Error_Handler
'Check all of the signatures
sSigValid = ""
Set wordapp = CreateObject("Word.Application")
Set worddoc = wordapp.Documents.Open ("c:\word\hola.doc")
wordapp.Visible = True
For Each objSignature In worddoc.Signatures
sSigValid = sSigValid + "Is Signature from: " + objSignature.Signer _
+ " Valid?: " + Str$(objSignature.IsValid) + vbCrLf
Next objSignature
MsgBox (sSigValid)
Set objSignature = Nothing
'*** close (but dont save) the document
worddoc.Close SaveChanges := False
Set worddoc = Nothing
wordapp.Quit
Set wordapp = Nothing
Exit Sub
Error_Handler:
MsgBox "An error has occurred while trying to validate the digital signature. Please try running the DigSigValid macro again."
Set objSignature = Nothing
'*** close (but dont save) the document
worddoc.Close SaveChanges := False
wordapp.Quit
Set wordapp = Nothing
End Sub