怎么用VB将连接在access中的数据输出到word中呢?

2025-01-04 07:17:39
推荐回答(2个)
回答1:

我以前写过一个写到excel中的。对你可能有帮助。
Private Sub Command2_Click()
p = InputBox("请输入文件名", "文件名.xls")
Dim i As Integer
Dim j As Integer
Dim xl As Excel.Application
Dim xlbook As Excel.Workbook
Dim xlsheet As Excel.Worksheet

Set xl = New Excel.Application
Set xlbook = xl.Workbooks.Add
Set xlsheet = xlbook.Worksheets("sheet1")

Select Case Combo1.Text
Case "查询入库单"
If Combo2.Text = "显示器" Then
xl.Range("C3").Value = "ID"
xl.Range("D3").Value = "商品编号"
xl.Range("E3").Value = "商品名称"
xl.Range("F3").Value = "供应商编号"
xl.Range("G3").Value = "供应商名称"
xl.Range("H3").Value = "商品规格"
xl.Range("I3").Value = "单位"
xl.Range("J3").Value = "数量"
xl.Range("K3").Value = "单价"
xl.Range("L3").Value = "金额"
xl.Range("M3").Value = "入库时间"
xl.Range("N3").Value = "经手人"
xl.Range("O3").Value = "备注"
ElseIf Combo2.Text = "主机散件" Then
xl.Range("C3").Value = "ID"
xl.Range("D3").Value = "商品编号"
xl.Range("E3").Value = "商品名称"
xl.Range("F3").Value = "商品类型"
xl.Range("G3").Value = "供应商名称"
xl.Range("H3").Value = "供应商编号"
xl.Range("I3").Value = "商品规格"
xl.Range("J3").Value = "单位"
xl.Range("K3").Value = "数量"
xl.Range("L3").Value = "单价"
xl.Range("M3").Value = "金额"
xl.Range("N3").Value = "入库时间"
xl.Range("O3").Value = "经手人"
xl.Range("P3").Value = "备注"
ElseIf Combo2.Text = "打印机、复印机" Then
xl.Range("C3").Value = "ID"
xl.Range("D3").Value = "商品编号"
xl.Range("E3").Value = "商品名称"
xl.Range("F3").Value = "商品类型"
xl.Range("G3").Value = "供应商名称"
xl.Range("H3").Value = "供应商编号"
xl.Range("I3").Value = "商品规格"
xl.Range("J3").Value = "单位"
xl.Range("K3").Value = "数量"
xl.Range("L3").Value = "单价"
xl.Range("M3").Value = "金额"
xl.Range("N3").Value = "入库时间"
xl.Range("O3").Value = "经手人"
xl.Range("P3").Value = "备注"
ElseIf Combo2.Text = "路由器、交换机" Then
xl.Range("C3").Value = "ID"
xl.Range("D3").Value = "商品编号"
xl.Range("E3").Value = "商品名称"
xl.Range("F3").Value = "商品类型"
xl.Range("G3").Value = "供应商名称"
xl.Range("H3").Value = "供应商编号"
xl.Range("I3").Value = "商品规格"
xl.Range("J3").Value = "单位"
xl.Range("K3").Value = "数量"
xl.Range("L3").Value = "单价"
xl.Range("M3").Value = "金额"
xl.Range("N3").Value = "入库时间"
xl.Range("O3").Value = "经手人"
xl.Range("P3").Value = "备注"
ElseIf Combo2.Text = "其他设备" Then
xl.Range("C3").Value = "ID"
xl.Range("D3").Value = "商品编号"
xl.Range("E3").Value = "商品名称"
xl.Range("F3").Value = "供应商编号"
xl.Range("G3").Value = "供应商名称"
xl.Range("H3").Value = "商品类型"
xl.Range("I3").Value = "商品规格"
xl.Range("J3").Value = "单位"
xl.Range("K3").Value = "数量"
xl.Range("L3").Value = "单价"
xl.Range("M3").Value = "金额"
xl.Range("N3").Value = "入库时间"
xl.Range("O3").Value = "经手人"
xl.Range("P3").Value = "备注"
End If

