By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,489 Members | 1,954 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,489 IT Pros & Developers. It's quick & easy.

create PDF and mail in one action

P: n/a
Probably just a minor detail, because I almost got it working.
Creating the PDF file goes fine, thanks to all the bas****-files from Also the mail functionality goes fine.
The only problem is that I need two buttons to make it work; one to
create the PDF file and one to fetch the recently created PDF and
attach it to en e-mail.

To create the PDF I use this code (and all the other files from "The
Access Web"):

Sub Button_Click()
PrintPDF "filename" , "reportname"
End Sub

Function PrintPDF(strPDFnaam As String, strRapport As String)

Call SetRegValue(HKEY_CURRENT_USER, "Software\Adobe\Acrobat
PDFWriter", "PDFFileName", strPDFnaam)
DoCmd.OpenReport strRapport, acViewNormal

End Function

To mail with attachment I use the code from Microsoft at which I rewrote to fit my

Sub Button_Click()

Set all "str..." Strings

SendMessage strFrom, strTo, strCC, strBCC, strSubject, strBody,
strAttachment, Preview
End Sub

Function SendMessage(Optional strFrom, Optional strTo, Optional strCC,
Optional strBCC, Optional strSubject, Optional strBody, Optional
strAttachment, Optional Preview As Boolean)

Dim objOutlook As Outlook.Application
Dim objOutlookNs As Outlook.NameSpace
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment

Set objOutlook = CreateObject("Outlook.Application")
Set objOutlookNs = objOutlook.GetNamespace("MAPI")

Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

With objOutlookMsg
.SentOnBehalfOfName = strFrom
Set objOutlookRecip = .Recipients.Add(strTo)
objOutlookRecip.Type = olTo

If strCC <> "" Then
Set objOutlookRecip = .Recipients.Add(strCC)
objOutlookRecip.Type = olCC
End If

If strBCC <> "" Then
Set objOutlookRecip = .Recipients.Add(strBCC)
objOutlookRecip.Type = olBCC
End If

.Subject = strSubject
.Body = strBody & vbCrLf & vbCrLf

If strAttachment <> "" Then
Set objOutlookAttach = .Attachments.Add(strAttachment)
End If

For Each objOutlookRecip In .Recipients
If Not objOutlookRecip.Resolve Then
End If

If Preview = True Then
strMessage = "The message has been send to " & strTo
strTitle = "Outlook"
intStyle = vbInformation
MsgBox strMessage, intStyle, strTitle
End If

End With
Set objOutlookMsg = Nothing
Set objOutlook = Nothing

Exit Function

What do I need to do to get it working in just one action? If I put
both functions behind one button, I only get the PDF, but it doesn't
get send. I tried to let the code wait or even sleep while creating
the PDF, but that didn't help.
Nov 12 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.