Send Current Object to PDF

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32661

    Send Current Object to PDF

    Hello everyone.

    I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).

    I know it can be done by selecting :
    1. External Data (Ribbon)
    2. PDF or XPS (from the Export Group)
    3. Select all desired options
    4. Publish

    That's fine, but how to get this done in VBA?

    I want to be clear that I don't need (or even want really) a whole bunch of code that does the whole job for me. I just need a pointer to what Method of DoCmd (or whatever) can be used to get this done. I am though, happy to sift through any code for the nugget if that's what you have to offer.

    Last, but not least, I've suggested elsewhere that all new questions should come with isladogs' Access/Windows/Office365 Version Checker info attached, so here's mine :

    Code:
    [B][U]Access / Windows / Office Version Info[/U][/B]
    Date/Time:              15/10/2023 17:56:25
    Workstation Name:       BELLPC10
    Operating System:       Windows 10 Enterprise Version 22H2 - Build 10.0.19045.3448 64-bit
    Access Full Version:    Access 2019 Version 2308 - Build 16.0.16731.20234 64-bit
    Access Path:            C:\Program Files\Microsoft Office\root\Office16\
    Office 365 Installed:   No
    Office Install Method:  Click-to-Run
    Update Channel:         Not applicable
    All offerings are received gratefully, as I appreciate any time expended regardless of how successfully helpful it may prove.
    Last edited by NeoPa; Oct 16 '23, 04:55 PM.
  • DJRhino
    New Member
    • Feb 2015
    • 107

    #2
    NeoPa,

    Code:
    Dim strReportName   As String
    Dim myReportWhere   As String
    Dim myOutPutFile    As String
    strReportName = Replace(Replace("REJECT TAG#|1 PN |2", "|1", Nz(" " & [REJECT TAG NUMBER], " ")), "|2", Nz([Part Number] & " " & [Descriptive Reason], " "))
                
    myReportWhere = "[REJECT TAG NUMBER] = [Forms]![Reject Tag Form Edit]![REJECT TAG NUMBER]"
    
    DoCmd.OpenReport "Reject Tag Report", acViewPreview, , myReportWhere
    
    myOutPutFile = "M:\Inspect\New Reject Tag Database\Reports" & "\" & strReportName & ".PDF"
    
    DoCmd.OutputTo acOutputReport, "Reject Tag Report", acFormatPDF, myOutPutFile, False, acExportQualityPrint
    
    DoCmd.Close acReport, "Reject Tag Report", acSaveNo
    This is how I do it...Sample of the code I use...Hope this is what you were looking for....

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32661

      #3
      Hi Rhino.

      It actually is what I was looking for. I was preparing a post to say "Thank you, but no." as it's something I already found & checked, but the secret (that I totally missed) was that it could, after all, be used to work with the active object as long as the [ObjectName] parameter is left blank. (And, as I subsequently discovered, even when it's not. See post #5)

      So thank you. You've pointed me in the right direction, and for that I'm grateful :-)
      Last edited by NeoPa; Oct 16 '23, 04:53 PM.

      Comment

      • DJRhino
        New Member
        • Feb 2015
        • 107

        #4
        NeoPa,

        Glad I could help, whether Directly or indirectly. This site has helped me so much.

        Rhino

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32661

          #5
          Hi Rhino.

          As a follow on, because I know this can cause issues for people sometimes, I would just explain that when used with [ObjectName:="So mething" (or even just passing it as the second parameter, as you have - which is [ObjectName]), and that object name happens to refer to an object that is already open in your Access window, whether currently selected or otherwise, what will be output is the version of the object that is already open.

          So, if you have a report called rptA, and this report is based on a table (called tblB) with 100 records and you execute the following code without first opening rptA, you will see the full report including each of the 100 records :
          Code:
          Call DoCmd.OutputTo(ObjectType:=acOutputReport _
                            , ObjectName:="rptA" _
                            , OutputFormat:=acFormatPDF _
                            , OutputFile:="C:\Temp\rptA.Pdf" _
                            , AutoStart:=True)
          However, if first you open rptA for preview, and filter it such that only two records are included, then regardless of how many, or which, other objects you open as well, executing the code above will, instead, show just what you can see in the preview on the screen.

          In case there are any who are so unused to using or seeing procedure calls done using the named parameters as I have shown above, here is a version that makes exactly the same call, but using positional parameters instead :
          Code:
          DoCmd.OutputTo acOutputReport, "rptA", acFormatPDF, OutputFile:="C:\Temp\rptA.Pdf", True
          Last edited by NeoPa; Oct 16 '23, 04:49 PM.

          Comment

          Working...