Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    miicker is offline Novice
    Windows 10 Access 2016
    Join Date
    Feb 2013
    Posts
    18

    Export to PDF variable filename (prompt for location)

    Hello everyone,



    I have a report which is an invoice, there is a field called InvoiceNr, and I want the filename to be the InvoiceNr. When I use code like "DoCmd.OutputTo" the filename is the same as report name.
    The only way to do this is to specify the "Output File", but then I have to specify the whole location, and I want the user to be prompted for the location, but that the name is changed to the Invoice Number.

    Changing the caption of the form doesn't work.

    Hopefully you guys can help me.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    There's a hole....if you let the user enter the folder, then they can also change the name, thus ruining your file.

  3. #3
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,917
    You may find this link useful: http://access.mvps.org/access/api/api0002.htm

  4. #4
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    code to save the pdf
    usage: SaveFile "inv1542"

    Code:
    Public Sub SaveFile(ByVal pvInvoice)
    Dim vDir, vFile
    Dim i As Integer
    
    vDir = "c:\"
    vFile = UserPickFile(vDir)
    If vFile <> "" Then
      i = InStrRev(vFile, "\")
      If i > 0 Then
        vDir = Left(vFile, i) 'the folder
        vFile = vDir & pvInvoice
        DoCmd.OutputTo acOutputReport, "report", acFormatPDF, vFile
      Else
         MsgBox "no file saved"
      End If
    End If
    End Sub
    
    Public Function UserPickFile(Optional pvPath)
    Dim strTable As String
    Dim strfilepath As String
    Dim sDialog As String, sDecr  As String, sExt As String
    
    If IsMissing(pvPath) Then pvPath = "c:\"
    
    With Application.FileDialog(msoFileDialogFilePicker)       'MUST ADD REFERENCE : Microsoft Office 11.0 Object Library
        .AllowMultiSelect = False
        .Title = "Locate a file to Import"
        .ButtonName = "Import"
        .Filters.Clear
        .Filters.Add "All Files", "*.*"
        .InitialFileName = pvPath
        .InitialView = msoFileDialogViewList    'msoFileDialogViewThumbnail
        ''.AllowMultiSelect = True
         
            If .show = 0 Then
               'There is a problem
               Exit Function
            End If
        
        'Save the first file selected
        UserPickFile = Trim(.SelectedItems(1))
    End With
    End Function

  5. #5
    miicker is offline Novice
    Windows 10 Access 2016
    Join Date
    Feb 2013
    Posts
    18
    Quote Originally Posted by ranman256 View Post
    There's a hole....if you let the user enter the folder, then they can also change the name, thus ruining your file.
    It's not a problem that the user can change the filename, I just want the default filename to be the invoice number, not "rptInvoice"

    I will try the code in a few hours.

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    I would probably do something like the following. There is not any need to add references. The default references should include the Office Object thing.

    Code:
    Dim strPath As String
    Dim strFileName As String
        With Application.FileDialog(msoFileDialogFolderPicker)
        
            .Title = "Locate an export to location"
            .ButtonName = "Choose"
            .InitialView = msoFileDialogViewThumbnail
            
                If .Show = 0 Then
                    'TODO create some error handle stuff here
                    Exit Sub
                End If
                
            strPath = Trim(.SelectedItems(1))
            
        End With
    strFileName = "MyFileName.pdf"
    DoCmd.OutputTo acOutputReport, "ReportName", acFormatPDF, strPath & "\" & strFileName

  7. #7
    miicker is offline Novice
    Windows 10 Access 2016
    Join Date
    Feb 2013
    Posts
    18
    Quote Originally Posted by miicker View Post
    It's not a problem that the user can change the filename, I just want the default filename to be the invoice number, not "rptInvoice"

    I will try the code in a few hours.
    When I run your code i get the following error:
    Code:
    Run-time error '-2147467259 (80004005)'
    
    Method 'FileDialog' of object '_Application' failed.
    When I hit debug it marks this line:
    Code:
    With Application.FileDialog(msoFileDialogFolderPicker)
    When I looked at the Microsoft website I found this:
    ' Requires reference to Microsoft Office 11.0 Object Library.
    How do I do this in Access 2016?

  8. #8
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    What code are you referring to?

    You can add references via the VBA editor. Tools>References
    If you are using Access 2010, the version number will be 14

  9. #9
    miicker is offline Novice
    Windows 10 Access 2016
    Join Date
    Feb 2013
    Posts
    18
    I'm referring to your code.
    Thanks i'm using Access 2016, and enabled number 16, and that worked.

    I'm not verry expirienced with VBA, so the code ranman provided does not make any sense to me, I don't know how to use it, sorry.

    With the code that was provided by "ItsMe" its working now, the user gets prompted for the location.

    Two questions:
    Is it possible to set a default location (for exemple, the windows that lets the user select the location opens My Documents istead of My Computer?)
    Is it possible to give the user an message if the file already exists, now the file just gets overwritten.

  10. #10
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    In my example, I am using the folder picker thing vs. the file picker. I believe the default folder is the initial view property. You would add it with the other assignments within the With block of code.

    Code:
            .Title = "Locate an export to location"
            .InitialView = \\ServerName\FolderName
            .ButtonName = "Choose"
    To get the path for the User's desktop you could use Window's Special Folder thing.
    Code:
    Dim strPath As String
    strPath = CreateObject("WScript.Shell").SpecialFolders("MyDocuments")

    To avoid overwriting a file, you can do something like the following.
    Code:
    If Dir(strPath & "\" & strFileName, vbDirectory) <> "" Then
    'perform the Outputo
    else
    msgbox "That report already exists!"
    end if

  11. #11
    miicker is offline Novice
    Windows 10 Access 2016
    Join Date
    Feb 2013
    Posts
    18
    Quote Originally Posted by ItsMe View Post
    In my example, I am using the folder picker thing vs. the file picker. I believe the default folder is the initial view property. You would add it with the other assignments within the With block of code.

    Code:
            .Title = "Locate an export to location"
            .InitialView = \\ServerName\FolderName
            .ButtonName = "Choose"
    This code does not work for me. How can I fix this? (I changed the servername to the server I use).

    I am now using the following code:
    Code:
    Private Sub ButtonPDF_Click()
    
    Dim strPath As String
    Dim strFileName As String
    Dim ReportName As String
    Dim strBeginPath As String
    ReportName = Me.Name
    
    
        With Application.FileDialog(msoFileDialogFolderPicker)
    
    
            .Title = "Locate an export to location"
            .InitialView = msoFileDialogViewThumbnail
            .ButtonName = "Choose"
    
    
    
    
                If .Show = 0 Then
                    'TODO create some error handle stuff here
                    Exit Sub
                End If
    
    
            strPath = Trim(.SelectedItems(1))
    
    
        End With
    
    
    strFileName = [pdfName]
    
    
    If Dir(strPath & "\" & strFileName & ".pdf", vbDirectory) <> "" Then
        Dim LResponse As Integer
        LResponse = MsgBox("File already exists, do you want to overwrite the file?", vbYesNo, "Warning")
            If LResponse = vbYes Then
                DoCmd.OutputTo acOutputReport, ReportName, acFormatPDF, strPath & "\" & strFileName & ".pdf"
            Else
            End If
        Else
        DoCmd.OutputTo acOutputReport, ReportName, acFormatPDF, strPath & "\" & strFileName & ".pdf"
    End If
    
    
    End Sub
    This code gives the user the warning that the file already exists and asks to overwrite the file.

  12. #12
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    What is the name of the file you are trying to use? Is it [pdfName]?

    You need to include the file extension, .pdf. Also, are you really using brackets around the file name? I suppose this is a valid file name. However, Windows and or VBA may not appreciate the special characters.

  13. #13
    miicker is offline Novice
    Windows 10 Access 2016
    Join Date
    Feb 2013
    Posts
    18
    Quote Originally Posted by ItsMe View Post
    What is the name of the file you are trying to use? Is it [pdfName]?


    You need to include the file extension, .pdf. Also, are you really using brackets around the file name? I suppose this is a valid file name. However, Windows and or VBA may not appreciate the special characters.
    I use a field in the report as name, the name of that field is [pdfName], and i declair it as a variable in:
    strFileName = [pdfNaam]

    I give it the file extension in:
    DoCmd.OutputTo acOutputReport, ReportName, acFormatPDF, strPath & "\" & strFileName & ".pdf"


    The code works just fine, I just want to change the default location (that is now just My Pc).

  14. #14
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    The code works just fine, I just want to change the default location (that is now just My Pc).
    I had made a mistake when I guesed the property for default folder. It is the same as it is for msoFileDialogFilePicker. So the following settings should ensure that it defaults to the User's MyDocuments folder.
    Code:
            .Title = "Locate an export to location"
            .ButtonName = "Choose"
            .InitialView = msoFileDialogViewThumbnail
            .InitialFileName = CreateObject("WScript.Shell").SpecialFolders("MyDocuments")

  15. #15
    miicker is offline Novice
    Windows 10 Access 2016
    Join Date
    Feb 2013
    Posts
    18
    Quote Originally Posted by ItsMe View Post
    I had made a mistake when I guesed the property for default folder. It is the same as it is for msoFileDialogFilePicker. So the following settings should ensure that it defaults to the User's MyDocuments folder.
    Code:
            .Title = "Locate an export to location"
            .ButtonName = "Choose"
            .InitialView = msoFileDialogViewThumbnail
            .InitialFileName = CreateObject("WScript.Shell").SpecialFolders("MyDocuments")
    Thank you! That did the trick, thank you very much for your help!

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Export table to txt file with a variable filename
    By macollins7 in forum Import/Export Data
    Replies: 6
    Last Post: 07-12-2012, 09:44 AM
  2. Replies: 3
    Last Post: 06-02-2011, 02:08 PM
  3. Variuse FileName When Export
    By shay in forum Import/Export Data
    Replies: 1
    Last Post: 12-01-2010, 11:36 AM
  4. Prompt for filename for import/export??
    By stephenaa5 in forum Import/Export Data
    Replies: 23
    Last Post: 10-23-2009, 03:43 PM
  5. Replies: 0
    Last Post: 06-11-2009, 01:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums