|
主窗体上有个客户组合框,绑定列是客户ID,显示列是客户名称,子窗体明细表中有一个产品字段组合框,绑定列是产品ID,显示列是产品名称,我现在要依据所显示的主窗体的字段记录和子窗体各个字段记录,通过点击打印按钮传递到word模板已设定的相应字段上。问题是:
客户字段和产品字段从窗体上传递到word客户字段及产品字段上的是绑定列的值(如1或2),这不是我需要的,我需要的是显示的值(如三通公司/AA沙发)。我不知道在代码中如何修改,请帮助!
Private Sub 打印_Click()
DoCmd.RunCommand acCmdSaveRecord '打印前先保存记录
Const maxRecordsInOnepage As Integer = 7 '发票最多可以打印的明细条数
Dim currentDetailsRecords As Integer
Dim totalPages As Integer
Dim I, N As Integer
'使用DAO操作打开明细记录集
Dim rstDetails As DAO.Recordset
Set rstDetails = CurrentDb.OpenRecordset("SELECT 货号, 产品ID, 单位, 数量, 单价, 金额 FROM 送货明细表1 WHERE 送货单号='" & Me.送货单号 & "'")
'如果没有记录 , 不执行下面程序
If rstDetails.EOF Then Exit Sub
'为了能得到记录总数量,DAO记录集要先把记录集位置移到最后,否则得不到RECORDCOUNT
rstDetails.MoveLast
rstDetails.MoveFirst
currentDetailsRecords = rstDetails.RecordCount
If currentDetailsRecords <= maxRecordsInOnepage Then '一页
totalPages = 1
Else
If currentDetailsRecords Mod maxRecordsInOnepage = 0 Then '除得尽,
totalPages = currentDetailsRecords / maxRecordsInOnepage
Else '除不尽
totalPages = Int(currentDetailsRecords / maxRecordsInOnepage) + 1
End If
End If
On Error Resume Next
Dim docApp As Object
Set docApp = GetObject(, "Word.Application") '得到当前的WORD程序对象,如果WORD已经运行
If Err Then '出错说明WORD没有运行
Err.Clear
Set docApp = CreateObject("Word.Application") '创建WORD对象
If Err Then
MsgBox "操作中没有装WORD程序!", vbQuestion, "发票套打程序"
Exit Sub
End If
End If
docApp.Visible = False '隐藏WORD程序,防止在执行程序时用户误操作
Dim doc As Object
Dim win As Object
For I = 1 To totalPages '多页的处理,使用循环
Set doc = docApp.Documents.Add(CurrentProject.Path & "\发票模板.doc") '使用定义好的模板创建新文件
Set win = doc.ActiveWindow
With win.Selection.Find '使用查找和替换
.Text = "[年]"
.Replacement.Text = Nz(Format(Me.开单日期, "yyyy"), "")
.Execute , , , , , , , , , , 2 '全部替换
.Text = "[月]"
.Replacement.Text = Nz(Format(Me.开单日期, "mm"), "")
.Execute , , , , , , , , , , 2
.Text = "[日]"
.Replacement.Text = Nz(Format(Me.开单日期, "dd"), "")
.Execute , , , , , , , , , , 2
.Text = "[客户ID]"
.Replacement.Text = Nz(Me.客户ID, "")
.Execute , , , , , , , , , , 2
.Text = "[送货单位盖章]"
.Replacement.Text = Nz(Me.送货单位盖章, "")
.Execute , , , , , , , , , , 2
.Text = "[客户地址]"
.Replacement.Text = Nz(Me.客户地址, "")
.Execute , , , , , , , , , , 2
For N = 1 To 7 '8条明细记录处理
.Text = "[" & "货号" & N & "]"
.Replacement.Text = Nz(IIf(rstDetails.EOF, "", rstDetails!货号), "") '如果已经没有记录,也要把WORD中的定义字段改成空字符串
.Execute , , , , , , , , , , 2 '全部查找和替换
.Text = "[" & "产品ID" & N & "]"
.Replacement.Text = Nz(IIf(rstDetails.EOF, "", rstDetails!产品ID), "")
.Execute , , , , , , , , , , 2
.Text = "[" & "单位" & N & "]"
.Replacement.Text = Nz(IIf(rstDetails.EOF, "", rstDetails!单位), "")
.Execute , , , , , , , , , , 2
.Text = "[" & "数量" & N & "]"
.Replacement.Text = Nz(IIf(rstDetails.EOF, "", rstDetails!数量), "")
.Execute , , , , , , , , , , 2
.Text = "[" & "单价" & N & "]"
.Replacement.Text = Nz(IIf(rstDetails.EOF, "", Format(rstDetails!单价, "0.00")), "")
.Execute , , , , , , , , , , 2
.Text = "[" & "金额" & N & "]"
.Replacement.Text = Nz(IIf(rstDetails.EOF, "", Format(rstDetails!金额, "0.00")), "")
.Execute , , , , , , , , , , 2
If Not rstDetails.EOF Then rstDetails.MoveNext
Next
.Text = "[人民币合计(大写)]"
.Replacement.Text = Nz(Me.送货明细表1.Form!txtSum, "")
.Execute , , , , , , , , , , 2
.Text = "[件数合计]"
.Replacement.Text = Nz(Me.送货明细表1.Form!件数合计, "")
.Execute , , , , , , , , , , 2
.Text = "[客户ID]"
.Replacement.Text = Nz(Me.客户ID, "")
.Execute , , , , , , , , , , 2
.Text = "[客户地址]"
.Replacement.Text = Nz(Me.客户地址, "")
.Execute , , , , , , , , , , 2
End With
doc.PrintPreview '打印预览
'doc.PrintOut '直接打印
Next
docApp.Visible = True '自动化完成后,显示WORD程序
End Sub |
|