Probably just a minor detail, because I almost got it working.
Creating the PDF file goes fine, thanks to all the bas****-files from
http://www.mvps.org/access/. 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)
ChangeToAcrobat
Call SetRegValue(HKEY_CURRENT_USER, "Software\Adobe\Acrobat
PDFWriter", "PDFFileName", strPDFnaam)
DoCmd.OpenReport strRapport, acViewNormal
ResetDefaultPrinter
End Function
***********
To mail with attachment I use the code from Microsoft at
http://support.microsoft.com/?id=209948 which I rewrote to fit my
needs.
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")
objOutlookNs.Logon
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
objOutlookRecip.Resolve
If Not objOutlookRecip.Resolve Then
objOutlookMsg.Display
End If
Next
If Preview = True Then
.Display
Else
.Send
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.