user2461461
user2461461

Reputation: 21

saving as PDF file on VBA return "Document not saved" error

the following code causes a "Document not saved" error:

sFileName = "C:\test.pdf"
Application.DisplayAlerts = False
wkbPDF.Worksheets.Select        
ActiveSheet.ExportAsFixedFormat Type := xlTypePDF, Filename := gsPDF_DIR & sFileName, _
  Quality:=xlQualityStandard, IncludeDocProperties := True, _
  IgnorePrintAreas := False, OpenAfterPublish := False

Application.DisplayAlerts = True

but it is sucessful doing it outside VBA (but it takes several minutes since the workbook contains more than 100 sheets).

Upvotes: 2

Views: 6944

Answers (3)

Geographos
Geographos

Reputation: 1496

Make sure, that your output file does not exist yet. If, for example, the gsPDF_DIR & sFileName has been saved previously in the same directory, where your workbook is located, then this error might also occur. You can see off this issue by removing previously saved file under gsPDF_DIR & sFileName or simply move your workbook to the other directory.

Upvotes: 0

Mohinder Singh
Mohinder Singh

Reputation: 13

I have also encountered the problem. The worst part is Microsoft VBA's error description. I was trying to save the file on C drive which has write access issue.So changed it to different location and it worked. Sometime VBA gives you error description which is unrelated to the actual problem.

Upvotes: 1

DeanOC
DeanOC

Reputation: 7282

Your Filename: parameter isn't going to be valid if gsPDF_DIR has any value, because sFileName has already specified the drive, folder and filename.

Try using Filename := sFileName

Upvotes: 1

Related Questions