Document Not Saved Error on ExportAsFixedFormat

Recently, I was accusing ExportAsFixedFormat of causing a problem with some code. It turned out to not be the culprit. But I was suspicious because I very often get a Document Not Saved runtime error when I export to PDF in a loop. When I get that error, I can click Debug and F5, and it happily continues until it errors again. Of course, watching something loop eighty-six times to make sure it doesn’t error kind of defeats the purpose of the loop. Today, I took a stand.

I have this code that changes the page fields on a pivot table and exports a range. The result is eighty-six PDFs in a folder that I sew together into one big report. There are two page fields, so there are two loops. Here’s the inner loop.

You see my pathetic DoEvents attempt at avoiding the error. The error stops on the ExportAsFixedFormat line, so the DoEvents doesn’t actually help. When the error happens, a .tmp file is left in the directory. And when I do the Debug, F5 thing, the .tmp file stays there forever. Clearly this is a temporary file that would someday become a PDF if not for this error.

The .tmp file is my evidence that the processed finished. I can ignore the error and as long as there is no temp file, I’ll know the error never occurred. I rewrote the loop thusly:

As long as there’s a file with a tmp extension, I keep trying to export. The Kill statement needs to be inside the On Error because there won’t be anything to Kill the first time.

After a rigorous test of one time, it worked. I should have put a little loop counter in there to see how many times it errored. And maybe to exit out in case it gets into an infinite loop situation.

9 thoughts on “Document Not Saved Error on ExportAsFixedFormat

  1. I love your DoEvents remark. Most of the times I’ve tried to use it have been similarly hopeful/futile. It’s kind of like how I used to run CodeCleaner fairly often to try to fix something, something that generally turned out to be bad code.

    It’s been a while since I’ve done the kind of project you describe, but I think the following worked. Iterate through the page fields, saving the results off as values-only worksheets, all in the same workbook. Then just select all sheets and print (save?) the results to PDF. I probably just did the printing (saving?) manually, but it’s only one workbook.

  2. “changes the page fields on a pivot table and exports a range. The result is eighty-six PDFs in a folder that I sew together into one big report”

    Dick,
    1. Had you thought of using slicers instead of manipulating the page field? Its just an alternate control method, but I have used it for PDFing and have never had your “Document Not saved” error. (Perhaps it forces Excel to keep up with VBA?)
    2. Have you tried rearranging the pivot and using the “Insert page break after each item” control under [Field Settings] for the pivot field? One benefit is that it produces only one PDF file with multiple pages.

  3. Hmmm, interesting ideas. I could copy and paste all to one worksheet with page breaks and just print that one worksheet.

    I only use slicers for a better UI experience, but I’ll try them and see if it makes a difference.

    The pivot table feeds a different area that’s formatted prettily, so the page breaks thing wouldn’t work. Wouldn’t it be nice if we could format pivot tables to look respectable?

    Thanks for the suggestions.

  4. Respectable is in the eye of the beholder.
    As in:
    Dude1: BEHOLD!!!
    Dude2: Ooh. Respectable!

  5. In a similar scenario I selected sheets for export and saved with ExportAsFixedFormat to PDF in one shot.
    Is there any advantage to export them separately and sew together after saving?

  6. They’re all the same page. I just change a pivot table page filter and print the same range over and over. That’s why they’re exported separately and sewn together.

  7. I’m definitely going to try putting it all in one workbook. I use PDF Split and Merge. I can script its command line to insert bookmarks and other cool stuff. I suppose I’ll lose that if I go the temporary workbook route.

  8. I quite like the Show Report Filter Pages Option for this kind of thing. If you were doing it manually from the menu options it would add an extra 80 odd tabs to your workbook in seconds and you could print then them all to PDF in one go. It’s only one line of VBA.


Posting code? Use <pre> tags for VBA and <code> tags for inline.

Leave a Reply

Your email address will not be published.