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.
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.