Page 2 of 2 FirstFirst 12
Results 16 to 28 of 28
  1. #16
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Does your Outlook template have the subject and message body already set up?



    You don't need to declare (dim) the variable(s) if you don't use it, but it should be a string.

    Please try this updated code, if your template does provide the subject just comment out the OutMail.Subject = sSubject in the function.
    Code:
    
    
    Code:
    Option Compare Database
    Option Explicit
    
    
    
    
    Private Sub cmd_Output_Certificates_To_Individual_PDFs_Click()
        Dim rs As DAO.Recordset
        Dim sFolder As String
        Dim sFile As String
        Dim sSubject As String
        Dim sBody As String
        Dim sEmailAddress As String
        Dim MyDB As DAO.Database
        
        'On Error GoTo Error_Handler
    
    
    
    
        'sFolder = Application.CurrentProject.path & "\"
        sFolder = "C:\Users\FSESO\Desktop\Back to Work\0 Reconciliation\Certificates\Certificates To Be Sent" & "\"
        'sEmailAddress = [f_Certificates]![txt_Home_Email]
        Set MyDB = CurrentDb
        Set rs = MyDB.OpenRecordset("q_Certificates_To_Email")
    
    
    
    
        With rs
            .MoveFirst
            Do While Not .EOF
                'DoCmd.OpenReport "r_Certificates", acViewPreview, , "[LName]= '" & ![LName] & "'", acHidden
                DoCmd.OpenReport "r_Certificates", acViewPreview, , "[LName]= '" & Replace(![LName], "'", "''") & "'"
                sFile = Nz(![LName], "") & "_" & Nz(![Nickname], "") & "_Restart_Certificate" & ".pdf"
                sFile = sFolder & sFile
                DoCmd.OutputTo acOutputReport, "r_Certificates", acFormatPDF, sFile, , , , acExportQualityPrint
                'If you wanted to create an e-mail and include an individual report, you would do so now
                DoCmd.Close acReport, "r_Certificates"
                sSubject =" Certificate for " & ![FNAME] & " " & ![LName]
                sEmailAddress =![Home Email]
                Call vcSendEmail_Outlook_With_Attachment sSubject, sEmailAddress,"", , sFile, "Enter body of message if template does not provide it"
                .MoveNext
            Loop
        End With
    
    
    
    
        Application.FollowHyperlink sFolder    'Optional / Open the folder housing the files
    
    
    
    
    Error_Handler_Exit:
        On Error Resume Next
        If Not rs Is Nothing Then
            rs.Close
            Set rs = Nothing
        End If
        Exit Sub
    
    
    
    
    Error_Handler:
        If Err.Number <> 2501 Then    'Let's ignore user cancellation of this action!
            MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
                   "Error Number: " & Err.Number & vbCrLf & _
                   "Error Source: cmd_Output_Certificates_To_Individual_PDFs" & vbCrLf & _
                   "Error Description: " & Err.Description & _
                   Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
                   , vbOKOnly + vbCritical, "An Error has Occured!"
        End If
        Resume Error_Handler
    
    
    End Sub

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  2. #17
    Oblio is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Aug 2013
    Posts
    50
    Quote Originally Posted by Gicu View Post
    Does your Outlook template have the subject and message body already set up?

    You don't need to declare (dim) the variable(s) if you don't use it, but it should be a string.

    Please try this updated code, if your template does provide the subject just comment out the OutMail.Subject = sSubject in the function.
    Code:
    
    
    Code:
    Option Compare Database
    Option Explicit
    
    
    
    
    Private Sub cmd_Output_Certificates_To_Individual_PDFs_Click()
        Dim rs As DAO.Recordset
        Dim sFolder As String
        Dim sFile As String
        Dim sSubject As String
        Dim sBody As String
        Dim sEmailAddress As String
        Dim MyDB As DAO.Database
        
        'On Error GoTo Error_Handler
    
    
    
    
        'sFolder = Application.CurrentProject.path & "\"
        sFolder = "C:\Users\FSESO\Desktop\Back to Work\0 Reconciliation\Certificates\Certificates To Be Sent" & "\"
        'sEmailAddress = [f_Certificates]![txt_Home_Email]
        Set MyDB = CurrentDb
        Set rs = MyDB.OpenRecordset("q_Certificates_To_Email")
    
    
    
    
        With rs
            .MoveFirst
            Do While Not .EOF
                'DoCmd.OpenReport "r_Certificates", acViewPreview, , "[LName]= '" & ![LName] & "'", acHidden
                DoCmd.OpenReport "r_Certificates", acViewPreview, , "[LName]= '" & Replace(![LName], "'", "''") & "'"
                sFile = Nz(![LName], "") & "_" & Nz(![Nickname], "") & "_Restart_Certificate" & ".pdf"
                sFile = sFolder & sFile
                DoCmd.OutputTo acOutputReport, "r_Certificates", acFormatPDF, sFile, , , , acExportQualityPrint
                'If you wanted to create an e-mail and include an individual report, you would do so now
                DoCmd.Close acReport, "r_Certificates"
                sSubject =" Certificate for " & ![FNAME] & " " & ![LName]
                sEmailAddress =![Home Email]
                Call vcSendEmail_Outlook_With_Attachment sSubject, sEmailAddress,"", , sFile, "Enter body of message if template does not provide it"
                .MoveNext
            Loop
        End With
    
    
    
    
        Application.FollowHyperlink sFolder    'Optional / Open the folder housing the files
    
    
    
    
    Error_Handler_Exit:
        On Error Resume Next
        If Not rs Is Nothing Then
            rs.Close
            Set rs = Nothing
        End If
        Exit Sub
    
    
    
    
    Error_Handler:
        If Err.Number <> 2501 Then    'Let's ignore user cancellation of this action!
            MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
                   "Error Number: " & Err.Number & vbCrLf & _
                   "Error Source: cmd_Output_Certificates_To_Individual_PDFs" & vbCrLf & _
                   "Error Description: " & Err.Description & _
                   Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
                   , vbOKOnly + vbCritical, "An Error has Occured!"
        End If
        Resume Error_Handler
    
    
    End Sub

    Cheers,
    Hi,

    The following line is causing an error: Syntax error

    Code:
                Call vcSendEmail_Outlook_With_Attachment sSubject, sEmailAddress, "", , sFile, "Enter body of message if template does not provide it"
    If I remove sSubject (because it is provided in the template) I still get Syntax error.

    Not sure what it does not like...

    Thanks,

    Bill

  3. #18
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Please try this version:
    Code:
    Option Compare Database
    Option Explicit
    
    Private Sub cmd_Output_Certificates_To_Individual_PDFs_Click()
        Dim rs As DAO.Recordset
        Dim sFolder As String
        Dim sFile As String
        Dim sSubject As String
        Dim sBody As String
        Dim sEmailAddress As String
        Dim MyDB As DAO.Database
        
        'On Error GoTo Error_Handler
        'sFolder = Application.CurrentProject.path & "\"
        sFolder = "C:\Users\FSESO\Desktop\Back to Work\0 Reconciliation\Certificates\Certificates To Be Sent" & "\"
        'sEmailAddress = [f_Certificates]![txt_Home_Email]
        Set MyDB = CurrentDb
        Set rs = MyDB.OpenRecordset("q_Certificates_To_Email")
    
        With rs
            .MoveFirst
            Do While Not .EOF
                'DoCmd.OpenReport "r_Certificates", acViewPreview, , "[LName]= '" & ![LName] & "'", acHidden
                DoCmd.OpenReport "r_Certificates", acViewPreview, , "[LName]= '" & Replace(![LName], "'", "''") & "'"
                sFile = Nz(![LName], "") & "_" & Nz(![Nickname], "") & "_Restart_Certificate" & ".pdf"
                sFile = sFolder & sFile
                DoCmd.OutputTo acOutputReport, "r_Certificates", acFormatPDF, sFile, , , , acExportQualityPrint
                'If you wanted to create an e-mail and include an individual report, you would do so now
                DoCmd.Close acReport, "r_Certificates"
                sSubject = " Certificate for " & ![FNAME] & " " & ![LName]
                sEmailAddress = ![Home Email]
                Call vcSendEmail_Outlook(sEmailAddress, , , , sFile, "Enter body of message if template does not provide it")
                .MoveNext
            Loop
        End With
        Application.FollowHyperlink sFolder    'Optional / Open the folder housing the files
    
    Error_Handler_Exit:
        On Error Resume Next
        If Not rs Is Nothing Then
            rs.Close
            Set rs = Nothing
        End If
        Exit Sub
    Error_Handler:
        If Err.Number <> 2501 Then    'Let's ignore user cancellation of this action!
            MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
                   "Error Number: " & Err.Number & vbCrLf & _
                   "Error Source: cmd_Output_Certificates_To_Individual_PDFs" & vbCrLf & _
                   "Error Description: " & Err.Description & _
                   Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
                   , vbOKOnly + vbCritical, "An Error has Occured!"
        End If
        Resume Error_Handler
    End Sub
    Function vcSendEmail_Outlook(sTo As String, Optional sSubject As String, Optional sCC As String, Optional sBcc As String, Optional sAttachment As String, Optional sBody As String)
        Dim OutApp As Object
        Dim OutMail As Object
       
        Set OutApp = CreateObject("Outlook.Application")
        OutApp.Session.Logon
        
        'Set OutMail = OutApp.CreateItem(0)
        Set OutMail = Application.CreateItemFromTemplate("C:\My Outlook Templates\Restart Process Completion Certificate.oft")
     
        OutMail.To = sTo
        If sCC <> "" Then OutMail.CC = sCC
        If sBcc <> "" Then OutMail.BCC = sBcc
        If sSubject <> "" Then OutMail.Subject = sSubject
        If sBody <> "" Then OutMail.HTMLBody = sBody
    
        OutMail.Attachments.Add (sAttachment)
    
        OutMail.Display  'Send | Display
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    End Function
    You could not remove sSubject in the first version as it was a required argument, you needed to remove it in the function as I mentioned in the previous post.
    Note that I've changed the function (renamed it too) to make the subject line optional (also moved it as the second argument). Please let me know!
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  4. #19
    Oblio is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Aug 2013
    Posts
    50
    Code:
    .CreateItemFromTemplate
    is Method or Datamember not found error

  5. #20
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Did you check the path and name for the template?

  6. #21
    Oblio is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Aug 2013
    Posts
    50
    Quote Originally Posted by Oblio View Post
    Code:
    .CreateItemFromTemplate
    is Method or Datamember not found error
    I tried moving the template to a more simple location, and same error...

    I tried adding Outlook Object Library 16.0 to see if that helped...

    I looked up the MSDN article about CreateItemFromTemplate and it was different terms than your version.

    Really am very sorry, I just do not know what to do...

    Click image for larger version. 

