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