Hi,
My code seeks to create a PDF file and then email it to a recipient. Can I get the code to "pause" until the file has been created? At the moment, it returns an error because the file doesn't exist, but that's because I haven't yet had a chance to confirm the filename and location.
I cobbled together the code below from more than one thread. I don't know if having my DIMs part way down is a problem, I know this is generally done first.
Anyway, the part of the code that creates the PDF file is working fine. It opens a dialog box and prompts me for the location and filename. I will ALWAYS use the same name and location.
The two approaches I can think of are:
1. Code the location and filename directly in VBA so that the file is created without a dialog box. Overwrite a pre-existing file if it exists.
2. Make the email part of the code wait until I have confirmed the location and filename, then resume and send the email.
But I don't know how to accomplish either of these!!
Oh...and I have added a supplementary question in the .Body line below, about how to put line breaks in the message.
Thanks,
My code seeks to create a PDF file and then email it to a recipient. Can I get the code to "pause" until the file has been created? At the moment, it returns an error because the file doesn't exist, but that's because I haven't yet had a chance to confirm the filename and location.
I cobbled together the code below from more than one thread. I don't know if having my DIMs part way down is a problem, I know this is generally done first.
Anyway, the part of the code that creates the PDF file is working fine. It opens a dialog box and prompts me for the location and filename. I will ALWAYS use the same name and location.
The two approaches I can think of are:
1. Code the location and filename directly in VBA so that the file is created without a dialog box. Overwrite a pre-existing file if it exists.
2. Make the email part of the code wait until I have confirmed the location and filename, then resume and send the email.
But I don't know how to accomplish either of these!!
Oh...and I have added a supplementary question in the .Body line below, about how to put line breaks in the message.
Thanks,
Code:
Private Sub cmdMHReport_Click()
DoCmd.SetWarnings False
DoCmd.OpenQuery "08 Make MH by Month"
DoCmd.OpenQuery "09 MH running total"
Set Application.Printer = Application.Printers("CutePDF Writer")
DoCmd.OpenReport "rptMH"
DoCmd.SetWarnings True
'------------------------------------------------------
'Email part
Dim objOutlook As New Outlook.Application ' outlook object
Dim objMessage As MailItem, strAttach As String ' outlook mail message
strAttach = "C:\Documents and Settings\ryanp2\Desktop\MH Report.pdf"
Set objMessage = objOutlook.CreateItem(olMailItem)
With objMessage
.To = "someone@somewhere.com" ' << << << change to real address
.subject = "MH Report"
.Body = "Dear Someone+Chr(13) + Chr(10)+ Here's your report" '<< << << Is this the correct way to put separate lines in the body?
.Attachments.Add strAttach
.Send
End With
Set objOutlook = Nothing
Set objMessage = Nothing
'----------------------------------------------------------------------------------
End Sub