Use LeBans ReportToPDF in custom toolbar

S

Song Su

I use LeBans ReportToPDF in command button to generate PDF and it works
great.

I have so many reports and they are all in preview when a command is
clicked. I want one button on "custom toolbar" to
1. close currently previewed report
2. generate PDF from that report
and ask user if you want send as email attachment. If yes:
3. attach PDF to email

Can anyone help?

Thanks.
 
K

krissco

I use LeBans ReportToPDF in command button to generate PDF and it works
great.

I have so many reports and they are all in preview when a command is
clicked. I want one button on "custom toolbar" to
1. close currently previewed report
2. generate PDF from that report
and ask user if you want send as email attachment. If yes:
3. attach PDF to email

Can anyone help?

Thanks.

Try this:
I believe Application.Reports (Application.Reports.Count - 1) will
return a reference to the last opened report (you will want some error/
bounds checking on that for sure!).

Supply the name of the report to Lebans' function to generate the PDF.

What is the e-mail program? Outlook?

-Kris
 
S

Song Su

Hi, Kris:

Thanks for the reply. More questions on this issue

1. what kind of button should I use on the custom tool bar and that button
refer to vba code?
2. you said 'supply name of the report to LeBans function. but I want that
button to take care of any previewed report. Looks like I need a variable?
how to use variable to refer to the last previewd report?
3. yes outlook is the default mail client

- Song
 
K

krissco

1. what kind of button should I use on the custom tool bar and that button
refer to vba code?

Any kind of button. Set the OnAction to "=FunctionName()"

2. you said 'supply name of the report to LeBans function. but I want that
button to take care of any previewed report. Looks like I need a variable?
how to use variable to refer to the last previewd report?

It depends on what you want to do. Do you want to PDF the Active
report? Do you want to PDF the last opened and currently open report
(whether or not it is active)? Do you want to PDF the most recently
opened report that is currently closed?

The code in this post will take care of the Active report. It would be
easy to modify to take care of the last opened and not necessarily
active report (with the Reports() collection). I think you will need
to come up with a solution if what you want is the most recent closed
report - ouch.

That said, here is my code that, when placed on a custom toolbar,
exports the active report as PDF:

Option Compare Database
Option Explicit

'OnAction of a toolbar button should be set to
"=ExportOpenReportToPDF()"
'This will output the report as a snapshot and call Lebans' function
to convert
'the snapshot to PDF.
Public Function ExportOpenReportToPDF()
Dim blRet As Boolean

If Application.Reports.Count > 0 Then

'Clear out temp file
If safeKill(getTempSNPFile()) Then

'Leaving the 2nd argument blank exports the Active object
'(You will want to disable this function for Forms, etc.)
'This will create a snapshot file
DoCmd.OutputTo acOutputReport, , acFormatSNP,
getTempSNPFile(), False

'Clear out temp file
If safeKill(getTempPDFFile()) Then
blRet = ConvertReportToPDF(, getTempSNPFile(),
getTempPDFFile(), False, False)
End If

'Call your e-mail functions here

End If

'Remove temp files - commented so that we can see the output.
'call safekill(gettemppdffile())
'call safekill(gettempsnpfile())

End If
End Function

'Just like kill only minimizes error handling in the calling function
Private Function safeKill(strFileName As String) As Boolean
On Error GoTo safeKill_Err

If Dir(strFileName) <> "" Then Kill strFileName
safeKill = True
Exit Function

safeKill_Err:
safeKill = False
End Function

'The path/filename of the temp file we use
Private Function getTempSNPFile() As String
getTempSNPFile = CurrentProject.path & "\" & "MyTempSNP.snp"
End Function

'The path/filename of the temp file we use
Private Function getTempPDFFile() As String
getTempPDFFile = CurrentProject.path & "\" & "MyTempPDF.PDF"
End Function

3. yes outlook is the default mail client

Please look at another post of mine. Modify the code as desired
(simplify it) to send the e-mail as an attachment w/ outlook:
http://groups.google.com/group/micr...s/browse_thread/thread/3dd8386d1f0cf641?tvc=2
 
S

Song Su

I assigned the button on custom toolbar OnAction to
=ExportOpenReportToPDF()

Then, I paste your code to my code of main form. I compiled without any
error message. Now when I open report in preview and click that button,
error message is:
"The expression you entered has a function name that Phone Book (which is
the name of my application) can't find"
 
S

Song Su

I pasted into a separate module and it works fine. file generated.
however, report preview is not closed. I want report preview closed and pdf
open.
which lines to change for that purpose?
 
K

krissco

I pasted into a separate module and it works fine. file generated.

*YAY*
however, report preview is not closed. I want report preview closed and pdf
open.
which lines to change for that purpose?

Use DoCmd.Close to close the report
Change one of the "False" parameters to "True" in ConvertReportToPDF()
to open the PDF file.

-Kris
 
S

Song Su

Thanks a lot. You are wonderful!

krissco said:
Use DoCmd.Close to close the report
Change one of the "False" parameters to "True" in ConvertReportToPDF()
to open the PDF file.

-Kris
 
S

Song Su

One more thing. When I use
DoCmd.Close
it closes my main form instead of previewed report. I cannot supply object
name to DoCmd.Close as I don't know what report will be open in preview.
 
K

krissco

One more thing. When I use
DoCmd.Close
it closes my main form instead of previewed report. I cannot supply object
name to DoCmd.Close as I don't know what report will be open in preview.

Right. I just learned this:
Screen.ActiveReport will return a Report reference to the - what else
- active report!
You should be able to specify Screen.ActiveReport.Name as the string
parameter for the report you want to close. For that matter, use this
method of obtaining the report name for the DoCmd.OutputTo command (we
left it blank before).

MS Help says that Screen.ActiveReport throws an error when there is no
active report - you will want to trap for that.

-Kris
 
S

Song Su

I'm new in these area. Can you help with the code?
1. where to put Screen.ActiveReport?
2. how to modify DoCmd.OutputTo acOutputReport, , acFormatSNP,
getTempSNPFile(), False?
3. how to trap?
 
K

krissco

1. where to put Screen.ActiveReport?
2. how to modify DoCmd.OutputTo acOutputReport, , acFormatSNP,
getTempSNPFile(), False?

DoCmd.OutputTo acOutputReport, , acFormatSNP, getTempSNPFile(), False
^
|
|
We left the second argument blank before.

Leaving the "Object Name" blank means "Active object" - not
necessarily the active report. It will be safer to explicitly list the
name of the active report. Try this:

DoCmd.OutputTo acOutputReport, Screen.ActiveReport.Name, acFormatSNP,
getTempSNPFile(), False

You also want to use this way of grabbing the active report name when
running DoCmd.Close().

3. how to trap?

Make the changes, then try calling the function with NO open reports
(save first please). The program will bomb when you try to reference
Screen.ActiveReport.Name.

Figure out what error is generated by calling Debug.Print Err.Number.

Look at my error handling for safeKill. You should always set up error
handling for your functions - even if it is basic stuff. Try something
like this:

Public Function ExportOpenReportToPDF()
on error goto ExportOpenReportToPDF_Err
.. . .
.. . .
.. . .
ExportOpenReportToPDF_Err:
if err.number = {Whatever error is generated by calling
Screen.ActiveReport.Name with no active report} then
msgbox "You gotta have a report open first!"
else
msgbox "Unhandled error# " & err.number & vbcrlf &
"Description: " & err.description
end if
end function

-Kris
 
S

Song Su

Hi, Kris,

Everything is working now. Thank you very much and it's very helpful and
educational session.

- Song
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top