Docmd.Output only works if output file already exists?


  • Simple code:

    DoCmd.OutputTo acOutputReport, MyReport, acFormatPDF, MyPath

    Works if MyPath already exists otherwise it doesn't????

    Friday, October 22, 2010 5:12 PM

All replies

  • Hello siicl,

    I think you are right,  OutputTo method is designed for developer to specify an existing folder.

                I have taken the reference about DoCmd.OutputTo on msdn website:

    And then, I tried the following code in Access2010,

    DoCmd.OutputTo acOutputReport, "All Assets", acFormatPDF

    DoCmd.OutputTo acOutputReport, "All Assets", acFormatPDF, "E:\PDFFiles\1.pdf"

    The first line code pop up a dialog need you to specify a file name and directory to save.

    The second line directly output the PDF file if the file folders PDFFiles exist, but if PDFFiles does not exist, it won’t work.

                Hopefully, this can do help to you, if you have any concern about it, feel free to follow up! Wish you a nice day!

    Best Wishes,

    Bruce Song

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, October 25, 2010 8:26 AM
  • So it's not just me then. Your statement:

    The second line directly output the PDF file if the file folders PDFFiles exist, but if PDFFiles does not exist, it won’t work.

    is exactly my issue. They actually built it this way? Wow doesn't make much sense. Guess I create a blank one and stick in the folder first.


    Monday, October 25, 2010 4:55 PM
  • Hi there,

    it's the folder (directory) that needs to exist before you attempt to use DoCmd.OutputTo with a file whose path points there, not the file itself!

    So, if you want to make sure that your target folder exists, check and create it beforehand, using something along the lines of:

    Public Function CheckCreateFolder(ByVal strFullPath As String) As Boolean
     On Error Resume Next
      If Dir(strFullPath, vbDirectory) = "" Then
       MkDir strFullPath
      End If
      If err.Number = 0 Then CheckCreateFolder = True Else CheckCreateFolder = False
    End Function

    Just note that a) you'll need the appropriate permission on the target machine to be able to create your directory and b) that the above (MkDir, that is) only supports creating one level (i.e. the last path in strFullPath). If you need to create a directory tree, you'll either have to do your own parsing (split the parts of the path and, starting from the left, check each [concatenated]part create it if it doesn't exist) or revert to using an API-call. Here's a sample method that utilizes the MakeSureDirectoryPathExists-method contained in ImageHlp.dll (copy/paste the API-declaration in the header-comment at the top of the module you put the method into). I probably haven't used that method in, uh, quite a few years, hence I'm not certain whether or not this exists on Win7 (I guess it should be - can't test where I'm at right now). FWIW, You should be able to locate the DLL under %system%\imagehlp.dll:

    'Checks whether the requested path exists; if not, the method will attempt to create
    'the complete path (or the missing parts).
    'The method returns <True> on success, <False> otherwise.
    'Requires declaring the following API-call on the module's header level:
    'Declare Function MakePath Lib "ImageHlp.dll" Alias _
    '  "MakeSureDirectoryPathExists" (ByVal lpPath As String) As Long
    Function CheckMakePath(ByVal strFullPath As String) As Boolean
      'Default: failure
      CheckMakePath = False
      'Assure path ends with a trailing backslash
      If Right(strFullPath, 1) <> "\" Then strFullPath = strFullPath & "\"
      If Dir(strFullPath, vbDirectory) = "" Then
       If MakePath(strFullPath) <> 0 Then CheckMakePath = True
       CheckMakePath = True
      End If
    End Function
    Just note that you don't need the above if you only need to assure that i.e. a folder exists that lies right underneath your app's directory.

    Monday, October 25, 2010 5:38 PM
  • Well now I'm confused. My folder does already exist because I used:

    MyPath = CurrentProject.Path

    MyPath = MyPath & "\File.pdf"

    Then I used:

    DoCmd.OutputTo acOutputReport, MyReport, acFormatPDF, MyPath

    But only worked if PDF file already existsed?

    Monday, October 25, 2010 6:06 PM
  • Hello siicl,

    Do you mean that DoCmd.OutputTo can work only if when you have File.pdf in your path beforehand?

     If so, I couldn’t reproduce your issue, I have tested your code with Access2010, however, whether File.pdf already existed beforehand or not, DoCmd.OutputTo all can work well! If File.pdf not exists, it will create a new file, or it will overwrite the origin one.

    By the way, what’s your version of your Access?

                Hopefully, this can do some help to you, please let me know your result!

    Best Regards,

    Bruce Song

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Tuesday, October 26, 2010 6:37 AM
  • Hm. I guess we're talking about some sort of permission problem then. Can you create your PDF if, in your DoCmd.OutputTo call, you omit the path/filename? Access should prompt you for the location to save then. What version are you using, BTW?
    Tuesday, October 26, 2010 6:39 AM
  • Hi siicl,

    why do you think so? Do you have any experience of well working OutputTo command with existing .pdf? What error do you have in your case?

    Andrey V Artemyev | Saint-Petersburg, Russia
    Tuesday, October 26, 2010 7:00 AM
  • I'm running XP; Access 2007

    If the file is not already in the folder I get the old generic error 2501

    If I leave out the file name it prompts me but if I try to save it to the same folder I get the error, unless the file already exists.

    I've tried every which way but only seems to works if the file already exists. Doesn't seem to have any problem overwriting this file.

    That's what I was afraid of, I'm nuts! I was hoping it was simply some switch or something that I didn't know about.
















    Tuesday, October 26, 2010 10:13 PM
  • Have you tried saving to a location that is commonly available for all, such as the MyDocuments folder, i.e. the folder you get by calling environ("UserProfile") ? Alternatively, can you try this on a different machine?

    Wednesday, October 27, 2010 7:49 AM
  • I have a similar problem - sort of.

    I have no problem saving to a network folder - either empty or overwriting an existing file of the same name. I get the 2501, OutputTo action canceled, when i try to save and overwrite a file that is shown on our internal webpage via sharepoint. Initially I thought the problem was trying to overwrite an existing file. Thanks to the post above, I was able to disprove that. Now I need to find a way for the file to be viewed on sharepoint in such a way that it can be overwritten while people are on SP.

    I know I didn't help you, siicl, but this thread helped me. Thanks for posting the initial question.

    Friday, November 19, 2010 8:01 PM
  • Access 2007 different OS, DoCmd.OutputTo PDF; folder exists, specific file may or may not exist

    I just spent 1+ day(s) figuring this out:

    • Win7 - file needs to exist, otherwise err 2501 (unless you just deleted it, and Win7 still thinks it exists)
    •         - from print preview, save as PDF fails unless file exists (finally figured this out via error message "make sure file exists"), then there is an option to overwrite
    • Vista - works OK with or without specific file

    According to research, DoCmd.OutputTo is extremely flaky on different OS or machines or output format which is my experience now.

    Thank you Bill Gates.  To whom may I address the bill for my time?

    Thursday, July 28, 2011 11:32 PM
  • I have a similar problem also, but no matter the file exists or not , it doesn't work on version 07,

    but it works well on version 03! OS:Xp

    Thursday, August 25, 2011 3:00 AM