1

I am just learning to use CoSign SAPI. I down loaded the example VB.Net program code for Visual Studio and it runs ok. I then tried to run code in Microsoft Word 2010 as a VBA module. The code executes ok up to the SAPI.logon statement and fails. Below is the code I am using. Also, do you know of any other example programs using SAPICom in Word VBA? Thanks very much for your help.

Public Sub testCoSign()

    Const SAPI_OK As Integer = 0

    Dim rc As Integer
    Dim SAPI As New SAPICrypt
    Dim sesMyHandle As New sesHandle

    'Custom Values
    Dim filePath As String   'PDF file to sign
    Dim username As String   'CoSign account username
    Dim password As String   'CoSign account password
    Dim domain As String     'CoSign account domain
    Dim sigPageNum As Integer  'Create signature on the first page
    Dim sigX As Integer        'Signature field X location
    Dim sigY As Integer        'Signature field Y location
    Dim sigWidth As Integer    'Signature field width
    Dim sigHeight As Integer   'Signature field height
    Dim timeFormat As String   'Time appearance format mask
    Dim dateFormat As String   'Date appearance format mask
    Dim appearanceMask As Integer 'Elements to display on the signature field

    'Initialize variables
    Set sesMyHandle = Nothing
    filePath = "c:\\temp\\demo.doc"
    username = "JSMITH"
    password = "*******"
    sigPageNum = 1
    sigX = 145
    sigY = 125
    sigWidth = 160
    sigHeight = 45
    timeFormat = "hh:mm:ss"
    dateFormat = "dd/MM/yyyy"
    appearanceMask = SAPI_ENUM_DRAWING_ELEMENT.SAPI_ENUM_DRAWING_ELEMENT_GRAPHICAL_IMAGE Or _
                     SAPI_ENUM_DRAWING_ELEMENT.SAPI_ENUM_DRAWING_ELEMENT_SIGNED_BY Or _
                     SAPI_ENUM_DRAWING_ELEMENT.SAPI_ENUM_DRAWING_ELEMENT_TIME


' Initialize SAPI library
rc = SAPI.Init
If rc <> SAPI_OK Then
    MsgBox "error initializing SAPI", vbOKOnly, "Error"
End If

' Acquire SAPI session handle
rc = SAPI.HandleAcquire(sesMyHandle)
If rc <> SAPI_OK Then
    MsgBox "error acquiring SAPI session handle", vbOKOnly, "Error"
End If

' Personalize SAPI Session
rc = SAPI.Logon(sesMyHandle, username, "", password)
If rc <> SAPI_OK Then
    MsgBox "failure to authenticate user", vbOKOnly, "Error"
End If

End Sub

2 Answers2

1

The code is woking now. The problem was due to invalid user account; I didn't realize the user account is case sensitive. After entering the user account name correctly the code is now working.

I also changed the code for instantiating the SAPI objects to the following, but the original code was not causing a problem.

    Dim SAPI As SAPICrypt
    Set SAPI = New SAPICrypt
    Dim sesMyHandle As sesHandle
    Set sesMyHandle = Nothing

Thanks for your help.

0

I don’t think you can call SAPI COM from VBA (basically a script from within Word). Can you try running your VB.Net as an independent application?

Sergey Kolodiy
  • 5,829
  • 1
  • 36
  • 58