Results 1 to 7 of 7
  1. #1
    KKenui is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2011
    Posts
    9

    Save Report as PDF, then email Report as Attachment

    Hello. What I am trying to do is have the current report be sent when the submit button is hit.



    Here is a little background. Users open the program and select the form they need to fill out. An automated number is assigned as the "Report Number" and saved as Report_# in the table. Once the form is filled out, they hit the submit button. I have the code behind the "On Click" of this submit button. What I would like to do is have the data that was just entered into the fields saved to a location on my network and then email that specific report that was just filled out. Below is the code as I have it now.. It is a little messy as I have been trying every which way to get this to work with no success.


    Location I would like to save to: S:\Safety\S.R.S. Reports\Initial Reports (I will change to FQDN but pasted this for now).

    Example saved file: S:\Safety\S.R.S. Reports\Initial Reports\Event Report #147.pdf




    Private Sub Submit_Click()
    ' Install the error handler.
    ' On Error GoTo UnexpectedError

    Dim EmailAddress As String
    Dim Your_Name As String

    'Behind a comand button save the report as PDF file
    DoCmd.OutputTo acOutputReport, "Single Event Report", acFormatPDF, "S:\Safety\S.R.S. Reports\Initial Reports\Event Report #& Me.Report_# &.pdf", False


    'DoCmd.OutputTo acOutputReport, "Single Event Report", "acFormatPDF", "C:\Temp\Single Event Report.pdf", False

    Const cdoSendUsingPickup = 1
    Const cdoSendUsingPort = 2 'Must use this to use Delivery Notification
    Const cdoAnonymous = 0
    Const cdoBasic = 1 ' clear text
    Const cdoNTLM = 2 'NTLM
    'Delivery Status Notifications
    Const cdoDSNDefault = 0 'None
    Const cdoDSNNever = 1 'None
    Const cdoDSNFailure = 2 'Failure
    Const cdoDSNSuccess = 4 'Success
    Const cdoDSNDelay = 8 'Delay
    Const cdoDSNSuccessFailOrDelay = 14 'Success, failure or delay

    Set objMsg = CreateObject("CDO.Message")
    Set objConf = CreateObject("CDO.Configuration")

    Set objFlds = objConf.Fields
    With objFlds
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "emailserver"
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "username"
    .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "Password"
    .Update
    End With

    strbody = "This is a sample message." & vbCrLf
    strbody = strbody & "It was sent using CDO." & vbCrLf

    With objMsg
    Set .Configuration = objConf
    '.To = Me.EmailAddress
    .From = "me@me.com"
    .Subject = "New Event Report Number " & Me.Report__


    strbody = "<B><I>Aloha!</I></B><br><br>" & _
    "Your <B><big>Safety Event</big></B> has been successfully submitted; all information you have provided will be treated as confidential.<br><br>" & _
    "By taking the time to use this system you do make a difference and your contribution is sincerely appreciated! " & _
    "Please allow up to 7 calendar days for a response from our company Safety Officer.<br><br>" & _
    "Mahalo!<br><br><br><br>" & _
    "Report Number: " & Me.Report__


    .HTMLBody = strbody
    'use .HTMLBody to send HTML email.
    .Addattachment "C:\Temp\Single Event Report & Me.Report_# &.pdf"
    '.Fields("urn:schemas:mailheader:disposition-notification-to") = Me.EmailAddress
    .Fields("urn:schemas:mailheader:return-receipt-to") = Me.EmailAddress
    .DSNOptions = cdoDSNSuccessFailOrDelay
    .Fields.Update
    .Send

    'Exit Sub

    'UnexpectedError:
    ' Describe the error to the user.
    ' MsgBox "Unexpected error" & _
    ' Str$(Err.Number) & _
    ' " in subroutine Form_Event_Reporting_Employee_Basic Submit." & _
    ' vbCrLf & _
    ' Err.Description
    ' Exit Sub

    End With
    DoCmd.Quit
    DoCmd.CloseDatabase

    End Sub


    Sub MyProc()
    On Error GoTo Err_MyProc
    Dim db As Database
    Dim rst As Recordset

    Set db = CurrentDb()
    Set rst = db.OpenRecordset("MyTable")
    'Useful code here.
    rst.Close 'Close what you opened.

    Exit_MyProc:
    Set rst = Nothing 'Deassign all objects.
    Set db = Nothing
    Exit Sub

    Err_MyProc:
    'Error handler here.
    Resume Exit_MyProc
    End Sub

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    What doesn't work, why no success - error message, wrong results, nothing happens? Do you really need to save the PDF or just send? Did you check out the SendObject method?
    http://msdn.microsoft.com/en-us/library/ff197046.aspx
    http://www.fmsinc.com/microsoftacces...endobject.html
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    KKenui is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2011
    Posts
    9
    It just does not work. I want it to save the file in a certain directory with the name "Event Report" and then the number of the report. I then want to create an email message that would grab the highest report number from that file, attach it to an email and then send it to the specified email address. I have gotten the report to save as a specific file name, but have not gotten the &Me.Report_# syntax right to where it saves with the correct report number.

    DoCmd.OutputTo acOutputReport, "Single Event Report", acFormatPDF, "S:\Safety\S.R.S. Reports\Initial Reports\Event Report #& Me.Report_# &.pdf", False

    The above code is where I am starting from.

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    You must concatenate variables. Including them within the quotes which results in a literal string, not the value of the variable.

    DoCmd.OutputTo acOutputReport, "Single Event Report", acFormatPDF, "S:\Safety\S.R.S. Reports\Initial Reports\Event Report #" & Me.Report_#, False
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    KKenui is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2011
    Posts
    9
    Thank you!!!

    So I got the code to work. I created a different report in Access. I then told access to save it as this report....

    DoCmd.OutputTo acOutputReport, "Safety Reporting System - Initial Report", acFormatPDF, "S:\Information Technology\Projects\Single Event Event Report Number #" & Me.Report__ & ".pdf", False, , , acExportQualityPrint


    Now I need to pass the report number to the dialogue box that pops up and asks for the report number. LOL. When I manually input the number it saves correctly.


    When I try to use this code,

    DoCmd.OutputTo acOutputReport, "Safety Reporting System - Initial Report", acFormatPDF, "S:\Information Technology\Projects\Single Event Event Report Number #" & Me.Report_# & ".pdf", False, , , acExportQualityPrint

    It runtimes out stating that it cannot find Me.Report_# even thou I selected it from the dropdown list (Method or data member not found).

  6. #6
    KKenui is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2011
    Posts
    9
    I got it to work!!!



    Sub MyProc()
    On Error GoTo Err_MyProc
    Dim db As Database
    Dim rst As Recordset

    Set db = CurrentDb()
    Set rst = db.OpenRecordset("Form_Event_Reporting_-_Employee_-_Basics")
    'Useful code here.
    rst.Close 'Close what you opened.

    Exit_MyProc:
    Set rst = Nothing 'Deassign all objects.
    Set db = Nothing
    Exit Sub

    Err_MyProc:
    'Error handler here.
    Resume Exit_MyProc
    End Sub


    Private Sub Submit_Click()
    On Error GoTo UnexpectedError
    Dim EmailAddress As String
    Dim Your_Name As String


    'Behind a comand button save the report as PDF file
    DoCmd.OutputTo acOutputReport, "[My Report with Query pointing to field name on form]", acFormatPDF, "S:\[My Path]\[My File Name] #" & Me.Report__ & ".pdf" , False, , , acExportQualityPrint

    Const cdoSendUsingPickup = 1
    Const cdoSendUsingPort = 2 'Must use this to use Delivery Notification
    Const cdoAnonymous = 0
    Const cdoBasic = 1 ' clear text
    Const cdoNTLM = 2 'NTLM
    'Delivery Status Notifications
    Const cdoDSNDefault = 0 'None
    Const cdoDSNNever = 1 'None
    Const cdoDSNFailure = 2 'Failure
    Const cdoDSNSuccess = 4 'Success
    Const cdoDSNDelay = 8 'Delay
    Const cdoDSNSuccessFailOrDelay = 14 'Success, failure or delay

    Set objMsg = CreateObject("CDO.Message")
    Set objConf = CreateObject("CDO.Configuration")

    Set objFlds = objConf.Fields
    With objFlds
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mailserver"
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "username"
    .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "Password"
    .Update
    End With

    With objMsg
    Set .Configuration = objConf
    .To = Me.EmailAddress
    .cc = "cc@mail.com"
    .From = "From@domain.com"
    .Subject = "New Event Report Number " & Me.Report__


    strbody = "<B><I>Aloha!</I></B><br><br>" & _
    "Your <B><big>Safety Event</big></B> has been successfully submitted; all information you have provided will be treated as confidential.<br><br>" & _
    "By taking the time to use this system you do make a difference and your contribution is sincerely appreciated! " & _
    "Please allow up to 7 calendar days for a response from our company Safety Officer.<br><br>" & _
    "Mahalo!<br><br><br><br>" & _
    "Report Number: " & Me.Report__


    .HTMLBody = strbody
    'use .HTMLBody to send HTML email.
    .Addattachment "S:\[My Path]\[My File Name] #" & Me.Report__ & ".pdf"
    '.Fields("urn:schemas:mailheader:disposition-notification-to") = Me.EmailAddress
    '.Fields("urn:schemas:mailheader:return-receipt-to") = Me.EmailAddress
    .DSNOptions = cdoDSNSuccessFailOrDelay
    .Fields.Update
    .Send

    End With

    Set rst = Nothing 'Deassign all objects.
    Set db = Nothing

    DoCmd.Quit
    DoCmd.CloseDatabase


    UnexpectedError:
    ' Describe the error to the user.
    MsgBox "Unexpected error" & _
    Str$(Err.Number) & _
    " in subroutine Form_Event_Reporting_Employee_Basic Submit." & _
    vbCrLf & _
    Err.Description

    Exit Sub
    End Sub

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Congratulations!
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Display pdf attachment in report
    By BrianPeerless in forum Reports
    Replies: 5
    Last Post: 05-05-2011, 12:18 PM
  2. Replies: 4
    Last Post: 04-13-2011, 10:11 AM
  3. Table with attachment field and report
    By Yaan in forum Access
    Replies: 1
    Last Post: 02-13-2011, 07:01 AM
  4. Save Report in Attachment
    By A S MANN in forum Queries
    Replies: 1
    Last Post: 10-08-2010, 08:33 PM
  5. Replies: 0
    Last Post: 07-06-2010, 08:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums