473,396 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Outputting reports as PDF

54
Hi I'm relatively new to VBA and could really do with some help please!! This is going to sound really long winded i'm sorry but I hope it will paint a picture of what i'm trying to do.

Basically here is what I want to do: I want a form (Selector) to have 4 check boxes and a Run command button. When the user clicks run another form (ReportsMenu) will appear giving various options. Importantly though, when the user clicks OK (in the ReportsMenu form) I want (depending on which check boxes are checked in the Selector form) to output the reports as pdf files. The code currently outputs the reports as snapshot or rich text format (depending on which radio button is checked in ReportsMenu) but they would prefer it if instead of this it just outputted straight to PDF when OK was clicked.

However currently the Selector form has 5 radio buttons (one is used to select all) so the user can only select all or one of the 4 options (which is why they want it to have check boxes instead so they can pick more than one but not necessarily all (which I have changed in the Selector form)). The problem (eventually got there i'm sorry!!) is with the ReportsMenu form. The original code for the ReportsMenu is very messy and, as i'm not very familiar with VBA (nor with the logic of the person who wrote the code), i'm not sure which parts to change (to print as PDF) and what to leave as it is. (I would just start the whole thing from scratch but there is some code linked to outlook that im not sure affects anything else at all or not)

Here is the code (it is very long I apologise):

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Function ExSnap1()
  4. On Error GoTo Macro1_Err
  5.  
  6. Dim MyChoice As String
  7. Dim MyReport As String
  8. Dim MySchool As String
  9.  
  10. If [Forms]![ReportsMenu]![ReportFrame] = 1 Then
  11. MyChoice = "Full Special Needs (Students and Invigilators) by Faculty"
  12. MyReport = "FullSpecialNeeds"
  13. Else
  14. If [Forms]![ReportsMenu]![ReportFrame] = 2 Then
  15. MyChoice = "FullTimetablebyFaculty"
  16. MyReport = "FullTimetable"
  17. Else
  18. If [Forms]![ReportsMenu]![ReportFrame] = 3 Then
  19. MyChoice = "StudentTimeTablesbyProgFaculty"
  20. MyReport = "StudentTimeTables"
  21. Else
  22. If [Forms]![ReportsMenu]![ReportFrame] = 4 Then
  23.     If [Forms]![Selector]![chkall].Value = False Then
  24.     Call TimetableBySchool 'individual school
  25.     GoTo Ending
  26.     Else 'all schools
  27.     MyChoice = "Date Order Timetable with Locations"
  28.     MyReport = "DateOrderTimeTables"
  29. End If
  30. Else
  31. If [Forms]![ReportsMenu]![ReportFrame] = 5 Then
  32. Call AllSnap    'all of the above
  33. Exit Function
  34. End If
  35. End If
  36. End If
  37. End If
  38. End If
  39.  
  40. If [Forms]![Selector]![frmFileFormat] = 1 Then
  41.     DoCmd.OutputTo acReport, MyChoice, "SnapshotFormat(*.snp)", "c:\" & MyReport & ".snp", False, ""
  42. Else
  43.     DoCmd.OutputTo acReport, MyChoice, acFormatRTF, "c:\" & MyReport & ".rtf", False, ""
  44. End If
  45.  
  46.  
  47.  
  48. If [Forms]![ReportsMenu]![ReportFrame] <> 4 Then
  49. MySchool = [Forms]![Selector]![School]
  50.  
  51. Dim rst As Recordset
  52. Set rst = CurrentDb.OpenRecordset("Select Contact, Faculty_ID from SchoolContacts where Faculty_ID = '" & MySchool & "'")
  53.  
  54. rst.MoveFirst
  55.  
  56. Dim myName As String
  57. Dim mySchoolName As String
  58. myName = rst.Fields(0).Value
  59. End If
  60.  
  61. Dim oOutlook As Outlook.Application
  62. Dim oMessage As Outlook.MailItem
  63. Dim sFileNames As String
  64. Dim oRecip As Outlook.Recipient
  65. Dim oAttach As Outlook.Attachment
  66.  
  67. DoCmd.Echo True, "Emailing Report"
  68.  
  69. Set oOutlook = CreateObject("Outlook.Application")
  70.  
  71. Set oMessage = oOutlook.CreateItem(olMailItem)
  72.  
  73. With oMessage
  74.     .ReadReceiptRequested = True
  75.     If [Forms]![ReportsMenu]![ReportFrame] <> 4 Then
  76.     Set oRecip = .Recipients.Add(myName)
  77.     oRecip.TYPE = olTo
  78.     oRecip.Resolve
  79.     Else
  80.     MySchool = "All Schools"
  81.     End If
  82.  
  83. If [Forms]![Selector]![frmFileFormat] = 1 Then
  84.  
  85.     .Subject = Format(Now(), "yyyy-mm-dd") & " Exam Timetabling: Snapshot Report"
  86.     .Body = "Find attached Snapshot Report: " & MyChoice & " for School Code: " & MySchool & vbCrLf & vbCrLf
  87.     .Body = .Body & "If you do not have Snapshot viewer, download it from http://www.microsoft.com/downloads/details.aspx?amp;amp;displaylang=en&familyid=B73DF33F-6D74-423D-8274-8B7E6313EDFB&displaylang=en" & vbCrLf & vbCrLf
  88.  
  89.     Set oAttach = .Attachments.Add("c:\" & MyReport & ".snp")
  90.  
  91. Else
  92.  
  93.     .Subject = Format(Now(), "yyyy-mm-dd") & " Exam Timetabling: Report"
  94.     .Body = "Find attached Report: " & MyChoice & " for School Code: " & MySchool & vbCrLf & vbCrLf
  95.  
  96.     Set oAttach = .Attachments.Add("c:\" & MyReport & ".rtf")
  97.  
  98. End If
  99.  
  100.     .Save
  101.     '.send
  102.  
  103. End With
  104. Set oOutlook = Nothing
  105. Macro1_Exit:
  106.     Exit Function
  107.  
  108. Macro1_Err:
  109.     MsgBox Error$
  110.     Resume Macro1_Exit
  111. Ending:
  112. End Function
  113.  
  114. Function TimetableBySchool()    'date order timetable for the selected school
  115. On Error GoTo Macro1_Err
  116.  
  117. Dim MyChoice As String
  118. Dim MyReport As String
  119. Dim MySchool As String
  120.  
  121. MyChoice = "Date Order Timetable with Locations by School"
  122. MyReport = "DateOrderTimeTablesBySchool"
  123.  
  124. If [Forms]![Selector]![frmFileFormat] = 1 Then
  125. DoCmd.OutputTo acReport, MyChoice, "SnapshotFormat(*.snp)", "c:\" & MyReport & ".snp", False, ""
  126. Else
  127. DoCmd.OutputTo acReport, MyChoice, acFormatRTF, "c:\" & MyReport & ".rtf", False, ""
  128. End If
  129.  
  130. MySchool = [Forms]![Selector]![School]
  131.  
  132. Dim rst As Recordset
  133. Set rst = CurrentDb.OpenRecordset("Select Contact, Faculty_ID from SchoolContacts where Faculty_ID = '" & MySchool & "'")
  134.  
  135. rst.MoveFirst
  136.  
  137. Dim myName As String
  138. Dim mySchoolName As String
  139. myName = rst.Fields(0).Value
  140.  
  141. Dim oOutlook As Outlook.Application
  142. Dim oMessage As Outlook.MailItem
  143. Dim sFileNames As String
  144. Dim oRecip As Outlook.Recipient
  145. Dim oAttach As Outlook.Attachment
  146.  
  147. DoCmd.Echo True, "Emailing Report"
  148.  
  149. Set oOutlook = CreateObject("Outlook.Application")
  150.  
  151. Set oMessage = oOutlook.CreateItem(olMailItem)
  152.  
  153. With oMessage
  154.     .ReadReceiptRequested = True
  155.  
  156. If [Forms]![Selector]![frmFileFormat] = 1 Then
  157.     .Subject = Format(Now(), "yyyy-mm-dd") & " Exam Timetabling: Snapshot Report"
  158.     .Body = "Find attached Snapshot Report: " & MyChoice & " for School Code: " & MySchool & vbCrLf & vbCrLf
  159.     .Body = .Body & "If you do not have Snapshot viewer, download it from http://www.microsoft.com/downloads/details.aspx?amp;amp;displaylang=en&familyid=B73DF33F-6D74-423D-8274-8B7E6313EDFB&displaylang=en" & vbCrLf & vbCrLf
  160.  
  161.     Set oAttach = .Attachments.Add("c:\" & MyReport & ".snp")
  162.  
  163. Else
  164.     .Subject = Format(Now(), "yyyy-mm-dd") & " Exam Timetabling: Report"
  165.     .Body = "Find attached Report: " & MyChoice & " for School Code: " & MySchool & vbCrLf & vbCrLf
  166.  
  167.     Set oAttach = .Attachments.Add("c:\" & MyReport & ".rtf")
  168.  
  169. End If
  170.     .Save
  171.     '.send
  172.  
  173. End With
  174. Set oOutlook = Nothing
  175. Macro1_Exit:
  176.     Exit Function
  177.  
  178. Macro1_Err:
  179.     MsgBox Error$
  180.     Resume Macro1_Exit
  181.  
  182. End Function
  183.  
  184.  
  185. Function AllSnap()      'all of the above option
  186. On Error GoTo Macro1_Err
  187.  
  188. Dim MyChoice As String
  189. Dim MyReport As String
  190. Dim MySchool As String
  191.  
  192. MyChoice = "Full Special Needs (Students and Invigilators) by Faculty"
  193. MyReport = "FullSpecialNeeds"
  194. MyChoice1 = "FullTimetablebyFaculty"
  195. MyReport1 = "FullTimetableby"
  196. MyChoice2 = "StudentTimeTablesbyProgFaculty"
  197. MyReport2 = "StudentTimeTables"
  198.  
  199. If [Forms]![Selector]![frmFileFormat] = 1 Then
  200.     DoCmd.OutputTo acReport, MyChoice, "SnapshotFormat(*.snp)", "c:\" & MyReport & ".snp", False, ""
  201.     DoCmd.OutputTo acReport, MyChoice1, "SnapshotFormat(*.snp)", "c:\" & MyReport1 & ".snp", False, ""
  202.     DoCmd.OutputTo acReport, MyChoice2, "SnapshotFormat(*.snp)", "c:\" & MyReport2 & ".snp", False, ""
  203. Else
  204.     DoCmd.OutputTo acReport, MyChoice, acFormatRTF, "c:\" & MyReport & ".rtf", False, ""
  205.     DoCmd.OutputTo acReport, MyChoice1, acFormatRTF, "c:\" & MyReport1 & ".rtf", False, ""
  206.     DoCmd.OutputTo acReport, MyChoice2, acFormatRTF, "c:\" & MyReport2 & ".rtf", False, ""
  207. End If
  208.  
  209. MySchool = [Forms]![Selector]![School]
  210.  
  211. Dim rst As Recordset
  212.  
  213. Set rst = CurrentDb.OpenRecordset("Select Contact, Faculty_ID from SchoolContacts where Faculty_ID = '" & MySchool & "'")
  214.  
  215. rst.MoveFirst
  216.  
  217. Dim myName As String
  218. Dim mySchoolName As String
  219. myName = rst.Fields(0).Value
  220.  
  221. Dim oOutlook As Outlook.Application
  222. Dim oMessage As Outlook.MailItem
  223. Dim sFileNames As String
  224. Dim oRecip As Outlook.Recipient
  225. Dim oAttach As Outlook.Attachment
  226. Dim oAttach1 As Outlook.Attachment
  227. Dim oAttach2 As Outlook.Attachment
  228.  
  229. DoCmd.Echo True, "Emailing Report"
  230.  
  231. Set oOutlook = CreateObject("Outlook.Application")
  232.  
  233. Set oMessage = oOutlook.CreateItem(olMailItem)
  234.  
  235. With oMessage
  236.     .ReadReceiptRequested = True
  237.     If [Forms]![ReportsMenu]![ReportFrame] <> 4 Then
  238.     Set oRecip = .Recipients.Add(myName)
  239.     Else
  240.     Set oRecip = .Recipients.Add("rgbf1")
  241.     End If
  242.         oRecip.TYPE = olTo
  243.         oRecip.Resolve
  244.  
  245. If [Forms]![Selector]![frmFileFormat] = 1 Then
  246.     .Subject = Format(Now(), "yyyy-mm-dd") & " Exam Timetabling: Snapshot Report"
  247.     .Body = "Find attached Snapshot Reports: " & MyChoice & ", " & MyChoice1 & " and " & MyChoice2 & " for School Code: " & MySchool & vbCrLf & vbCrLf
  248.     .Body = .Body & "If you do not have Snapshot viewer, download it from http://www.microsoft.com/downloads/details.aspx?amp;amp;displaylang=en&familyid=B73DF33F-6D74-423D-8274-8B7E6313EDFB&displaylang=en" & vbCrLf & vbCrLf
  249.  
  250.     Set oAttach = .Attachments.Add("c:\" & MyReport & ".snp")
  251.     Set oAttach1 = .Attachments.Add("c:\" & MyReport1 & ".snp")
  252.     Set oAttach2 = .Attachments.Add("c:\" & MyReport2 & ".snp")
  253. Else
  254.     .Subject = Format(Now(), "yyyy-mm-dd") & " Exam Timetabling: Report"
  255.     .Body = "Find attached Snapshot Reports: " & MyChoice & ", " & MyChoice1 & " and " & MyChoice2 & " for School Code: " & MySchool & vbCrLf & vbCrLf
  256.  
  257.     Set oAttach = .Attachments.Add("c:\" & MyReport & ".rtf")
  258.     Set oAttach1 = .Attachments.Add("c:\" & MyReport1 & ".rtf")
  259.     Set oAttach2 = .Attachments.Add("c:\" & MyReport2 & ".rtf")
  260.  
  261.  
  262. End If
  263.     .Save
  264.     '.send
  265.  
  266. End With
  267. Set oOutlook = Nothing
  268. Macro1_Exit:
  269.     Exit Function
  270.  
  271. Macro1_Err:
  272.     MsgBox Error$
  273.     Resume Macro1_Exit
  274.  
  275. End Function
  276.  
  277.  
As you can see the code is very messy and hard to follow for a simpleton like me. If you could help me to adjust the code accordingly that would be great!

If you need me to clarify anything else let me know

Thankyou I really appreciate it!
Sep 17 '09 #1

✓ answered by ajalwaysus

Please look at this link about printing out to a PDF, I understand you are just learning VBA but you look like you are picking it up quickly and this should get you in the right direction.

Lebans Report to PDF

-AJ

22 9241
ajalwaysus
266 Expert 100+
Please look at this link about printing out to a PDF, I understand you are just learning VBA but you look like you are picking it up quickly and this should get you in the right direction.

Lebans Report to PDF

-AJ
Sep 17 '09 #2
Megalog
378 Expert 256MB
If you're using Access 2007, printing to a pdf is reduced to a simple one line command:

Expand|Select|Wrap|Line Numbers
  1. DoCmd.OutputTo acReport, MyChoice, acFormatPDF, "c:\" & MyReport & ".pdf", False, ""
Access 2007 RTM & 2007 Service Pack 1 require you have the Convert to PDF/XPS add-in installed.

Access 2007 Service Pack 2 adds native support for the conversions.

Now, adding this line into your routines above is the tricky part. If I have time later, and nobody else assists you, I'll revisit the code and make some suggestions. But let us know which version you and your clients are using first.
Sep 17 '09 #3
g diddy
54
Thanks for the link ajalwaysus I will check it out now.
If you're using Access 2007, printing to a pdf is reduced to a simple one line command:

Expand|Select|Wrap|Line Numbers DoCmd.OutputTo acReport, MyChoice, acFormatPDF, "c:\" & MyReport & ".pdf", False, ""
Access 2007 RTM & 2007 Service Pack 1 require you have the Convert to PDF/XPS add-in installed.

Access 2007 Service Pack 2 adds native support for the conversions.

Now, adding this line into your routines above is the tricky part. If I have time later, and nobody else assists you, I'll revisit the code and make some suggestions. But let us know which version you and your clients are using first.
Thank you mate. I'm using Access 2003 if that helps at all?
Sep 18 '09 #4
g diddy
54
Anyone at all able to help me please?

Thanks
Sep 22 '09 #5
g diddy
54
If it helps, here are the names of the checkboxes I will be replacing the radio buttons with:
(OLD - Radio) [Forms]![ReportsMenu]![ReportFrame] = 1
(NEW - Check) SNInvigBySchool
(OLD - Radio) [Forms]![ReportsMenu]![ReportFrame] = 2
(NEW - Check) InvigTTBySchool
(OLD - Radio) [Forms]![ReportsMenu]![ReportFrame] = 3
(NEW - Check) StuTTBySchool
(OLD - Radio) [Forms]![ReportsMenu]![ReportFrame] = 4
(NEW - Check) DateOrderTimetables
(OLD - Radio) [Forms]![ReportsMenu]![ReportFrame] = 5
(NEW - Check) Select All

Although the code is over 200 lines long in post 1 there is, im guessing by looking at the code, only about 20 lines or so that actually need changing the problem is I don't really know how to do it. Help would be greatly appreciated!!
Sep 22 '09 #6
g diddy
54
OK I have spent all week working through the code (on my own...) and I have now sorted out the problem with the code (I think so anyways!) All I need to do now is convert it to PDF (instead of rtf) so i'l retry that link thanks AJalwaysus
Sep 22 '09 #7
NeoPa
32,556 Expert Mod 16PB
I'm sure once you reply indicating how you got on with it then AJ will respond G_Diddy.
Sep 22 '09 #8
g diddy
54
OK thanks NeoPa.

So far I have changed the code so that it now accomodates for check boxes rather than radio buttons and have also got rid of code that was redundant. The only problem i'm stuck with now is that I don't know how to change it to PDF. I have looked at that site and have found the function:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdReportToPDF_Click()
  2. ' Save the Report as a PDF document.
  3. ' The selected report is first exported to Snapshot format.
  4. ' The Snapshot file is then broken out into its
  5. ' component Enhanced Metafiles(EMF), one for each page of the report.
  6. ' Finally, the EMF's are converted to PDF pages within the master
  7. ' PDF document.
  8.  
  9. ' The function call is:
  10. 'Public Function ConvertReportToPDF( _
  11. 'Optional RptName As String = "", _
  12. 'Optional SnapshotName As String = "", _
  13. 'Optional OutputPDFname As String = "", _
  14. 'Optional ShowSaveFileDialog As Boolean = False, _
  15. 'Optional StartPDFViewer As Boolean = True, _
  16. 'Optional CompressionLevel As Long = 150, _
  17. 'Optional PasswordOpen As String = "", _
  18. 'Optional PasswordOwner As String = "", _
  19. 'Optional PasswordRestrictions As Long = 0, _
  20. 'Optional PDFNoFontEmbedding as Long = 0, _
  21. 'Optional PDFUnicodeFlags As Long = 0 _
  22. ') As Boolean
  23.  
  24. ' RptName is the name of a report contained within this MDB
  25. ' SnapshotName is the name of an existing Snapshot file
  26. ' OutputPDFname is the name you select for the output PDF file
  27. ' ShowSaveFileDialog is a boolean param to specify whether or not to display
  28. ' the standard windows File Dialog window to select an exisiting Snapshot file
  29. ' CompressionLevel - Resolution in DPI(Dots per Inch) to apply to embedded Images
  30. ' PasswordOpen - Users require to Open PDF
  31. ' PasswordOwner  - Users require to modify PDF
  32. ' PasswordRestrictions - Restrictions for viewing/editing/printing PDF - See modReportToPDF for comments
  33. ' PDFNoFontEmbedding - Do not Embed fonts in PDF. Set to 1 to stop the
  34. ' default process of embedding all fonts in the output PDF. If you are
  35. ' using ONLY - any of the standard Windows fonts
  36. ' using ONLY - any of the standard 14 Fonts natively supported by the PDF spec
  37. 'The 14 Standard Fonts
  38. 'All version of Adobe's Acrobat support 14 standard fonts. These fonts are always available
  39. 'independent whether they're embedded or not.
  40. 'Family name PostScript name Style
  41. 'Courier Courier fsNone
  42. 'Courier Courier-Bold fsBold
  43. 'Courier Courier-Oblique fsItalic
  44. 'Courier Courier-BoldOblique fsBold + fsItalic
  45. 'Helvetica Helvetica fsNone
  46. 'Helvetica Helvetica-Bold fsBold
  47. 'Helvetica Helvetica-Oblique fsItalic
  48. 'Helvetica Helvetica-BoldOblique fsBold + fsItalic
  49. 'Times Times-Roman fsNone
  50. 'Times Times-Bold fsBold
  51. 'Times Times-Italic fsItalic
  52. 'Times Times-BoldItalic fsBold + fsItalic
  53. 'Symbol Symbol fsNone, other styles are emulated only
  54. 'ZapfDingbats ZapfDingbats fsNone, other styles are emulated only
  55.  
  56. ' PDFUnicodeFlags controls how each metafile text record is interpreted in terms
  57. ' of Unicode and BiDi language. See modDocumentor for details.
  58. '
  59. ' You must pass either RptName or SnapshotName or set the ShowSaveFileDialog param to TRUE.
  60. ' Any file names you pass to this function must include the full path. If you only include the
  61. ' filename for the output PDF then your document will be saved to your My Documents folder.
  62.  
  63.  
  64. Dim blRet As Boolean
  65. ' Call our convert function
  66. ' Please note the last param signals whether to perform
  67. ' font embedding or not. I have turned font embedding ON for this example.
  68. blRet = ConvertReportToPDF(Me.lstRptName, vbNullString, _
  69. Me.lstRptName.Value & ".pdf", False, True, 150, "", "", 0, 0, 0)
  70. ' To modify the above call to force the File Save Dialog to select the name and path
  71. ' for the saved PDF file simply change the ShowSaveFileDialog param to TRUE.
  72.  
  73. End Sub
  74.  
However i'm not sure how to call it in my code. In the code below I want to replace all rtf with PDF

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Function ExSnap1()
  4. On Error GoTo Macro1_Err
  5.  
  6. Dim MyChoice As String
  7. Dim MyChoiceOne As String
  8. Dim MyChoiceTwo As String
  9. Dim MyChoiceThree As String
  10. Dim MyReport As String
  11. Dim MyReportOne As String
  12. Dim MyReportTwo As String
  13. Dim MyReportThree As String
  14. Dim MySchool As String
  15. Dim Choice As Boolean
  16. Choice = Nz(SNInvigBySchool, False)
  17. Dim ChoiceOne As Boolean
  18. ChoiceOne = Nz(InvigTTBySchool, False)
  19. Dim ChoiceTwo As Boolean
  20. ChoiceTwo = Nz(StuTTBySchool, False)
  21. Dim ChoiceThree As Boolean
  22. ChoiceThree = Nz(DateOrderTimetables, False)
  23. Dim ChoiceFour As Boolean
  24. ChoiceFour = Nz(Select_All, False)
  25.  
  26. If [Forms]![ReportsMenu]![SNInvigBySchool].Value = True Then
  27. Choice = True
  28. MyChoice = "Full Special Needs (Students and Invigilators) by Faculty"
  29. MyReport = "FullSpecialNeeds"
  30. Else
  31. If [Forms]![ReportsMenu]![InvigTTBySchool].Value = True Then
  32. ChoiceOne = True
  33. MyChoiceOne = "FullTimetablebyFaculty"
  34. MyReportOne = "FullTimetable"
  35. Else
  36. If [Forms]![ReportsMenu]![StuTTBySchool].Value = True Then
  37. ChoiceTwo = True
  38. MyChoiceTwo = "StudentTimeTablesbyProgFaculty"
  39. MyReportTwo = "StudentTimeTables"
  40. Else
  41. If [Forms]![ReportsMenu]![DateOrderTimetables].Value = True Then
  42. ChoiceThree = True
  43.     If [Forms]![Selector]![chkall].Value = False Then
  44.     Call TimetableBySchool 'individual school
  45.     GoTo Ending
  46.     Else 'all schools
  47.     MyChoiceThree = "Date Order Timetable with Locations"
  48.     MyReportThree = "DateOrderTimeTables"
  49. End If
  50. Else
  51. If [Forms]![ReportsMenu]![Select All].Value = True Then
  52.     ChoiceFour = True
  53.     MyChoice = "Full Special Needs (Students and Invigilators) by Faculty"
  54.     MyReport = "FullSpecialNeeds"
  55.     MyChoiceOne = "FullTimetablebyFaculty"
  56.     MyReportOne = "FullTimetable"
  57.     MyChoiceTwo = "StudentTimeTablesbyProgFaculty"
  58.     MyReportTwo = "StudentTimeTables"
  59.     If [Forms]![Selector]![chkall].Value = False Then
  60.     Call TimetableBySchool 'individual school
  61.     GoTo Ending
  62.     Else 'all schools
  63.     MyChoiceThree = "Date Order Timetable with Locations"
  64.     MyReportThree = "DateOrderTimeTables"
  65.  
  66. Exit Function
  67. End If
  68. End If
  69. End If
  70. End If
  71. End If
  72. End If
  73.  
  74. If Choice = True Then
  75.     DoCmd.OutputTo acReport, MyChoice, acFormatRTF, "c:\" & MyReport & ".rtf", False, ""
  76. Else
  77. If ChoiceOne = True Then
  78.     DoCmd.OutputTo acReport, MyChoiceOne, acFormatRTF, "c:\" & MyReportOne & ".rtf", False, ""
  79. Else
  80. If ChoiceTwo = True Then
  81.     DoCmd.OutputTo acReport, MyChoiceTwo, acFormatRTF, "c:\" & MyReportTwo & ".rtf", False, ""
  82. Else
  83. If ChoiceThree = True Then
  84.     DoCmd.OutputTo acReport, MyChoiceThree, acFormatRTF, "c:\" & MyReportThree & ".rtf", False, ""
  85. Else
  86. If ChoiceFour = True Then
  87.     DoCmd.OutputTo acReport, MyChoice, acFormatRTF, "c:\" & MyReport & ".rtf", False, ""
  88.     DoCmd.OutputTo acReport, MyChoiceOne, acFormatRTF, "c:\" & MyReportOne & ".rtf", False, ""
  89.     DoCmd.OutputTo acReport, MyChoiceTwo, acFormatRTF, "c:\" & MyReportTwo & ".rtf", False, ""
  90.     DoCmd.OutputTo acReport, MyChoiceThree, acFormatRTF, "c:\" & MyReportThree & ".rtf", False, ""
  91. End If
  92. End If
  93. End If
  94. End If
  95. End If
  96.  
  97. MySchool = [Forms]![Selector]![School]
  98.  
  99. Dim rst As Recordset
  100. Set rst = CurrentDb.OpenRecordset("Select Contact, Faculty_ID from SchoolContacts where Faculty_ID = '" & MySchool & "'")
  101.  
  102. rst.MoveFirst
  103.  
  104. Dim myName As String
  105. Dim mySchoolName As String
  106. myName = rst.Fields(0).Value
  107.  
  108. Dim oOutlook As Outlook.Application
  109. Dim oMessage As Outlook.MailItem
  110. Dim sFileNames As String
  111. Dim oRecip As Outlook.Recipient
  112. Dim oAttach As Outlook.Attachment
  113. Dim oAttach1 As Outlook.Attachment
  114. Dim oAttach2 As Outlook.Attachment
  115. Dim oAttach3 As Outlook.Attachment
  116.  
  117. DoCmd.Echo True, "Emailing Report"
  118.  
  119. Set oOutlook = CreateObject("Outlook.Application")
  120.  
  121. Set oMessage = oOutlook.CreateItem(olMailItem)
  122.  
  123. With oMessage
  124.     .ReadReceiptRequested = True
  125.     If [Forms]![ReportsMenu]![DateOrderTimetables] = True Then
  126.     MySchool = "All Schools"
  127.     Else
  128.     Set oRecip = .Recipients.Add(myName)
  129.     oRecip.TYPE = olTo
  130.     oRecip.Resolve
  131.     End If
  132.  
  133.     .Subject = Format(Now(), "yyyy-mm-dd") & " Exam Timetabling: Report"
  134.     If Choice = True Then
  135.     .Body = "Find attached Report: " & MyChoice & " for School Code: " & MySchool & vbCrLf & vbCrLf
  136.     Set oAttach = .Attachments.Add("c:\" & MyReport & ".rtf")
  137.  
  138.     If ChoiceOne = True Then
  139.     .Body = "Find attached Report: " & MyChoiceOne & " for School Code: " & MySchool & vbCrLf & vbCrLf
  140.     Set oAttach1 = .Attachments.Add("c:\" & MyReportOne & ".rtf")
  141.  
  142.     If ChoiceTwo = True Then
  143.     .Body = "Find attached Report: " & MyChoiceTwo & " for School Code: " & MySchool & vbCrLf & vbCrLf
  144.     Set oAttach2 = .Attachments.Add("c:\" & MyReportTwo & ".rtf")
  145.  
  146.     If ChoiceThree = True Then
  147.     .Body = "Find attached Report: " & MyChoiceThree & " for School Code: " & MySchool & vbCrLf & vbCrLf
  148.     Set oAttach3 = .Attachments.Add("c:\" & MyReportThree & ".rtf")
  149.  
  150.     If Choice = True And ChoiceOne = True Then
  151.     .Body = "Find attached Report: " & MyChoice & ", " & MyChoiceOne & " for School Code: " & MySchool & vbCrLf & vbCrLf
  152.     Set oAttach = .Attachments.Add("c:\" & MyReport & ".rtf")
  153.     Set oAttach1 = .Attachments.Add("c:\" & MyReportOne & ".rtf")
  154.  
  155.     If Choice = True And ChoiceTwo = True Then
  156.     .Body = "Find attached Report: " & MyChoice & ", " & MyChoiceTwo & " for School Code: " & MySchool & vbCrLf & vbCrLf
  157.     Set oAttach = .Attachments.Add("c:\" & MyReport & ".rtf")
  158.     Set oAttach2 = .Attachments.Add("c:\" & MyReportTwo & ".rtf")
  159.  
  160.     If Choice = True And ChoiceThree = True Then
  161.     .Body = "Find attached Report: " & MyChoice & ", " & MyChoiceThree & " for School Code: " & MySchool & vbCrLf & vbCrLf
  162.     Set oAttach = .Attachments.Add("c:\" & MyReport & ".rtf")
  163.     Set oAttach3 = .Attachments.Add("c:\" & MyReportThree & ".rtf")
  164.  
  165.     If ChoiceOne = True And ChoiceTwo = True Then
  166.     .Body = "Find attached Report: " & MyChoiceOne & ", " & MyChoiceTwo & " for School Code: " & MySchool & vbCrLf & vbCrLf
  167.     Set oAttach1 = .Attachments.Add("c:\" & MyReportOne & ".rtf")
  168.     Set oAttach2 = .Attachments.Add("c:\" & MyReportTwo & ".rtf")
  169.  
  170.     If ChoiceOne = True And ChoiceThree = True Then
  171.     .Body = "Find attached Report: " & MyChoiceOne & ", " & MyChoiceThree & " for School Code: " & MySchool & vbCrLf & vbCrLf
  172.     Set oAttach1 = .Attachments.Add("c:\" & MyReportOne & ".rtf")
  173.     Set oAttach3 = .Attachments.Add("c:\" & MyReportThree & ".rtf")
  174.  
  175.     If ChoiceTwo = True And ChoiceThree = True Then
  176.     .Body = "Find attached Report: " & MyChoiceTwo & ", " & MyChoiceThree & " for School Code: " & MySchool & vbCrLf & vbCrLf
  177.     Set oAttach2 = .Attachments.Add("c:\" & MyReportTwo & ".rtf")
  178.     Set oAttach3 = .Attachments.Add("c:\" & MyReportThree & ".rtf")
  179.  
  180.     If Choice = True And ChoiceOne = True And ChoiceTwo = True Then
  181.     .Body = "Find attached Report: " & MyChoice & ", " & MyChoiceOne & ", " & MyChoiceTwo & " for School Code: " & MySchool & vbCrLf & vbCrLf
  182.     Set oAttach = .Attachments.Add("c:\" & MyReport & ".rtf")
  183.     Set oAttach1 = .Attachments.Add("c:\" & MyReportOne & ".rtf")
  184.     Set oAttach2 = .Attachments.Add("c:\" & MyReportTwo & ".rtf")
  185.  
  186.     If Choice = True And ChoiceOne = True And ChoiceThree = True Then
  187.     .Body = "Find attached Report: " & MyChoice & ", " & MyChoiceOne & ", " & MyChoiceThree & " for School Code: " & MySchool & vbCrLf & vbCrLf
  188.     Set oAttach = .Attachments.Add("c:\" & MyReport & ".rtf")
  189.     Set oAttach1 = .Attachments.Add("c:\" & MyReportOne & ".rtf")
  190.     Set oAttach3 = .Attachments.Add("c:\" & MyReportThree & ".rtf")
  191.  
  192.     If Choice = True And ChoiceTwo = True And ChoiceThree = True Then
  193.     .Body = "Find attached Report: " & MyChoice & ", " & MyChoiceTwo & ", " & MyChoiceThree & " for School Code: " & MySchool & vbCrLf & vbCrLf
  194.     Set oAttach = .Attachments.Add("c:\" & MyReport & ".rtf")
  195.     Set oAttach2 = .Attachments.Add("c:\" & MyReportTwo & ".rtf")
  196.     Set oAttach3 = .Attachments.Add("c:\" & MyReportThree & ".rtf")
  197.  
  198.     If ChoiceOne = True And ChoiceTwo = True And ChoiceThree = True Then
  199.     .Body = "Find attached Report: " & MyChoiceOne & ", " & MyChoiceTwo & ", " & MyChoiceThree & " for School Code: " & MySchool & vbCrLf & vbCrLf
  200.     Set oAttach1 = .Attachments.Add("c:\" & MyReportOne & ".rtf")
  201.     Set oAttach2 = .Attachments.Add("c:\" & MyReportTwo & ".rtf")
  202.     Set oAttach3 = .Attachments.Add("c:\" & MyReportThree & ".rtf")
  203.  
  204.     If Choice = True And ChoiceOne = True And ChoiceTwo = True And ChoiceThree = True Then
  205.     .Body = "Find attached Report: " & MyChoice & ", " & MyChoiceOne & ", " & MyChoiceTwo & ", " & MyChoiceThree & " for School Code: " & MySchool & vbCrLf & vbCrLf
  206.     Set oAttach = .Attachments.Add("c:\" & MyReport & ".rtf")
  207.     Set oAttach1 = .Attachments.Add("c:\" & MyReportOne & ".rtf")
  208.     Set oAttach2 = .Attachments.Add("c:\" & MyReportTwo & ".rtf")
  209.     Set oAttach3 = .Attachments.Add("c:\" & MyReportThree & ".rtf")
  210.  
  211.     If ChoiceFour = True Then
  212.     .Body = "Find attached Report: " & MyChoice & ", " & MyChoiceOne & ", " & MyChoiceTwo & ", " & MyChoiceThree & " for School Code: " & MySchool & vbCrLf & vbCrLf
  213.     Set oAttach = .Attachments.Add("c:\" & MyReport & ".rtf")
  214.     Set oAttach1 = .Attachments.Add("c:\" & MyReportOne & ".rtf")
  215.     Set oAttach2 = .Attachments.Add("c:\" & MyReportTwo & ".rtf")
  216.     Set oAttach3 = .Attachments.Add("c:\" & MyReportThree & ".rtf")
  217.     End If
  218.     End If
  219.     End If
  220.     End If
  221.     End If
  222.     End If
  223.     End If
  224.     End If
  225.     End If
  226.     End If
  227.     End If
  228.     End If
  229.     End If
  230.     End If
  231.     End If
  232.     End If
  233.  
  234.  
  235.     .Save
  236.     '.send
  237.  
  238. End With
  239. Set oOutlook = Nothing
  240. Macro1_Exit:
  241.     Exit Function
  242.  
  243. Macro1_Err:
  244.     MsgBox Error$
  245.     Resume Macro1_Exit
  246. Ending:
  247. End Function
  248.  
  249. Function TimetableBySchool()    'date order timetable for the selected school
  250. On Error GoTo Macro1_Err
  251.  
  252. Dim MyChoice As String
  253. Dim MyReport As String
  254. Dim MySchool As String
  255.  
  256. MyChoice = "Date Order Timetable with Locations by School"
  257. MyReport = "DateOrderTimeTablesBySchool"
  258.  
  259. DoCmd.OutputTo acReport, MyChoice, acFormatRTF, "c:\" & MyReport & ".rtf", False, ""
  260.  
  261. MySchool = [Forms]![Selector]![School]
  262.  
  263. Dim rst As Recordset
  264. Set rst = CurrentDb.OpenRecordset("Select Contact, Faculty_ID from SchoolContacts where Faculty_ID = '" & MySchool & "'")
  265.  
  266. rst.MoveFirst
  267.  
  268. Dim myName As String
  269. Dim mySchoolName As String
  270. myName = rst.Fields(0).Value
  271.  
  272. Dim oOutlook As Outlook.Application
  273. Dim oMessage As Outlook.MailItem
  274. Dim sFileNames As String
  275. Dim oRecip As Outlook.Recipient
  276. Dim oAttach As Outlook.Attachment
  277.  
  278. DoCmd.Echo True, "Emailing Report"
  279.  
  280. Set oOutlook = CreateObject("Outlook.Application")
  281.  
  282. Set oMessage = oOutlook.CreateItem(olMailItem)
  283.  
  284. With oMessage
  285.     .ReadReceiptRequested = True
  286.     .Subject = Format(Now(), "yyyy-mm-dd") & " Exam Timetabling: Report"
  287.     .Body = "Find attached Report: " & MyChoice & " for School Code: " & MySchool & vbCrLf & vbCrLf
  288.  
  289.     Set oAttach = .Attachments.Add("c:\" & MyReport & ".rtf")
  290.  
  291.     .Save
  292.     '.send
  293.  
  294. End With
  295. Set oOutlook = Nothing
  296. Macro1_Exit:
  297.     Exit Function
  298.  
  299. Macro1_Err:
  300.     MsgBox Error$
  301.     Resume Macro1_Exit
  302.  
  303. End Function
  304.  
I really appreciate your help!
Sep 23 '09 #9
ajalwaysus
266 Expert 100+
g diddy,

What the Lebans code does, is that you need to open your report and then you are supposed to feed it to the Leban function from the link, it will in turn convert it to a PDF.

Here is a sample of what I do...

Expand|Select|Wrap|Line Numbers
  1. Private Sub ReportToPDF()
  2. Dim blRet As Boolean
  3. Dim strFullPath As String
  4.  
  5.     DoCmd.OpenReport "MY_REPORT", acViewPreview, , , acHidden
  6.     strFullPath = "C:\MY_REPORT.pdf"
  7. ' Call our convert function
  8. ' Please note the last param signals whether to perform
  9. ' font embedding or not. I have turned font embedding ON for this example.
  10.     blRet = ConvertReportToPDF("MY_REPORT", vbNullString, _
  11.     strFullPath, False, False, 150, "", "", 0, 0, 0)
  12. ' To modify the above call to force the File Save Dialog to select the name and path
  13. ' for the saved PDF file simply change the ShowSaveFileDialog param to TRUE.
  14.     DoCmd.Close acDefault, "MY_REPORT", acSaveYes
  15. End Sub
  16.  
This is with the expectation that you have saved the 2 modules Leban provided in his sample DB to your DB.

Let me know if this makes sense,
AJ
Sep 23 '09 #10
NeoPa
32,556 Expert Mod 16PB
@g diddy
I hope you appreciate that doing that for you is not what we're about.

We will provide examples of what's required but we expect you to go through your own code and apply it where necessary. We don't need to see large quantities of your code in the post.
Sep 23 '09 #11
g diddy
54
Thank you for your quick response ajalwaysus I will give it a try now.
Sep 24 '09 #12
g diddy
54
you need to open your report and then you are supposed to feed it to the Leban function from the link, it will in turn convert it to a PDF.
I've changed the code with one check box for the time being to test it and I get the compile error: Sub or function not defined with the following highlighted (ConvertReportToPDF).
Expand|Select|Wrap|Line Numbers
  1.     blRet = ConvertReportToPDF("MyChoice", vbNullString, _
  2.     strFullPath, False, False, 150, "", "", 0, 0, 1)
  3.  
Just wondering where was ConvertReportToPDF defined? Or is this something that I need to change for my code?

Thanks

Best Regards
Sep 24 '09 #13
ajalwaysus
266 Expert 100+
Did you import the "modReportToPDF" and "clsCommonDialog" modules that were in the Leban database? Because you need this before you can run my code.

-AJ
Sep 24 '09 #14
g diddy
54
No I hadn't, my apologies. I have added them in and now get the error Compile Error - Exit Sub not allowed in Function or Property. Then the "Exit Sub" line is highlighted. Sorry to be a pain but I really am a novice :$ I tried removing it then I got an error saying that Macro1 wasn't there. Here is the end of the code:

Expand|Select|Wrap|Line Numbers
  1. Macro1_Exit:
  2.     Exit Sub
  3.  
  4. Macro1_Err:
  5.     MsgBox Error$
  6.     Resume Macro1_Exit
  7. Ending:
  8. End Function
  9.  
Sep 24 '09 #15
ajalwaysus
266 Expert 100+
With just a quick glance, because this is a whole lot of code, I think it is because you are using an "Exit Sub" but the final line in your code indicates this is a Function. Perhaps replacing "Exit Sub" with "Exit Function" will work.

Let me know if this works,
-AJ
Sep 24 '09 #16
NeoPa
32,556 Expert Mod 16PB
@ajalwaysus
It will. Nice catch AJ.
Sep 24 '09 #17
g diddy
54
Yes that was the problem there, school boy error by me missing that! thanks for spotting it so quickly!

I now get the error:

Expand|Select|Wrap|Line Numbers
  1. The report name 'MyChoice' you entered in either the property
  2. sheet or macro is misspelled or refers to a report that doesn't exist.
  3.  
Dim MyChoice As String is the variable and it holds the name of the report. I have a few of these variables as there are a few reports. MyReport is the name of the file when it is opened/sent/saved - it has worked with rtf so i've obviously messed up with the new code somewhere. How do you call the variable in the code?

To give you an idea of what I have for the check box I am testing:

Expand|Select|Wrap|Line Numbers
  1. *chunk of code declaring variables etc missed out*
  2. MyChoice = "Full Special Needs (Students and Invigilators) by Faculty"
  3. MyReport = "FullSpecialNeeds"
  4.     DoCmd.OpenReport "MyChoice", acViewPreview, , , acHidden
  5.     strFullPath = "C:\MyReport.pdf"
  6.     blRet = ConvertReportToPDF("MyChoice", vbNullString, _
  7.     strFullPath, False, False, 150, "", "", 0, 0, 1
  8.  
  9. * big chunk of code to do with other things missed out*
  10.  
  11.    .Body = "Find attached Report: " & MyChoice & " for School Code: " & MySchool & vbCrLf & vbCrLf
  12.     Set oAttach = .Attachments.Add("c:\" & MyReport & ".pdf")
  13.  
If I can get one working I should hopefully be able to fix the rest myself. The only problem is i'm useless at troubleshooting!!

Thanks again!!
Sep 25 '09 #18
NeoPa
32,556 Expert Mod 16PB
In lines #4 & #6 you use the string "MyChoice" instead of the variable MyChoice. Have fun.
Sep 25 '09 #19
g diddy
54
I love you

Thank you NeoPa and Ajalwaysus for all your help!!
I really appreciate it. Thank you :D
Sep 25 '09 #20
NeoPa
32,556 Expert Mod 16PB
Well, it was mainly AJ's knowledge of the PDF stuff that did the trick, but always glad to see a happy camper anyway :)
Sep 25 '09 #21
ajalwaysus
266 Expert 100+
It's an all around effort. Glad it works. =)

-AJ
Sep 25 '09 #22
gawd the hoops access and adobe make you go through to do what should be simple. complete failure on their part.
Apr 16 '12 #23

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: LarsenMTL | last post by:
I have a long running python cgi which in the end returns a link to a pdf file. While it runs it generates a log that uses stdout right into the html. I use the sys.stdout.flush() to make this log...
13
by: Jacek Dziedzic | last post by:
Hello! I have a piece of code that needs to display a formatted table of pointers using streams, with the pointers represented as hex values. It looks more or less like this: #include...
2
by: Andy | last post by:
Hi I'm really stuck outputting a double number to the console with three decimal places if the furthest right value is a zero. I can coutput the number 4.546 as 4.546 but then if I output...
2
by: cephelo | last post by:
I have no problems outputting the attribute value when the node is in context, for example, @id when an <status> node is in context. However, I am having trouble outputting it in a <xsl:value-of...
6
by: DeniseY | last post by:
I have an Access report that is created on the fly by the user selecting the fields to be included. The Access report comes out fine, but I want it to automatically output to an Excel spreadsheet....
4
by: Peter Nimmo | last post by:
Hi, I am writting a windows application that I want to be able to act as if it where a Console application in certain circumstances, such as error logging. Whilst I have nearly got it, it...
17
by: Matt | last post by:
Hello. I'm having a very strange problem that I would like ot check with you guys. Basically whenever I insert the following line into my programme to output the arguments being passed to the...
5
by: phong.lee | last post by:
Hello all, I was wondering if someone can assist me in outputting 6 reports into a pdf file? I created a macro that generates the 6 reports and right now it's saved as a snapshot on my drive. ...
12
by: billelev | last post by:
This is probably a very easy question to answer: I have been outputting some text to a message box, similar to the following: strOutput = "---" & Chr(10) & Chr(10) strOutput = strOutput &...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.