Case "查询出库单"
If Combo2.Text = "显示器" Then
xl.Range("C3").Value = "ID"
xl.Range("D3").Value = "商品编号"
xl.Range("E3").Value = "商品名称"
xl.Range("F3").Value = "客户名称"
xl.Range("G3").Value = "商品规格"
xl.Range("H3").Value = "单位"
xl.Range("I3").Value = "数量"
xl.Range("J3").Value = "单价"
xl.Range("K3").Value = "金额"
xl.Range("L3").Value = "出库时间"
xl.Range("M3").Value = "经手人"
xl.Range("N3").Value = "备注"
ElseIf Combo2.Text = "主机散件" Then
xl.Range("C3").Value = "ID"
xl.Range("D3").Value = "商品编号"
xl.Range("E3").Value = "商品名称"
xl.Range("F3").Value = "客户名称"
xl.Range("G3").Value = "商品类型"
xl.Range("H3").Value = "商品规格"
xl.Range("I3").Value = "单位"
xl.Range("J3").Value = "数量"
xl.Range("K3").Value = "单价"
xl.Range("L3").Value = "金额"
xl.Range("M3").Value = "出库时间"
xl.Range("N3").Value = "经手人"
xl.Range("O3").Value = "备注"
ElseIf Combo2.Text = "打印机、复印机" Then
xl.Range("C3").Value = "ID"
xl.Range("D3").Value = "商品编号"
xl.Range("E3").Value = "商品名称"
xl.Range("F3").Value = "客户名称"
xl.Range("G3").Value = "商品类型"
xl.Range("H3").Value = "商品规格"
xl.Range("I3").Value = "单位"
xl.Range("J3").Value = "数量"
xl.Range("K3").Value = "单价"
xl.Range("L3").Value = "金额"
xl.Range("M3").Value = "出库时间"
xl.Range("N3").Value = "经手人"
xl.Range("O3").Value = "备注"
ElseIf Combo2.Text = "路由器、交换机" Then
xl.Range("C3").Value = "ID"
xl.Range("D3").Value = "商品编号"
xl.Range("E3").Value = "商品名称"
xl.Range("F3").Value = "客户名称"
xl.Range("G3").Value = "商品类型"
xl.Range("H3").Value = "商品规格"
xl.Range("I3").Value = "单位"
xl.Range("J3").Value = "数量"
xl.Range("K3").Value = "单价"
xl.Range("L3").Value = "金额"
xl.Range("M3").Value = "出库时间"
xl.Range("N3").Value = "经手人"
xl.Range("O3").Value = "备注"
ElseIf Combo2.Text = "其他设备" Then
xl.Range("C3").Value = "ID"
xl.Range("D3").Value = "商品编号"
xl.Range("E3").Value = "商品名称"
xl.Range("F3").Value = "客户名称"
xl.Range("G3").Value = "商品类型"
xl.Range("H3").Value = "商品规格"
xl.Range("I3").Value = "单位"
xl.Range("J3").Value = "数量"
xl.Range("K3").Value = "单价"
xl.Range("L3").Value = "金额"
xl.Range("M3").Value = "出库时间"
xl.Range("N3").Value = "经手人"
xl.Range("O3").Value = "备注"
End If

Case "查询报损单"
xl.Range("C3").Value = "ID"
xl.Range("D3").Value = "货品名称"
xl.Range("E3").Value = "货品类型"
xl.Range("F3").Value = "货品规格"
xl.Range("G3").Value = "损坏数量"
xl.Range("H3").Value = "添加人"
xl.Range("I3").Value = "添加时间"
xl.Range("J3").Value = "备注"
xl.Range("K3").Value = ""
xl.Range("L3").Value = ""
xl.Range("M3").Value = ""
xl.Range("N3").Value = ""
xl.Range("O3").Value = ""
End Select

For i = 1 To Adodc1.Recordset.RecordCount
j = 3 + i
For k = 0 To 14
w = Chr(99 + k) & j
xl.Range(w).Value = DataGrid1.Columns(k)
Next k
If Adodc1.Recordset.EOF = False Then
Adodc1.Recordset.MoveNext
End If
Next i

xlbook.SaveAs App.Path & "\EXCEL表\" & p
xl.quit
MsgBox "数据导出完成", 64

End Sub

回答2:

形成文本文件, 再用word打开就行了.
Dim aDB As New ADODB.Connection
Dim aF As ADODB.Field
Dim aR As New ADODB.Recordset

aDB.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\table1.mdb;Persist Security Info=False"

Open "c:\123.txt" For Output As #1

aR.Open "学生", aDB, adOpenStatic, adLockReadOnly

Do
For Each aF In aR.Fields
Write #1, aF.Value
Next
aR.MoveNext
Loop Until aR.EOF

Close #1

aR.Close
aDB.Close
Set aR = Nothing
Set aDB = Nothing