473,382 Members | 1,766 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,382 software developers and data experts.

Create pdf from report using Cutepdf by means of VBA

TheSmileyCoder
2,322 Expert Mod 2GB
I would like whenever a report is created by a user in my db to store the report as a pdf on a network location, without showing/troubling the user.

Anyone know if this can be done, and hopefully how?

Im currently using CutePDF, but any freeware pdf-writer could be an option.


I would need to be able to "feed" the cutePDF writer (or similar) with the desired filename.
Jan 15 '10 #1

✓ answered by ADezii

I wrote an Off-the-Wall routine that should enable you to Auto Fill the Save As Dialog with a Unique Filename and Save it to the destination of your choice. You can change Lines 1, 8, and 11 to suite your specific needs.
Expand|Select|Wrap|Line Numbers
  1. Const conPATH_TO_PDF As String = "C:\Stuff\"
  2. Dim strPathToPDF As String
  3. Dim strReportName As String
  4.  
  5. 'Changes the Default Printer, then resets it to the original Printer.
  6. 'This will only work if the report is set up to Print to the Default
  7. 'Printer
  8. Set Application.Printer = Application.Printers("PDF Converter")
  9.  
  10. 'Report Name assign to Variable
  11. strReportName = "rptPDF"
  12.  
  13. 'Make the *.PDF File Unique
  14. strPathToPDF = conPATH_TO_PDF & strReportName & Format$(Now(), "yyyymmddhhmmss") & ".pdf"
  15.  
  16. DoCmd.OpenReport strReportName, acViewNormal, , , acWindowNormal
  17.  
  18. 'Fill in the Absolute/Unique Path in the Save As Dialog
  19. SendKeys strPathToPDF, True
  20.  
  21. 'TAB to the Save Command Button
  22. SendKeys "{TAB 2}", True
  23.  
  24. 'Simulate Clicking the Save Key
  25. SendKeys "{ENTER}", True
  26. SendKeys "{ENTER}", True
  27.  
  28. 'Reset the Default Printer back to the way it was
  29. Set Application.Printer = Nothing

8 16277
nico5038
3,080 Expert 2GB
From the CutePDF FAQ:
- How to bypass Save As dialog box ,or make unattended installation?
The Custom Edition supports those features.

This custom edition will however cost some $$'s.

Basically you need an application that can be controlled by the SHELL() command.

Nic;o)
Jan 15 '10 #2
ADezii
8,834 Expert 8TB
I wrote an Off-the-Wall routine that should enable you to Auto Fill the Save As Dialog with a Unique Filename and Save it to the destination of your choice. You can change Lines 1, 8, and 11 to suite your specific needs.
Expand|Select|Wrap|Line Numbers
  1. Const conPATH_TO_PDF As String = "C:\Stuff\"
  2. Dim strPathToPDF As String
  3. Dim strReportName As String
  4.  
  5. 'Changes the Default Printer, then resets it to the original Printer.
  6. 'This will only work if the report is set up to Print to the Default
  7. 'Printer
  8. Set Application.Printer = Application.Printers("PDF Converter")
  9.  
  10. 'Report Name assign to Variable
  11. strReportName = "rptPDF"
  12.  
  13. 'Make the *.PDF File Unique
  14. strPathToPDF = conPATH_TO_PDF & strReportName & Format$(Now(), "yyyymmddhhmmss") & ".pdf"
  15.  
  16. DoCmd.OpenReport strReportName, acViewNormal, , , acWindowNormal
  17.  
  18. 'Fill in the Absolute/Unique Path in the Save As Dialog
  19. SendKeys strPathToPDF, True
  20.  
  21. 'TAB to the Save Command Button
  22. SendKeys "{TAB 2}", True
  23.  
  24. 'Simulate Clicking the Save Key
  25. SendKeys "{ENTER}", True
  26. SendKeys "{ENTER}", True
  27.  
  28. 'Reset the Default Printer back to the way it was
  29. Set Application.Printer = Nothing
Jan 16 '10 #3
TheSmileyCoder
2,322 Expert Mod 2GB
NICE!

Simple, but impressive. Looking forward to testing it.
Jan 16 '10 #4
ADezii
8,834 Expert 8TB
It was actually functional on my end, I guess anything is possible! (LOL)

P.S. - The strange part is that the first
Expand|Select|Wrap|Line Numbers
  1. SendKeys "{ENTER}", True
appears to be ignored.
Jan 16 '10 #5
nico5038
3,080 Expert 2GB
I'm a bit reluctant to use the SendKeys, as they might get interrupted / send to another application when the user clicks somewhere on the screen...
There's never a 100% guarantee that it's finished correctly, so you need to make sure your application offers a "re-try".

Nic;o)
Jan 17 '10 #6
ADezii
8,834 Expert 8TB
Hello Nico, I would imagine that the chances of a User clicking somewhere else on the Screen after clicking a Command Button to initiate code execution would be infinitesimal, but to be absolutely sure, you can suppress all forms of Keyboard/Mouse Input prior to executing the code, then enable Input again post execution. Refer to Code Lines 4 and 32.

P.S. - Error Checking intentionally omitted.
Expand|Select|Wrap|Line Numbers
  1. Public Declare Function BlockInput Lib "USER32.DLL" (ByVal fBlockIt As Long) As Long
Const conPATH_TO_PDF As String = "C:\Stuff\"
Expand|Select|Wrap|Line Numbers
  1. Dim strPathToPDF As String
  2. Dim strReportName As String
  3.  
  4. BlockInput True
  5.  
  6. 'Changes the Default Printer, then resets it to the original Printer.
  7. 'This will only work if the report is set up to Print to the Default
  8. 'Printer
  9. Set Application.Printer = Application.Printers("PDF Converter")
  10.  
  11. 'Report Name assign to Variable
  12. strReportName = "rptPDF"
  13.  
  14. 'Make the *.PDF File Unique
  15. strPathToPDF = conPATH_TO_PDF & strReportName & Format$(Now(), "yyyymmddhhmmss") & ".pdf"
  16.  
  17. DoCmd.OpenReport strReportName, acViewNormal, , , acWindowNormal
  18.  
  19. 'Fill in the Absolute/Unique Path in the Save As Dialog
  20. SendKeys strPathToPDF, True
  21.  
  22. 'TAB to the Save Command Button
  23. SendKeys "{TAB 2}", True
  24.  
  25. 'Simulate Clicking the Save Key
  26. SendKeys "{ENTER}", True
  27. SendKeys "{ENTER}", True
  28.  
  29. 'Reset the Default Printer back to the way it was
  30. Set Application.Printer = Nothing
  31.  
  32. BlockInput False
Jan 17 '10 #7
TheSmileyCoder
2,322 Expert Mod 2GB
Well I use a DB that creates (and formats) 100 page long documents (Requirement specifications), as well as doing crossreferences, image formatting, and creating a Table of contents, so it takes around 30 seconds for my code to generate the report. I imagine sending it to the CutePDF writer is another 5 seconds.

What would happen if an error occured while you have blocked input? Would you be "stuck"?
I presume you could add a error handler like:

Expand|Select|Wrap|Line Numbers
  1. Public Sub GenereatePDF()
  2. On error goto Err_Handler
  3. BlockInput True
  4.  
  5. /Code code code
  6.  
  7.  
  8. Err_Handler:
  9. BlockInput False 
  10.  
Jan 17 '10 #8
ADezii
8,834 Expert 8TB
Hello TheSmileyOne, you answered your own question. Please be advised that this approach is a little 'HAIRY' since it is usually never a good idea to block all forms of Input into an Application. I do feel, however, with careful coding and a little User Training, it can be practical. The decision is all yours.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command13_Click()
  2. On Error GoTo Err_Command13_Click:
  3. Const conPATH_TO_PDF As String = "C:\Stuff\"
  4. Dim strPathToPDF As String
  5. Dim strReportName As String
  6.  
  7. BlockInput True
  8.  
  9. 'Changes the Default Printer, then resets it to the original Printer.
  10. 'This will only work if the report is set up to Print to the Default
  11. 'Printer
  12. Set Application.Printer = Application.Printers("PDF Converter")
  13.  
  14. 'Report Name assign to Variable
  15. strReportName = "rptPDF"
  16.  
  17. 'Make the *.PDF File Unique
  18. strPathToPDF = conPATH_TO_PDF & strReportName & Format$(Now(), "yyyymmddhhmmss") & ".pdf"
  19.  
  20. DoCmd.OpenReport strReportName, acViewNormal, , , acWindowNormal
  21.  
  22. 'Fill in the Absolute/Unique Path in the Save As Dialog
  23. SendKeys strPathToPDF, True
  24.  
  25. 'TAB to the Save Command Button
  26. SendKeys "{TAB 2}", True
  27.  
  28. 'Simulate Clicking the Save Key
  29. SendKeys "{ENTER}", True
  30. SendKeys "{ENTER}", True
  31.  
  32. 'Reset the Default Printer back to the way it was
  33. Set Application.Printer = Nothing
  34.  
  35. BlockInput False
  36.  
  37. Exit_Command13_Click:
  38.   Exit Sub
  39.  
  40. Err_Command13_Click:
  41.   BlockInput False
  42.     MsgBox Err.Description, vbExclamation, "Error in Command13_Click()"
  43.       Resume Exit_Command13_Click
  44. End Sub
Jan 17 '10 #9

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

Similar topics

1
by: JSB | last post by:
I am new to Access and am hoping an Access guru out there can help me. I am using an Access 2002 project to report on a SQL Server database. I am reporting various information for around 100...
1
by: Aby Mathews | last post by:
I am using Cutepdf to convert access report to pdf format. It's working fine. How can I set the save location and save as file by default and how can I attach this file automatically to outlook....
2
by: Mike Alpha | last post by:
I'm using Access 2000 and I can't seem to get it to send just one page of a report to Outlook Express. It will only send the whole report. I right click on Send To Mail Recipient and the Send box...
12
by: DGB | last post by:
Hello All, I have been doing quite a bit of research in this group and have not been able to find the answer that I am looking for. Here is my issue. We currently have Adobe Acrobat v5...
13
by: salad | last post by:
Hi Guys: I was stuck. I needed to send a report to a file. My beautiful report(s) in Access were going to require loss of formatting with RTFs, a PITA in WordMailMerge, sending it as a text...
6
by: Bluejay906 | last post by:
I am trying to upgrade a squirrelly Access app. I need to change a button on a form to run VBA instead of a macro, because the user wants to be able to print the report to a printer or to a CutePDF....
8
by: Good Man | last post by:
Hi there We've found a fantastic solution for generating PDF documents with fairly complex XHTML and CSS. It does a great job (on all our tests so far...) It's called "Prince" (...
4
by: mehrhardt | last post by:
My situtation: I'm not very good at Access. I've created a report (R_SalesHist) listing 1000 customers sales history. Each customer's sales history fits nicely on a single page. When I run...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.