Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2012
    Posts
    68

    Unanswered: print report selected in list box to pdf file

    So i currently have this code in place.

    Code:
    Private Sub printreport_Click()
    
    For Each varSelected In Me.reportlist.ItemsSelected
    DoCmd.OpenReport Me.reportlist.ItemData(varSelected), acPrint
    
    
    Next varSelected
    
    
    End Sub
    Which prints the report. I want to have the report print to a pdf and save to a certain location given a certain name. I have tried playing with DoCmd.Output, but that seems to only let me reference 1 Object name. Is there a way to establish the object name using the items selected? Help's appreciated.

  2. #2
    Join Date
    May 2005
    Location
    Nevada, USA
    Posts
    2,888
    Provided Answers: 6
    You should be able to pass the value from the listbox for the report name, just as you are here. You can also dynamically set the file name. I have this in a client's db I happen to have open:

    DoCmd.OutputTo acOutputReport, "ReportName", acFormatPDF, "C:\intacc\" & strFileName & ".pdf"

    Where strFileName contains the desired file name.
    Paul

  3. #3
    Join Date
    Mar 2012
    Posts
    68
    Something like this maybe?

    Code:
    Private Sub printreport_Click()
    
        Dim varSelected As Variant
        Dim strFileName As String
        
        For Each varSelected In Me.reportlist.ItemsSelected
        DoCmd.OutputTo acOutputReport, Me.reportlist.ItemData(varSelected), acFormatPDF, "J:\Job Transfers\" & strFileName & ".pdf"
        Next varSelected
        
        strFileName = Me.reportdate
        
        
    End Sub

  4. #4
    Join Date
    May 2005
    Location
    Nevada, USA
    Posts
    2,888
    Provided Answers: 6
    You'd want to set the file name before the output line, and that would name them all the same, so you'd only end up with the last one. I'd assume you'd want to incorporate the report name or something.
    Paul

  5. #5
    Join Date
    Mar 2012
    Posts
    68
    I'd actually like to name them based on a value in a textbox. Hence the strFileName = Me.reportdate.

  6. #6
    Join Date
    Mar 2012
    Posts
    68
    Here's the code:
    Code:
    Private Sub printreport_Click()
    
        Dim varSelected As Variant
        Dim strFileName As String
    
        strFileName = Me.reportdate
    
        
        For Each varSelected In Me.reportlist.ItemsSelected
            DoCmd.OutputTo acOutputReport, Me.reportlist.ItemData(varSelected), acFormatPDF, "J:\Job Transfers\" & strFileName & ".pdf"
        Next varSelected
        
    End Sub
    and here is the error
    " runtime 2501
    The Output to action was cancelled"

    On this line:

    Code:
    DoCmd.OutputTo acOutputReport, Me.reportlist.ItemData(varSelected), acFormatPDF, "J:\Job Transfers\" & strFileName & ".pdf"

  7. #7
    Join Date
    Mar 2012
    Posts
    68
    Quote Originally Posted by pbaldy View Post
    You'd want to set the file name before the output line, and that would name them all the same, so you'd only end up with the last one. I'd assume you'd want to incorporate the report name or something.
    Should i include hashtagsfor me.reportdate. For example strFileName = " # me.reportdate # " ?

  8. #8
    Join Date
    May 2005
    Location
    Nevada, USA
    Posts
    2,888
    Provided Answers: 6
    No, you should probably use the Format() function on it, else you're probably getting "/" in it, which you don't want in a file name.

    Format(me.reportdate, "yyyymmdd")
    Paul

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •