1

我只是在学习使用 CoSign SAPI。我下载了 Visual Studio 的示例 VB.Net 程序代码,它运行正常。然后我尝试在 Microsoft Word 2010 中将代码作为 VBA 模块运行。代码在 SAPI.logon 语句之前执行正常并失败。下面是我正在使用的代码。另外,您知道在 Word VBA 中使用 SAPICom 的任何其他示例程序吗?非常感谢您的帮助。

公共子 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

结束子

4

2 回答 2

1

代码现在正在工作。问题是由于用户帐户无效;我没有意识到用户帐户区分大小写。正确输入用户帐户名称后,代码现在可以工作了。

我还将用于实例化 SAPI 对象的代码更改为以下代码,但原始代码没有引起问题。

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

谢谢你的帮助。

于 2014-04-18T14:58:17.197 回答
0

我认为您不能从 VBA 调用 SAPI COM(基本上是 Word 中的脚本)。您可以尝试将 VB.Net 作为独立应用程序运行吗?

于 2014-04-17T20:35:15.883 回答