【问题标题】:How to open PDF stored in sql server [image] field using ms access 2007如何使用 ms access 2007 打开存储在 sql server [image] 字段中的 PDF
【发布时间】:2011-11-11 05:43:39
【问题描述】:

我正在将数据库从访问转换为 sql 后端访问前端。该数据库嵌入了 pdf 文档,这些文档最终被 SQL Server 的数据导入工具存储为 [image] 数据。

我的问题是我希望用户能够通过单击在 access 中创建的报告中的 pdf 图标来打开 pdf 文件。

这可以用 VBA 完成还是有更简单的方法?我完全不知道如何做到这一点。

感谢您的回答!

我编辑了 BlobToFile 函数以去除 ole 标头,因为 adobe 无法读取文件(evince 可以,mac 预览也可以)

我能够像这样做我想做的事:

 Private Sub PDFDocument_Click()
     Call BlobToFile("C:\db\MyPDFFile.pdf", Me.PDFDocument)
     If Dir("C:\db\MyPDFFile.pdf") <> "" Then
        FollowHyperlink ("C:\db\MyPDFFile.pdf")
     End If
 End Sub

 'Function:  BlobToFile - Extracts the data in a binary field to a disk file.
 'Parameter: strFile - Full path and filename of the destination file.
 'Parameter: Field - The field containing the blob.
 'Return:    The length of the data extracted.
  Public Function BlobToFile(strFile As String, ByRef Field As Object) As Long
     On Error GoTo BlobToFileError

     Dim nFileNum As Integer
     Dim abytData() As Byte
     Dim abytParsedData() As Byte
     Dim copyOn As Boolean
     Dim copyIndex As Long

     BlobToFile = 0
     nFileNum = FreeFile
     copyOn = False
     copyIndex = 0

     Open strFile For Binary Access Write As nFileNum
     abytData = Field
     ReDim abytParsedData(UBound(abytData))

     For i = LBound(abytData) To UBound(abytData) - 1
         If copyOn = False Then
             If Chr(abytData(i)) = "%" And Chr(abytData(i + 1)) = "P" And Chr(abytData(i + 2)) = "D" And Chr(abytData(i + 3)) = "F" Then
                 copyOn = True
             End If
         End If

         If copyOn = True Then
             abytParsedData(copyIndex) = abytData(i)
             copyIndex = copyIndex + 1
         End If
     Next

     Put #nFileNum, , abytParsedData

     BlobToFile = LOF(nFileNum)

     BlobToFileExit:
     If nFileNum > 0 Then Close nFileNum
          Exit Function

     BlobToFileError:
     MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, _
            "Error writing file in BlobToFile"
     BlobToFile = 0
     Resume BlobToFileExit

 End Function

【问题讨论】:

标签: sql-server ms-access vba


【解决方案1】:

如果我了解您要做什么,您基本上希望 Adob​​e Reader 打开内存中的 pdf 文件“对象”。这是不可能的。您需要将 pdf 文件写入系统硬盘驱动器,然后从那里打开它。您可以通过使用计算机临时文件夹或自己管理文件/文件夹来实现您的要求。例如,您可以在每次打开应用程序时清理您的 PDF 文件夹。

这里有一些代码可以帮助你做你想做的事情。此代码不处理与创建文件夹、生成文件名、检查文件是否已存在等有关的任何事情。我假设您将能够处理这些。我在 Command1_Click 中的代码假定您将 SQL Server 与 ODBC 链接表一起使用。

我在这里使用 FollowHyperlink,但我强烈建议您使用 Allen Browne's GoHyperlink function 打开文件。 FollowHyperlink 可能会出现安全错误。

Private Sub Command1_Click()
    Dim r As DAO.Recordset, sSQL As String
    sSQL = "SELECT ID, BlobField FROM MyTable"
    Set r = CurrentDb.OpenRecordset(sSQL, dbOpenDynaset, dbSeeChanges)
    If Not (r.EOF And r.BOF) Then
        Call BlobToFile("C:\MyPDFFile.pdf", r("BlobField"))
        If Dir("C:\MyPDFFile.pdf") <> "" Then
            FollowHyperlink("C:\MyPDFFile.pdf")
        End If
    End If
    r.Close
    Set r = Nothing
End Sub


'Function:  BlobToFile - Extracts the data in a binary field to a disk file.
'Parameter: strFile - Full path and filename of the destination file.
'Parameter: Field - The field containing the blob.
'Return:    The length of the data extracted.
Public Function BlobToFile(strFile As String, ByRef Field As Object) As Long
    On Error GoTo BlobToFileError

    Dim nFileNum As Integer
    Dim abytData() As Byte
    BlobToFile = 0
    nFileNum = FreeFile
    Open strFile For Binary Access Write As nFileNum
    abytData = Field
    Put #nFileNum, , abytData
    BlobToFile = LOF(nFileNum)

BlobToFileExit:
    If nFileNum > 0 Then Close nFileNum
    Exit Function

BlobToFileError:
    MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, _
           "Error writing file in BlobToFile"
    BlobToFile = 0
    Resume BlobToFileExit

End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-18
    • 1970-01-01
    • 2012-09-06
    • 1970-01-01
    相关资源
    最近更新 更多