【示例】导出多表格WORD文档
时 间:2010-10-10 10:15:26
作 者:林岚 ID:3651 城市:中卫
摘 要:1、包含导出两个表格到WORD.
2、包含在查询中产生自动增加序号的函数。
正 文:
感谢网站,感谢各位老师。汇集大家的智慧,完成自己的设想,能拿得出手就绝不吝啬。
Function Zsb(模板名, 文件名, 记录集, 记录集2, 起始行, 表号, Optional 条件 As String)
Dim doc As New Word.Document ' 定义引用 Microsoft Word 的变量。
Dim BTable As Word.Table
Dim dbs As Database '定义引用数据库的变量。
Dim rst As DAO.Recordset '定义引用记录集的变量。
Dim i, j, P, m, n As Integer
Dim s As String
'On Error GoTo err1
'使用DAO操作打开明细记录集
Set dbs = CurrentDb()
If Nz(条件) <> "" Then 记录集 = "select * from " & 记录集 & " where " & 条件
Set rst = dbs.OpenRecordset(记录集) '设置记录集
If InStr(1, UCase(模板名), ".DOC") > 0 Then
WJ1 = CurrentProject.Path & "\" & 模板名
'模板文件名(CurrentProject.Path为当前数据库的路径)
Else
WJ1 = CurrentProject.Path & "\" & 模板名 & ".DOC"
'模板文件名(CurrentProject.Path为当前数据库的路径)
End If
If InStr(1, UCase(文件名), ".DOC") > 0 Then
WJ2 = CurrentProject.Path & "\" & 文件名 '目标文件名
Else
WJ2 = CurrentProject.Path & "\" & 文件名 & ".DOC" '目标文件名
End If
FileCopy WJ1, WJ2 '拷贝文件(模板文件拷贝成目标文件)
Set doc = GetObject(WJ2, "Word.Document") '建立与Word的连接变量
doc.Application.Visible = True '打开属性为真
doc.Activate
Set BTable = doc.Application.ActiveDocument.Tables(表号)
Set rst = dbs.OpenRecordset(记录集) '设置记录集
If Not rst.EOF Then rst.MoveFirst
i = 起始行
While Not rst.EOF
Set rowNew = BTable.Rows.Add() '加入一行
j = 0
For Each HCell In BTable.Rows(i).Cells
HCell.Range.InsertAfter Nz(rst.Fields(j))
j = j + 1
Next HCell
rst.MoveNext
i = i + 1
Wend
Set rst = dbs.OpenRecordset(记录集2) '设置记录集
If Not rst.EOF Then rst.MoveFirst
i = 起始行
Set BTable = doc.Application.ActiveDocument.Tables(表号 + 1)
While Not rst.EOF
Set rowNew = BTable.Rows.Add() '加入一行
j = 0
For Each HCell In BTable.Rows(i).Cells
HCell.Range.InsertAfter Nz(rst.Fields(j))
j = j + 1
Next HCell
rst.MoveNext
i = i + 1
Wend
doc.Save '保存Word
'doc.Application.Quit '关闭Word
Set doc = Nothing '清除内存变量
Set BTable = Nothing
Set dbs = Nothing
Set rst = Nothing
Zsb = True
Exit Function
err1:
doc.Application.Quit
Set doc = Nothing '清除内存变量
Set BTable = Nothing
Set dbs = Nothing
Set rst = Nothing
Zsb = False
MsgBox ("出现错误,可能是Word已打开,请关闭Word后再试")
End Function
'查询中生成自动排序的字段
Function SerializeAdo(qryname As String, KeyName As String, KeyValue, Optional orderneme As String = "", Optional Condition As String = "") As Long
' On Error GoTo Err_Serialize
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Dim px, tj
If Condition <> "" Then
tj = "select * from " & qryname & " where " & Condition
Else
tj = "select * from " & qryname
End If
If orderneme <> "" Then
px = tj & " orDER BY " & orderneme
Else
px = tj
End If
rs.ActiveConnection = CurrentProject.Connection
rs.Open px, , adOpenStatic, adLockReadOnly, adCmdTableDirect
'Find the current record with key value
Select Case rs.Fields(KeyName).Type
' Find data type key value
Case DB_INTEGER, DB_LONG, DB_CURRENCY, DB_SINGLE, DB_DOUBLE, DB_BYTE
rs.Find "[" & KeyName & "] = " & KeyValue
' Find date type key value
Case DB_DATE
rs.Find "[" & KeyName & "] = #" & KeyValue & "#"
' Find text type key value
Case DB_TEXT
rs.Find "[" & KeyName & "] = '" & KeyValue & "'"
Case Else
MsgBox "Errer ; Invalid KeyName or KeyValue "
End Select
SerializeAdo = Nz(rs.AbsolutePosition, 0)
rs.Close
Set rs = Nothing
Exit Function
Err_Serialize:
MsgBox "QueryName ", 64, " is wrong "
Exit Function
End Function
Access软件网官方交流QQ群 (群号:54525238) Access源码网店
常见问答:
技术分类:
源码示例
- 【源码QQ群号19834647...(12.17)
- 【Access选项卡示例】Ac...(09.09)
- 【Access源码示例】按输入...(09.02)
- 【Access日期区间段查询】...(08.29)
- 【Access日期区间段查询】...(08.27)
- Access怎样才能实现日期时...(08.21)
- 【Access定时打开查询】A...(08.19)
- Access生成固定数量的记录...(08.13)
- Access怎样才能实现日期时...(08.12)
- Access利用导航窗体控件对...(08.03)
学习心得
最新文章
- Access表中的字段名、字段标题...(09.19)
- Access快速开发平台--更改“...(09.18)
- 【中秋及国庆优惠】Access培训...(09.15)
- Access如何将日期型的数值转换...(09.14)
- 英文输入法输入数据中存在单引号引起...(09.11)
- 【Access选项卡示例】Acce...(09.09)
- 让Access光标停留在指定的控件...(09.07)
- 关于Access查询条件里使用通配...(09.06)
- Access报表偷懒制作法--Ac...(09.05)
- Access快速开发平台--窗体数...(09.04)