473,796 Members | 2,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Outputting reports as PDF

54 New Member
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
22 9322
NeoPa
32,579 Recognized Expert Moderator MVP
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 Recognized Expert Contributor
It's an all around effort. Glad it works. =)

-AJ
Sep 25 '09 #22
blah DeBlah
9 New Member
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
2547
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 output relatively realtime. The log, however, has grown too long. I wish to output it instead into a textarea. I thought I could just use the textarea tags before the output starts. This works, but my output is no longer "real time" even...
13
2793
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 <iostream> #include <iomanip> using namespace std;
2
2776
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 0.220 it comes out as 0.22 and drops the zero. Also, outputting 1.000 outputs as 1. How can I format it to include the zeros?
2
1477
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 /> element: <xsl:value-of select="procresults/settings@priority /> where it looks like
6
5229
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. Again, I have this part working. But the fields in the resulting spreadsheet are in a different order than the Access report. (Example: The fields in the Access report might go FirstName, LastName,Address,City--in the Excel spreadsheet, they come up...
4
10518
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 doesn't seem to write to the screen in the way I would expect. The output is:
17
3383
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 programme: printf("\nCommand line arguement %d: %s.", i , argv ); The porgramme outputs 3 of the command line arguements, then gives a segmentation fault on the next line, followed by other strange
5
2554
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. Any ideals or sample will be greatly appreciated.
12
13430
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 & "VAR:" & AddTabs(1) & Format(rsVar!, "currency") strOutput = strOutput & Chr(10) & Chr(10) strOutput = strOutput & "Position Value:" & AddTabs(1) & Format(rsVar!, "currency") & Chr(10) strOutput = strOutput & "Position Risk:" & AddTabs(1) &...
0
10461
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10239
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10019
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7555
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6796
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4122
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.