locked
Export report to pdf with unique name - but not save RRS feed

  • Question

  • it was hard to title this question...  OutputTo   pdf

    rptTest

    If one leaves the file name blank in the OutputTo  it triggers the opening of the File Explorer  - - which is what is wanted - so the user can save it in whatever folder they seek...

    but in this case the pdf's name is the object name: rptTest - - which is not wanted (though of course can be manually changed which is not wanted either...auto title the file from data in the app with a date stamp would be nice...)

    To build a unique name - is easy enough - but it would go into File Name clause - which in turn requires a path and thus no File Explorer, plus saves it somewhere...which is not the process desired.... 

    I can think of some kludgy things to do such as copy/change report name or pdf file.....but I think I've done this before using a method of some sort but can't seem to get a handle on it at the moment....I was thinking the report's caption would flow to be the output PDF's name but my test doesn't find that to be so....

    Saturday, July 18, 2020 2:19 PM

Answers

  • Got it.  Here's the code:

     Dim strPath As String
       strPath = "C:\"
        Dim strFile As String
        strFile = "Blahblah.pdf"
        With Application.FileDialog(2) ' msoFileDialogFolderPicker
              .InitialFileName = strFile
            If .Show Then
                strPath = .SelectedItems(1)
            Else
                Beep
                Exit Sub
            End If
        End With
      ' If Right(strPath, 1) <> "\" Then
      '     strPath = strPath & "\"
      ' End If
        DoCmd.OutputTo _
            ObjectType:=acOutputReport, _
            ObjectName:="ContractEntry", _
            OutputFormat:=acFormatPDF, _
            OutputFile:=strPath

    what's different from original from HV is:

     .InitialFileName = strFile

    deactivated the tag on of the \ symbol

    removed the strFile variable from the final Outputfile line

    this then opens Windows File Explorer with a custom name already entered into the File Name box

    Saturday, July 18, 2020 4:19 PM
  • I'm glad you came up with something you like, but I hope you're aware that the user can change both the path AND the file name in your version.

    Regards, Hans Vogelaar (https://www.eileenslounge.com)

    Saturday, July 18, 2020 6:39 PM

All replies

  • You could use something like this:

    Sub Test()
        Dim strPath As String
        Dim strFile As String
        strFile = "Blahblah.pdf"
        With Application.FileDialog(4) ' msoFileDialogFolderPicker
            If .Show Then
                strPath = .SelectedItems(1)
            Else
                Beep
                Exit Sub
            End If
        End With
        If Right(strPath, 1) <> "\" Then
            strPath = strPath & "\"
        End If
        DoCmd.OutputTo _
            ObjectType:=acOutputReport, _
            ObjectName:="rptTest", _
            OutputFormat:=acFormatPDF, _
            OutputFile:=strPath & strFile
    End Sub


    Regards, Hans Vogelaar (https://www.eileenslounge.com)

    Saturday, July 18, 2020 3:02 PM
  • copied in ....only changed the rpt name....

    it is a directory explorer where the button says Directory....    rather than OK..... and instead of File Name text box it is Folder Name text box....

    I see your comment    -  I guess I need a different dialog maybe did not explain well - seeking to save the uniquely auto named file to user chosen folder....

    also  no name was in the file/folder text box of Windows Explorer - was expecting blahblah....

    ' msoFileDialogFolderPicker
    Saturday, July 18, 2020 3:21 PM
  • changed it to 2 and is the correct File Dialog

    but blahblah is not showing as the file name - this text box is blank...

    if I enter in any value it treats it as a directory....won't allow save if left blank


    Saturday, July 18, 2020 3:28 PM
  • Got it.  Here's the code:

     Dim strPath As String
       strPath = "C:\"
        Dim strFile As String
        strFile = "Blahblah.pdf"
        With Application.FileDialog(2) ' msoFileDialogFolderPicker
              .InitialFileName = strFile
            If .Show Then
                strPath = .SelectedItems(1)
            Else
                Beep
                Exit Sub
            End If
        End With
      ' If Right(strPath, 1) <> "\" Then
      '     strPath = strPath & "\"
      ' End If
        DoCmd.OutputTo _
            ObjectType:=acOutputReport, _
            ObjectName:="ContractEntry", _
            OutputFormat:=acFormatPDF, _
            OutputFile:=strPath

    what's different from original from HV is:

     .InitialFileName = strFile

    deactivated the tag on of the \ symbol

    removed the strFile variable from the final Outputfile line

    this then opens Windows File Explorer with a custom name already entered into the File Name box

    Saturday, July 18, 2020 4:19 PM
  • I'm glad you came up with something you like, but I hope you're aware that the user can change both the path AND the file name in your version.

    Regards, Hans Vogelaar (https://www.eileenslounge.com)

    Saturday, July 18, 2020 6:39 PM
  • Thank you for taking the time to make that point.  While it is not an issue in this case, I better understand your original code now.  It was only a folder selector, keeping the file name away from the user.  I can see how in some cases that would be a key issue.  thanks again
    Saturday, July 18, 2020 7:37 PM