Results 1 to 4 of 4
  1. #1
    cbrsix is offline Competent Performer
    Windows 11 Office 365
    Join Date
    Jul 2011
    Posts
    215

    Environ Functions - Save an export to any desktop

    Hi,



    I have the below code that is used to generate a report, save it as a PDF to a specific users desktop, and to print it out. Now more than one person is responsible for this activity. The problem I am running into is that the OutputTo code is specific to only the original users desktop. I want to allow any user to perform this duty from their own computer. I have potentially found a solution by utilizing Environmental functions. However, I have no experience with them and am not exactly sure how to properly integrate it into my current code.


    Code:
    Private Sub cmdPrint_Click()
    
    Dim dbs As Object
    Dim rst As Object
    Dim strSql As String
    Dim strWhere As String
    
    strSql = "Account36MonthList" 'Select the Query where you want your information to be drawn from
    
    Set dbs = CurrentDb
    Set rst = dbs.OpenRecordset(strSql)
    
        rst.MoveFirst
        While Not rst.EOF
            strWhere = " [PTSAccountNumber] = """ & rst![PTSAccountNumber] & """"
    '        DoCmd.OpenReport "36MonthCoverLetter", acViewNormal, , strWhere, , "False" 'Opens the report based on the strWhere and prints the report
            DoCmd.OpenReport "36MonthCoverLetter", acViewPreview, , strWhere, , "False" 'Opens the report based on the strWhere to be output to PDF
            DoCmd.OutputTo acOutputReport, "36MonthCoverLetter", acFormatPDF, "C:\Users\REDACTED\Desktop\Cover Letter " & rst!ClientName & " " & Right(rst![Account Number], 4) & ".pdf" 'Saves the individual report at a PDF
            DoCmd.PrintOut
            DoCmd.Close acReport, "36MonthCoverLetter"
            rst.MoveNext
        Wend
    
    Set rst = Nothing
    
    Me.cmdPrint.Enabled = False 'makes the button unclickable so a task does not get duplicated
    End Sub
    Here is a link to another forum where I found the potential solution.http://https://www.access-programmer...esktop.279464/

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Link does not work?
    Link is actually https://www.access-programmers.co.uk...esktop.279464/

    Try as an example from that link.
    Code:
    Dim UserProfilePath as String
    UserProfilePath = Environ("Userprofile")
    Dim abre
    abre = Shell("explorer.exe "  & UserProfilePath & "\Documents\Backups", vbNormalFocus)
    Use Debug.Print or the immediate window to check your values.

    Or then you could just Google the function?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    434
    change this line:
    Code:
            DoCmd.OutputTo acOutputReport, "36MonthCoverLetter", acFormatPDF, "C:\Users\REDACTED\Desktop\Cover Letter " & rst!ClientName & " " & Right(rst![Account Number], 4) & ".pdf" 'Saves the individual report at a PDF

    To:
    Code:
            DoCmd.OutputTo acOutputReport, "36MonthCoverLetter", acFormatPDF, Environ$("Userprofile") & "\Desktop\Cover Letter " & rst!ClientName & " " & Right(rst![Account Number], 4) & ".pdf" 'Saves the individual report at a PDF

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Why save business communications in private user folders? Don't want saved in a centralized location?

    Could save to same folder where database frontend is located:

    Code:
    DoCmd.OutputTo acOutputReport, "36MonthCoverLetter", acFormatPDF, CurrentProject.Path & "\CoverLetter\" & rst!ClientName & " " & Right(rst![Account Number], 4) & ".pdf"
    Code should probably check that CoverLetter folder exists and if it doesn't, create it.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 05-19-2021, 10:00 AM
  2. Replies: 42
    Last Post: 03-04-2019, 02:52 AM
  3. Replies: 1
    Last Post: 02-17-2013, 04:46 PM
  4. Environ("username") ---> #Name? Error
    By KiEESH in forum Forms
    Replies: 2
    Last Post: 02-15-2013, 07:23 AM
  5. Problem with Environ function in 2003 SP3
    By wgreynol in forum Programming
    Replies: 2
    Last Post: 10-21-2010, 11:07 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