|
2#
楼主 |
发表于 2009-12-21 09:16:40
|
只看该作者
本帖最后由 yxf614 于 2009-12-21 16:12 编辑
这贴不能沉哟.,谢谢大家来看下.
我的代码如下:
Private Sub 打印_Click()
DoCmd.RunCommand acCmdSaveRecord '打印前先保存记录
Const maxRecordsInOnepage As Integer = 8
Dim currentDetailsRecords As Integer
Dim totalPages As Integer
Dim I, N As Integer
'使用DAO操作打开明细记录集
Dim rstDetails As DAO.Recordset
Set rstDetails = CurrentDb.OpenRecordset("SELECT 买方姓名,经办人, 联系电话,备注 FROM 合同签约 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(Me.业主姓名, "")
.Execute , , , , , , , , , , 2
End With
doc.PrintPreview '打印预览
'doc.PrintOut '直接打印
Next
docApp.Visible = True '自动化完成后,显示WORD程序
End Sub |
|