设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 12812|回复: 15
打印 上一主题 下一主题

[A高7]如何使用VBA代码将插入OLE字段中的对象还原成一个文件

[复制链接]

点击这里给我发消息

1#
发表于 2008-4-23 18:36:57 | 显示全部楼层
整理一下,未测试

MS Access OLE Object to Disk File Export

--------------------------------------------------------------------------------

'Written : 9 Jun 2004
'Modified : 27 Jul 2004
'Author : Sonam Gurung 2004
'Module to export bmp file data from OLE Field to disk file
'===========================

Option Compare Database
Option Explicit

'Constants That User Can Modify
Const table = "table1"
Const unid = "id"
Const field = "data"
Const dirpath = "c:\"
'End of Constant Declaration

Sub SaveOLEToFile()
        Dim db As Database, rs As DAO.Recordset, q As String, fname As String, b() As Byte
        Set db = CurrentDb()
        q = "select " & unid & "," & field & " from " & table
        Set rs = db.OpenRecordset(q)
        While Not rs.EOF
                fname = dirpath & "\" & Trim(Str(rs.Fields(0).Value)) & ".tif"
                ReDim b(rs.Fields(1).Size)
                b = rs.Fields(1).Value
                Open fname For Binary Access Write As #1
                Put #1, , b
                Close #1
                TrimFile (fname)
                rs.MoveNext
        Wend
        rs.Close
        Set rs = Nothing
End Sub

Sub TrimFile(ByVal fname As String)
        Dim b() As Byte, l As Long
        ReDim b(4)
        Open fname For Binary As #1
        For l = 1 To LOF(1)
                Seek #1, l
                Get #1, , b
                If b(0) = asc("B") And b(1) = asc("M") And b(2) = asc("B") and b(3)="K" Then Seek #1, l: Exit For
        Next l
        ReDim b(LOF(1) - l)
        Get #1, , b
        Close #1

        Open fname For Binary As #1
        Put #1, , b
        Close #1
End Sub
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-5 00:35 , Processed in 0.093037 second(s), 23 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表