Word 2003 and Digital Signatures

K

karflips33

Is it possible to automate the signing of Word 2003 docs with
Digital Signatures?


My problem with the code approach using


ActiveDocument.Signatures.Add


is that Word comes up with a prompt. Obviously this is no good
when trying to automate document creation, etc.


There doesn't appear to be any way to specify a particular
certificate, or to suppress the prompt.


I assume there must be some way to add digital signatures to
Word 2003 docs thru code, but how?


I am using VB.Net to automate Word.


Any suggestions gratefully received.
 
K

karflips33

Well, after several days of sweat and tears I've finally come up with
something.

It's almost the hackiest code I've ever written but it works. If anyone
comes
up with something better, please post it.

In the end I run the ActiveDocument.Signatures.Add code as normal and
then
"hit enter" from another thread, making sure to direct the keystroke to
the
correct window.


'waiting for certificate prompt to be shown
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

'API declarations, etc that allow us to talk directly to our running
instance of Word
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA"
(ByVal lpClassName As String, ByVal lpWindowName As String) As Int32
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA"
(ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal
lParam As Int32) As Int32
Public Const WM_CHAR = &H102
Public Const VK_RETURN = &HD
Public Const WM_KEYDOWN = &H100
Public Const WM_KEYUP = &H101
Public rc As Int32


sub SignDoc()

wrdApp.Documents.Open(drContracts("DocLocation"))

'''
'
'We want to digitally sign the doc, make it read only for the client
Dim sig As Microsoft.Office.Core.Signature
Dim t As WordSignatureDelegate
t = AddressOf WordSignature
If blnFirstRun Then
t.BeginInvoke(True, Nothing, Nothing)
Else
t.BeginInvoke(False, Nothing, Nothing)
End If

Logger.Write("Attempting to add signature to : " &
drContracts("DocLocation"))
sig = wrdApp.ActiveDocument.Signatures.Add
sig.AttachCertificate = True
wrdApp.ActiveDocument.Signatures.Commit()
Logger.Write("Signature added to : " & impDealRef)

'
'''

wrdApp.ActiveDocument.Close()

End Sub

Public Delegate Sub WordSignatureDelegate(ByVal blnFirstRun As Boolean)

Public Sub WordSignature(ByVal blnFirstRun As Boolean)

Dim intHandle As Int32

Sleep(5000) 'wait for Signatures.Add to run

intHandle = FindWindow("#32770", "select certificate")
rc = PostMessage(intHandle, WM_KEYDOWN, 13, 0)
rc = PostMessage(intHandle, WM_KEYUP, 13, 0)

'on the first run, we need to hit enter twice
If blnFirstRun Then
Sleep(5000) 'wait for next dialog to show

intHandle = FindWindow("#32770", "Signing data with your private
exchange key")
rc = PostMessage(intHandle, WM_KEYDOWN, 13, 0)
rc = PostMessage(intHandle, WM_KEYUP, 13, 0)
End If
End Sub
 

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