macro to save to pdf with file path autofill from cell reference

team_qq

New Member
Joined
Jan 20, 2011
Messages
3
I have been trying to write what I thought would be a simple macro to save a worksheet as a pdf file with a file path based on cell references. For some reason, I can't seem to get it to work and have tried everything I know to fix it.

The macro I wrote looks like this:


Public Sub Save_As_PDF_Test()

Dim Name As String
Dim Stamp As String
Dim WhereTo As String
Dim sFileName As String

Sheets("Settings").Activate
Range("B24").Activate
WhereTo = ActiveCell.Value
Range("B29").Activate
Stamp = ActiveCell.Value
If Stamp = "" Then Stamp = Date

Sheets("Form").Activate
Range("B2").Activate
Name = ActiveCell.Value

sFileName = WhereTo & Name & "_" & Stamp & ".pdf"

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
sFileName, Quality _
:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=True

End Sub

When I run the macro, I get the following error message:

"Run time error '1004' Document not saved. Document may be open, or an error may be encountered when when saving."

The document has indeed been saved in .xlsm format and other macros appear to be running fine. The Error shown when running the debugger is in the next to last command to save to PDF ("ActiveSheet.ExportAsFixedFormat Type:.....")

I am running Excel 2010 in Windows 7 on a virtual machine using Parallels on a Mac. I have had not trouble with macros that save documents to a cell reference file path - the error appears isolated to the Save to PDF command.

I'm interested in any ideas how to get this working.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Thanks you for the welcome!

By this I assume you mean recording a macro?

I'm limited here because the file path information in the cells are an equation and I wouldn't know how to copy and paste their outcome into the Save As file line.
 
Upvote 0
I am having the same problem! Some one please help if you know a solution. Here's my code for the Save as PDF part:

Sub Save_As_PDF()
'
' Save_As_PDF Macro
' Saves the report as a PDF
'

Dim SaveLocation As String
Dim Filename As String

SaveLocation = Sheets("Run %Favs & Save All Reports").Range("F1").Value
Filename = Sheets("Run %Favs & Save All Reports").Range("F3").Value

Sheets(Array("Summary Sheet", "Manager Items")).Select
Sheets("Summary Sheet").Activate
ChDir (SaveLocation)
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
Filename, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False

Sheets("Generate Manager Report").Select


I have a workbook set up that runs dynamic reports based on the names I put in. I have 1,000+ names that the macro needs to run through and generate reports for. After one report is generated, the macro uses the above code to save that report as a pdf to a folder on my desktop.

The macro works perfectly fine most of the time. It'll run through several names (200 - 300 names), generate the reports and save to a folder on my desktop without a problem and then it'll randomly throw the 1004 runtime error "Document not saved."

When I hit "Debug" it highlights this portion of the code:

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
Filename, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False

But when I hit play again (to continue running the macro) it continues on without a problem for another 200 - 300 names before it throws the error again.

How can I keep it from throwing this error? I don't want to have to babysit the macro as it goes through 1,000+ names to make sure it hasn't thrown an error.

Thanks in advance!
 
Upvote 0
I'm not familiar with the DoEvents function. I just did a search for it and based on the explanations I've found, I'm not sure how the DoEvents function would help.

It seems that the DoEvents function stops the macro for a period of time? I don't want the macro to stop. Or does the DoEvents allow the macro to keep running even if it encounters the runtime error 1004?
 
Upvote 0
DoEvents yields execution so that the operating system can process other events. You won't notice the pause, but it may be sufficient fix your problem.
 
Upvote 0
I had the same problem. The problem for me turned out to be a permissions problem.

I was trying to save the PDF to C:\ (temporarily) but manually trying to save it to PDF there reminded me that Excel doesn't like C:\ .

I pointed it to a different folder and it worked fine.
 
Upvote 0

Forum statistics

Threads
1,214,801
Messages
6,121,644
Members
449,045
Latest member
Marcus05

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top