Automatically save file before print to PDF

wpryan

Well-known Member
Joined
May 26, 2009
Messages
533
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I have a script that prints an entire workbook to PDF, but it seems to give problems if the file is not saved before the script is run. Script is here:


Code:
Sub PrintPDF()


Dim fp As String, fn As String, model As String
Dim wb As Workbook


Sheets("Release").Visible = False


Calc_Footer


fn = Sheets("General Data").Range("C10").Value
alt_fn = "SCP " & Sheets("General Data").Range("B10").Value
fp = ThisWorkbook.Path & "\" & fn


If fn = "" Then
Dim ansfn
ansfn = MsgBox("Report Number missing. Press OK to complete form else Cancel to print blank form", vbOKCancel, "Report Incomplete!")
    If ansfn = vbOK Then
        Exit Sub
    Else:
        fp = ThisWorkbook.Path & "\" & alt_fn
        
    End If
End If


Dim ans
ans = MsgBox("Output file will be saved in the same directory as the original file", vbOKCancel)
    If ans = vbCancel Then
        Exit Sub
    End If


Sheets("Summary").Unprotect Password:="Z8_Service"
Sheets("Summary").Range("B57") = "Report Finalized on " & Format(Date, "dd-mmm-yyyy") & " By:"




Set wb = ActiveWorkbook


wb.ExportAsFixedFormat Type:=xlTypePDF, _
                       Filename:=fp, _
                       Quality:=xlQualityStandard, _
                       IncludeDocProperties:=True, _
                       IgnorePrintAreas:=False, _
                       OpenAfterPublish:=True
                       
Sheets("Summary").Range("B57") = "Print and sign or place digital signature here after finalizing form."
Sheets("Summary").Protect Password:="Z8_Service"
Sheets("Release").Visible = True
End Sub

I can't figure out where the code is going wrong to produce the error. It seems not to happen after the file is saved to .xlsm format, though, so I thought I could automatically save the file before running the code. I'm not sure how to do that, though.

...any ideas?
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
... a little further information about this. I added the line
Code:
ActiveWorkbook.Save
after the Declarations at the beginning of the code, and receive
runtime error -2147018887 (80071979)
Document not saved
on the line
wb.ExportAsFixedFormat

thanks in advance for any help in this...
 
Upvote 0
Crazy... I believe found and fixed the problem... The filename is a kind of a dynamic thing, where it is dependent on what a user does earlier in the data entry process. One choice left the filename with a forward slash. It seems the code didn't like that...
thanks all for your suggestions though.
 
Upvote 0

Forum statistics

Threads
1,203,094
Messages
6,053,507
Members
444,667
Latest member
KWR21

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