Navyguy
Registered User.
- Local time
- Today, 16:56
- Joined
- Jan 21, 2004
- Messages
- 194
Hi to everyone…again
I have been trying to learn how to print a merged word document from a button on a form. Have read the MS knowledge base site and numerous posts and I thought I had come up with a winner…but I was wrong, so looking for your tutelage once again.
Name of DB: Test.mbd
Form: FrmSampleNotice
Fields on Form: Surname
Location
Report Number
Button Name:BtnSampleNotice
Query: QrySampleNotice
Word Document Path: C:\My Documents\Learning Access\Merge Sample Notice.doc
I know the Query works fine and I know that when I run the merge from the .doc it works ok also, but now I am trying to get to do it from the button on the form. I also know that the event procedure is linked to the OnClick properties. I do not get any error messages or VB errors either so not sure where else to look.
Here is the code that I am using:
Private Sub BtnSampleNotice_OnClick()
Call MergeToWord("C:\My Documents\Learning Access\Merge Sample Notice.doc", "QrySampleNotice") 'For example
End Sub
Public Sub MergeToWord(strDocName As String, MyQuery As String)
Dim objApp As Object
'Change cursor to hourglass
DoCmd.Hourglass True
'Open Mailmerge Document
'Start Word
Set objApp = CreateObject("Word.Application")
With objApp
.Visible = True 'Make it visible
.Documents.Open strDocName 'Open the Mailmerge Document
'Use the Query defined in the arguments as the datasource
.ActiveDocument.MailMerge.OpenDataSource Name:=CurrentDb.Name, Connection:="QrySampleNotice" & MyQuery
End With
'Create, print and close Document
With objApp
.ActiveDocument.MailMerge.Execute 'execute mailmerge
.ActiveDocument.PrintOut Background:=False, Copies:=1 'print out 1 copy
.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges 'Avoid Saving over your template
.Quit SaveChanges:=wdDoNotSaveChanges 'close all documents
End With
Set objApp = Nothing
ErrorHandler:
Select Case Err.Number
Case 4157
End Select
DoCmd.Hourglass False 'Cursor back to normal
End Sub
When this is working I would like to also incorporate the code that wayneryan helped me with the other day using the Nz(Dcount…) so if the query is null then no “Notices” will print which I think will look like this but not sure:
'
' If no related records, msgbox, then exit sub
'
If Nz(DCount("[Surname] or [Report Number] or [Location]", _
"[QrySampleNotices]", _
"[Surname] or [Report Number] or [Location]", = '" & Me.[Surname] or [Report Number] or [Location]", & "'"), 0) = 0 Then
MsgBox "There Are No Person(s) Listed Under That Name", vbOKOnly, "Invalid Search Criterion!"
Me.[Report Number].SetFocus
Exit Sub
End If
As always thanks for the help.
BTW Wayne if you are reading, you enjoyed the Rums and Coke!!!
Navyguy
I have been trying to learn how to print a merged word document from a button on a form. Have read the MS knowledge base site and numerous posts and I thought I had come up with a winner…but I was wrong, so looking for your tutelage once again.
Name of DB: Test.mbd
Form: FrmSampleNotice
Fields on Form: Surname
Location
Report Number
Button Name:BtnSampleNotice
Query: QrySampleNotice
Word Document Path: C:\My Documents\Learning Access\Merge Sample Notice.doc
I know the Query works fine and I know that when I run the merge from the .doc it works ok also, but now I am trying to get to do it from the button on the form. I also know that the event procedure is linked to the OnClick properties. I do not get any error messages or VB errors either so not sure where else to look.
Here is the code that I am using:
Private Sub BtnSampleNotice_OnClick()
Call MergeToWord("C:\My Documents\Learning Access\Merge Sample Notice.doc", "QrySampleNotice") 'For example
End Sub
Public Sub MergeToWord(strDocName As String, MyQuery As String)
Dim objApp As Object
'Change cursor to hourglass
DoCmd.Hourglass True
'Open Mailmerge Document
'Start Word
Set objApp = CreateObject("Word.Application")
With objApp
.Visible = True 'Make it visible
.Documents.Open strDocName 'Open the Mailmerge Document
'Use the Query defined in the arguments as the datasource
.ActiveDocument.MailMerge.OpenDataSource Name:=CurrentDb.Name, Connection:="QrySampleNotice" & MyQuery
End With
'Create, print and close Document
With objApp
.ActiveDocument.MailMerge.Execute 'execute mailmerge
.ActiveDocument.PrintOut Background:=False, Copies:=1 'print out 1 copy
.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges 'Avoid Saving over your template
.Quit SaveChanges:=wdDoNotSaveChanges 'close all documents
End With
Set objApp = Nothing
ErrorHandler:
Select Case Err.Number
Case 4157
End Select
DoCmd.Hourglass False 'Cursor back to normal
End Sub
When this is working I would like to also incorporate the code that wayneryan helped me with the other day using the Nz(Dcount…) so if the query is null then no “Notices” will print which I think will look like this but not sure:
'
' If no related records, msgbox, then exit sub
'
If Nz(DCount("[Surname] or [Report Number] or [Location]", _
"[QrySampleNotices]", _
"[Surname] or [Report Number] or [Location]", = '" & Me.[Surname] or [Report Number] or [Location]", & "'"), 0) = 0 Then
MsgBox "There Are No Person(s) Listed Under That Name", vbOKOnly, "Invalid Search Criterion!"
Me.[Report Number].SetFocus
Exit Sub
End If
As always thanks for the help.
BTW Wayne if you are reading, you enjoyed the Rums and Coke!!!
Navyguy