D
Derek Wittman
Good afternoon,
I am working on controlling a Mail Merge from within MS Access using the
Word references. I have my main document and my datasource already linked to
it. What I cannot figure out is how to do the equivalent of Merging to a New
Document by way of the VBA code.
I want to merge to the new document, then save the document, then print or
email (depending on what the user selects).
Any and all help is greatly appreciated! Thank you!
Derek
My code is:
Private Sub cmdSubmit_Click()
Dim MSwordApp As word.Application
Dim MSwordDoc As word.Document
Dim strwordfilename As String
Dim savewordfilename As String
Dim selectme As Integer
DoCmd.RunSQL "Delete * from tblShortFormData"
Select Case txtRemaining
Case Is > 0.01, Is < -0.01
MsgBox "The total percentage MUST equal 100%. Please use the
'Remaining' field to check your math.", vbOKOnly, "Check your calcuations:"
Case Else
DoCmd.RunSQL "INSERT INTO tblShortFormData ([Store#], ProjectID,
ContractDate, Vendorname, Price, CompletionDate, WorkDesc, ProgPC1, ProgPC2,
ProgPC3, ProgPC4, ProgPC5, ProgPC6, ProgPC7, ProgPC8, Term1, Term2, Term3,
Term4, Term5, Term6, Term7, Term8) SELECT forms!frmShortForm.cboDCNumber,
Forms!frmShortForm.txtProjectNo, date(), forms!frmShortForm.txtVendor,
forms!frmShortForm.txtPrice, forms!frmShortForm.txtDone,
forms!frmShortForm.txtScope, forms!frmShortForm.txtPC1,
forms!frmShortForm.txtPC2, forms!frmShortForm.txtPC3,
forms!frmShortForm.txtPC4, forms!frmShortForm.txtPC5,
forms!frmShortForm.txtPC6, forms!frmShortForm.txtPC7,
forms!frmShortForm.txtPC8, forms!frmShortForm.txtTerm1,
forms!frmShortForm.txtTerm2, forms!frmShortForm.txtTerm3,
forms!frmShortForm.txtTerm4, forms!frmShortForm.txtTerm5,
forms!frmShortForm.txtTerm6, forms!frmShortForm.txtTerm7,
forms!frmShortForm.txtTerm8"
Set MSwordApp = CreateObject("word.application")
MSwordApp.Visible = True
strwordfilename =
"v:\distcntr\dcdesign\projmgmt\Contract\Short_Forms\Short_FormB_Unprotected.doc"
Set MSwordDoc = MSwordApp.Documents.Open(strwordfilename)
savewordfilename = "v:\distcntr\dcdesign\projmgmt\contract\Short_Forms\" &
Forms!frmShortForm.cboDCNumber & Forms!frmShortForm.txtVendor & ".doc"
MSwordApp.ActiveDocument.MailMerge.Destination = wdSendToNewDocument
MSwordDoc.SaveAs (savewordfilename)
selectme = MsgBox("Do you want to print or email the Short Form Contract?
YES to Print and NO to email", vbYesNo, "YES to Print or NO to email")
Select Case selectme
Case 6
MSwordDoc.PrintOut , , , , , , , , , , , True
Case 7
MSwordApp.ActiveDocument.SendMail
End Select
MSwordDoc.Close
MSwordApp.Quit
Set MSwordDoc = Nothing
Set MSwordApp = Nothing
DoCmd.SelectObject acForm, "frmShortForm"
DoCmd.Close
End Select
End Sub
I am working on controlling a Mail Merge from within MS Access using the
Word references. I have my main document and my datasource already linked to
it. What I cannot figure out is how to do the equivalent of Merging to a New
Document by way of the VBA code.
I want to merge to the new document, then save the document, then print or
email (depending on what the user selects).
Any and all help is greatly appreciated! Thank you!
Derek
My code is:
Private Sub cmdSubmit_Click()
Dim MSwordApp As word.Application
Dim MSwordDoc As word.Document
Dim strwordfilename As String
Dim savewordfilename As String
Dim selectme As Integer
DoCmd.RunSQL "Delete * from tblShortFormData"
Select Case txtRemaining
Case Is > 0.01, Is < -0.01
MsgBox "The total percentage MUST equal 100%. Please use the
'Remaining' field to check your math.", vbOKOnly, "Check your calcuations:"
Case Else
DoCmd.RunSQL "INSERT INTO tblShortFormData ([Store#], ProjectID,
ContractDate, Vendorname, Price, CompletionDate, WorkDesc, ProgPC1, ProgPC2,
ProgPC3, ProgPC4, ProgPC5, ProgPC6, ProgPC7, ProgPC8, Term1, Term2, Term3,
Term4, Term5, Term6, Term7, Term8) SELECT forms!frmShortForm.cboDCNumber,
Forms!frmShortForm.txtProjectNo, date(), forms!frmShortForm.txtVendor,
forms!frmShortForm.txtPrice, forms!frmShortForm.txtDone,
forms!frmShortForm.txtScope, forms!frmShortForm.txtPC1,
forms!frmShortForm.txtPC2, forms!frmShortForm.txtPC3,
forms!frmShortForm.txtPC4, forms!frmShortForm.txtPC5,
forms!frmShortForm.txtPC6, forms!frmShortForm.txtPC7,
forms!frmShortForm.txtPC8, forms!frmShortForm.txtTerm1,
forms!frmShortForm.txtTerm2, forms!frmShortForm.txtTerm3,
forms!frmShortForm.txtTerm4, forms!frmShortForm.txtTerm5,
forms!frmShortForm.txtTerm6, forms!frmShortForm.txtTerm7,
forms!frmShortForm.txtTerm8"
Set MSwordApp = CreateObject("word.application")
MSwordApp.Visible = True
strwordfilename =
"v:\distcntr\dcdesign\projmgmt\Contract\Short_Forms\Short_FormB_Unprotected.doc"
Set MSwordDoc = MSwordApp.Documents.Open(strwordfilename)
savewordfilename = "v:\distcntr\dcdesign\projmgmt\contract\Short_Forms\" &
Forms!frmShortForm.cboDCNumber & Forms!frmShortForm.txtVendor & ".doc"
MSwordApp.ActiveDocument.MailMerge.Destination = wdSendToNewDocument
MSwordDoc.SaveAs (savewordfilename)
selectme = MsgBox("Do you want to print or email the Short Form Contract?
YES to Print and NO to email", vbYesNo, "YES to Print or NO to email")
Select Case selectme
Case 6
MSwordDoc.PrintOut , , , , , , , , , , , True
Case 7
MSwordApp.ActiveDocument.SendMail
End Select
MSwordDoc.Close
MSwordApp.Quit
Set MSwordDoc = Nothing
Set MSwordApp = Nothing
DoCmd.SelectObject acForm, "frmShortForm"
DoCmd.Close
End Select
End Sub