正文

打开的一篇word文档,保存到数据库的某个字段中2005-09-25 08:06:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/iamben250/5276.html

分享到:

打开的一篇word文档,保存到数据库的某个字段中 编号:QA004640建立日期: 2002年11月25日 最后修改日期:2002年11月25日 所属类别: Visual Basic - Office开发Visual Basic - 数据库 于丽霞:     操作系统:Win     编程工具:VB     问题:我想把打开的一篇word文档,保存到数据库的某个字段中,刚开始用了宏的办法。先写一下宏,代码如下:     Sub Mac()     Dim PageNo As String     Dim WORDstr As String     Dim i As Integer          'WORD 97 文 档 视 图 设 定 为 页 面 方 式     ActiveWindow.View.Type = wdPageView     ActiveDocument.Repaginate     '获 得 文 档 页 数 并 赋 值 给 变 量 PageNo     PageNo = ActiveDocument.BuiltInDocumentProperties(wdPropertyPages)     For i = 1 To PageNo     ' 光 标 移 动 到 文 档 某一 页 的 开 始     Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=i     ' 全 选 文 档某一 页 的 所 有 内 容     Selection.EndKey Unit:=wdStory, Extend:=wdExtend     WORDstr = WORDstr & Selection.Text     Next     End Sub          在vb中调用,写在button_click事件中,代码如下:     Private Sub Command2_Click()     Dim wrdApp As Object     Dim wrdDoc As Object     Dim strFileName As String          On Error GoTo DocError          Set wrdApp = CreateObject("Word.Application")     strFileName = App.Path & "\normal.wz"     Set wrdDoc = wrdApp.Documents.Open(strFileName)     wrdApp.Run "mac"          Exit Sub          DocError:     If Err.Number <> 0 Then MsgBox Err.Description          wrdApp.Quit          Set wrdApp = Nothing     Set wrdDoc = Nothing     End Sub          这种方法,我不知道在执行run时,该怎样把执行的结果,放在vb的变量中,再就是它不能保存格式,这很不好。你们还有什么其它的办法吧,请告诉我好吧? 回答:     stone的意见:     最好把整个Word文档保存到数据库,这样就不会丢失Wrod中的格式及所有其它的一些内容。     完整的代码如下:如果是用SQL那么保存文件的字段类型应该是Binary。如果是用Access,那么保存文件的字段应该用OLD对象,在表中显示为长二进制数据。          '将任何文件从数据库中下载到本地:     Public Function LoadFile(ByVal col As ADODB.Field, ByVal FileName As String) As Boolean '获得binary数据     On Error GoTo myerr:      Dim arrBytes() As Byte      Dim FreeFileNumber As Integer      lngsize = col.ActualSize      arrBytes = col.GetChunk(lngsize)      FreeFileNumber = FreeFile      Open FileName For Binary Access Write As #FreeFileNumber      Put #FreeFileNumber, , arrBytes      Close #FreeFileNumber      LoadFile = True     myerr:      If Err.Number <> 0 Then      LoadFile = False      Err.Clear      End If     End Function          '将文件从本地上传到数据库中     Public Function UpLoadFile(ByVal FileName, ByVal col As ADODB.Field) As Boolean      On Error GoTo myerr:      Dim arrBytes() As Byte      Dim FreeFileNumber As Integer      FreeFileNumber = FreeFile      Open FileName For Binary As #FreeFileNumber      n = LOF(FreeFileNumber)      ReDim arrBytes(1 To n) As Byte      Get #FreeFileNumber, , arrBytes      Close #FreeFileNumber      col.AppendChunk (arrBytes)      UpLoadFile = True     myerr:      If Err.Number <> 0 Then      UpLoadFile = False      Err.Clear      End If     End Function               szh的意见:     数据库字段:wjmc 文件名,wjsx 文件的扩展名。Wjnr 文件的内容为二进制。(若access数据库为“ole对象”,sql server为“image”)          该程序可以操作所有的文件类型。     Dim Wenjian As String          Dim RD As Byte          Dim SIZE As Long          Const MYSIZE = 1048576          Dim WENJIANN() As Byte           Dim Rs As New ADODB.Recordset           Rs.Open "select * from wj", Cn, 1, 3           Rs.AddNew           Rs!wjmc = Mid(Name, 1, InStr(Name, ".") - 1)           Rs!wjsx = Mid(Name, InStr(Name, ".") + 1)           ‘name为文件的名称加扩展名           Open Filename For Binary Access Read As #1           SIZE = LOF(1)           Do While SIZE - MYSIZE >= 0           ReDim WENJIANN(MYSIZE) As Byte           Get #1, , WENJIANN           Rs!wjnr.AppendChunk WENJIANN           SIZE = SIZE - MYSIZE           Loop           If SIZE > 0 Then           ReDim WENJIANN(SIZE) As Byte           Get #1, , WENJIANN           Rs!wjnr.AppendChunk WENJIANN           End If           Close #1           Rs.Update           Set Rs = Nothing               ldx的意见:     其实用不着这么复杂。先将你的Word文档转换成RTF文档。再用两个语句方法既可:一个是RichTextBox1.LoadFile strOpen, 0,另一个是rsRecodset("字段名").AppendChunk RichTextBox1.TextRTF。其中strOpen是你要打开的RTF文档。     前一个语句将你的RTF文档显示到RichTextBox控件中,后一语句将RichTextBox控件中的格式化文档灌入数据库字段。这是两个核心语句,其余配合的语句我想你应该知道。如果你不想用第二个语句,那么再画一个Data数据控件则更简单。

阅读(5259) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册