Solved Saving Report As PDF on desktop (1 Viewer)

theinviter

Registered User.
Local time
Yesterday, 22:41
Joined
Aug 14, 2014
Messages
168
HI
i wanna save a report as PDF file.
but always get Error; invalid folder path,
i tried

Dim fileName As String, fldrPath As String, filePath As String
Dim answer As Integer

fileName = "Member Contact Details" 'filename for PDF file*

fldrPath = "C:\Users\Default\Desktop" 'folder path where pdf file will be saved *

filePath = fldrPath & "\" & fileName & ".pdf"

'check if file already exists
If FileExist(filePath) Then
answer = MsgBox(prompt:="PDF file already exists: " & vbNewLine & filePath & vbNewLine & vbNewLine & _
"Would you like to replace existing file?", buttons:=vbYesNo, Title:="Existing PDF File")
If answer = vbNo Then Exit Sub
End If

On Error GoTo invalidFolderPath
DoCmd.OutputTo objecttype:=acOutputReport, objectName:=Me.Name, outputformat:=acFormatPDF, outputFile:=filePath

MsgBox prompt:="PDF File exported to: " & vbNewLine & filePath, buttons:=vbInformation, Title:="Report Exported as PDF"
Exit Sub

invalidFolderPath:
MsgBox prompt:="Error: Invalid folder path. Please update code.", buttons:=vbCritical
End Sub



Function FileExist(FileFullPath As String) As Boolean


Dim value As Boolean
value = False
If Dir(FileFullPath) <> "" Then
value = True
End If
FileExist = value
End Function
 

June7

AWF VIP
Local time
Yesterday, 21:41
Joined
Mar 9, 2014
Messages
4,309
In future, please post code between CODE tags.

This example worked for me:
Code:
DoCmd.OutputTo acOutputReport, "Compensation", acFormatPDF, Environ("USERPROFILE") & "\Desktop\Comp.pdf"
I tried and failed:
"C:\Users\Default\Desktop\Comp.pdf"
and
"C:\Users\Public\Desktop\Comp.pdf"
 
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Today, 01:41
Joined
Feb 19, 2002
Messages
35,925
fldrPath = "C:\Users\Default\Desktop" 'folder path where pdf file will be saved *
1. You need the path to end with a "\" so the name can be appended correctly.
2. Saving to the desktop is a seriously poor practice. Either have the user set up directories for various purposes and save the preferences in a table so each user can have his own, or have the user pick a destination just before you generate the new .pdf.
 

June7

AWF VIP
Local time
Yesterday, 21:41
Joined
Mar 9, 2014
Messages
4,309
Pat, the "\" is concatenated in the next line that appends the file name.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 01:41
Joined
Feb 19, 2002
Messages
35,925
Try this to construct the path:
Code:
fldrPath = "C:\Users\" & Environ("UserName") & "\Desktop" 'folder path where pdf file will be saved *

And I repeat. Saving files to the desktop is seriously poor practice. This is something that hasn't been done in 30 years as a default. It is a leftover concept from the time when every application that got installed thought it was the only application in the universe the PC belonged only to it.
 

June7

AWF VIP
Local time
Yesterday, 21:41
Joined
Mar 9, 2014
Messages
4,309
Not disagreeing with principle. Just saying that was not code error. I have encountered issue with programmatically saving files into user folder. Seems IT put a block on that. Had to rewrite some code.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 01:41
Joined
Feb 19, 2002
Messages
35,925
@June7 You mentioned Public and User. Did you try the specific logged in user's desktop?
 

June7

AWF VIP
Local time
Yesterday, 21:41
Joined
Mar 9, 2014
Messages
4,309
No. I am not working with a network. Will let OP test if they want.
 

Users who are viewing this thread

Top Bottom