Name:	.CreateItem.jpg 
Views:	14 
Size:	54.1 KB 
ID:	42806

    Oblio
    Last edited by Oblio; 08-24-2020 at 05:13 PM. Reason: add a pic

  7. #22
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I have a question and a few comments.......

    Q1) Why is the error handler initialization commented out?


    C1) These two lines
    Code:
        sFolder = "C:\Users\FSESO\Desktop\Back to Work\0 Reconciliation\Certificates\Certificates To Be Sent" & "\"
        sFile = Nz(![LName], "") & "_" & Nz(![Nickname], "") & "_Restart_Certificate" & ".pdf"
    could be written like
    Code:
        sFolder = "C:\Users\FSESO\Desktop\Back to Work\0 Reconciliation\Certificates\Certificates To Be Sent\"
        sFile = Nz(![LName], "") & "_" & Nz(![Nickname], "") & "_Restart_Certificate.pdf"
    (I know.... a minor thing, but.......



    C2) Speaking of the error handler, I would write it like this:
    Code:
    Error_Handler_Exit:
        On Error Resume Next
        rs.Close
        Set rs = Nothing
        Set MyDB = Nothing
        Exit Sub

    C3) Shouldn't the last line of routine "cmd_Output_Certificates_To_Individual_PDFs_Click( )" be
    Code:
        Resume Error_Handler_Exit
    and not
    Code:
         Resume Error_Handler



    Last thing, in the call to the function "vcSendEmail_Outlook", I think you would still need to have the variable "sSubject" as a parameter - even if you left it blank (Null).
    Otherwise, you would never be able to have the subject included in the email.
    Code:
    Call vcSendEmail_Outlook(sEmailAddress, sSubject, , , sFile, "Enter body of message if template does not provide it")
    for that matter, I would also include variables for "sCC" and "sBCC" in the code just in case.

  8. #23
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Maybe try changing
    Code:
    Set OutMail = Application.CreateItemFromTemplate("C:\My Outlook Templates\Restart Process Completion Certificate.oft")
    to

    Code:
    Set OutMail = OutApp.CreateItemFromTemplate("C:\My Outlook Templates\Restart Process Completion Certificate.oft")



    I think you should use OutApp instead of Application.
    Since your code runs in Access, the keyword Application refers to the Access Application object, not to the Outlook Application object. (But I could be wrong )

  9. #24
    Oblio is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Aug 2013
    Posts
    50
    Quote Originally Posted by ssanfu View Post
    Maybe try changing
    Code:
    Set OutMail = Application.CreateItemFromTemplate("C:\My Outlook Templates\Restart Process Completion Certificate.oft")
    to

    Code:
    Set OutMail = OutApp.CreateItemFromTemplate("C:\My Outlook Templates\Restart Process Completion Certificate.oft")



    I think you should use OutApp instead of Application.
    Since your code runs in Access, the keyword Application refers to the Access Application object, not to the Outlook Application object. (But I could be wrong )
    Hi, thanks very much for your help with this !!!

    Everything compiles, but when I push the command button I get the following popup:

    Click image for larger version. 

Name:	Debug 1.jpg 
Views:	11 
Size:	23.9 KB 
ID:	42810

    I have tried two different places with the same error. The template is not open.
    Do I have to use the desktop version of Outlook, or the Web version from Office 365... I usually use the web version...

    Thanks, again for your help.

    Oblio

  10. #25
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Glad to see Steve saw the problem with the Outlook reference! I have not worked with Outlook templates before so I got the code from the same article you showed. I suspect you need the desktop version but can you try first to move the template in the same folder as the Access front-end (I assume it is a split database with the back-end on a network folder and each front-end in a local folder on each workstation) as you know you have full read/write permissions in that one.
    Edit: also see if this helps:https://stackoverflow.com/questions/...with-excel-vba
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  11. #26
    Oblio is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Aug 2013
    Posts
    50
    Quote Originally Posted by Gicu View Post
    Glad to see Steve saw the problem with the Outlook reference! I have not worked with Outlook templates before so I got the code from the same article you showed. I suspect you need the desktop version but can you try first to move the template in the same folder as the Access front-end (I assume it is a split database with the back-end on a network folder and each front-end in a local folder on each workstation) as you know you have full read/write permissions in that one.
    Edit: also see if this helps:https://stackoverflow.com/questions/...with-excel-vba
    Cheers,
    I have not split the database yet as I am still changing things. I will split it eventually, of course.

    Thanks,

    Oblio

  12. #27
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    So do you still get the error if you have the template in a read/write folder?

    If you still get it maybe you can save the subject and body of the message in a table in Access (called tblEmailSettings) and populate those two arguments of the function (the very first version I've sent you). You can build a form bound to the new table and if you use Rich Text for the textbox bound to the email body you can copy and paste from the Outlook template and keep any of the fancy formatting that you might have.

    Cheers,
    Vlad
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  13. #28
    Oblio is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Aug 2013
    Posts
    50
    Quote Originally Posted by Gicu View Post
    So do you still get the error if you have the template in a read/write folder?

    If you still get it maybe you can save the subject and body of the message in a table in Access (called tblEmailSettings) and populate those two arguments of the function (the very first version I've sent you). You can build a form bound to the new table and if you use Rich Text for the textbox bound to the email body you can copy and paste from the Outlook template and keep any of the fancy formatting that you might have.

    Cheers,
    Vlad
    Yes, I have enabled Full control to everybody on the folder, simplified the name, but it didn't work.

    I will try the table Idea.

    Thanks so much for your help !!

    Oblio

Page 2 of 2 FirstFirst 12
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 7
    Last Post: 03-21-2018, 04:58 AM
  2. Replies: 8
    Last Post: 06-24-2015, 08:22 AM
  3. Replies: 11
    Last Post: 12-20-2012, 12:30 PM
  4. Export Report to Single PDF Pages
    By 02kev02 in forum Import/Export Data
    Replies: 2
    Last Post: 05-23-2012, 07:03 AM
  5. Replies: 1
    Last Post: 04-13-2011, 01:23 PM

Tags for this Thread

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