Save as PDF depending on certain criteria

rooster05

New Member
Joined
Mar 4, 2017
Messages
34
Apologies if this is in the wrong place, or has been asked before.
i am trying to create code to enable me to have a sheet that would enable the user to automatically save as PDF, but only if the cell B6 is completed, this is the code i have. This asks for B6 to be completed but will then go on to save.

Or, if possible i would like it to be sent as PDF email


Private Sub workbook_beforeclose(cancel As Boolean)
Sub SaveAsPDF()
'
' SaveAsPDF Macro
Range("a1:f48").Select
ActiveCell.FormulaR1C1 = "."
If Cells("6,2").Value = "" Then
MsgBox "Please complete Cell B6"
cancel = True

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, fileName:= _
"C:\Users\Steve\Desktop\Request to Change an Order_v1 0 RV (2)(AutoRecovered)1.pdf" _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=True

End If
End Sub


any help would be appreciated

thanks
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Create this as a new sub and call it in your current sub when you want it to run or copy and paste the body of the code into where you want it to run.
Looks like in your current code you would replace

Code:
[COLOR=#333333]ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, fileName:= _[/COLOR]
[COLOR=#333333]"C:\Users\Steve\Desktop\Request to Change an Order_v1 0 RV (2)(AutoRecovered)1.pdf" _[/COLOR]
[COLOR=#333333], Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _[/COLOR]
[COLOR=#333333]:=False, OpenAfterPublish:=True[/COLOR]

With the below code


Code:
Sub SaveAndEmail()
           
    Application.DisplayAlerts = False
    
    Dim MyFullPath As String
 
    'Customize this as needed this will be where the file is saved
    MyFullPath = "C:\Users\Desktop\Temp.pdf"


    'Saves the file as a PDF
    ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=MyFullPath, _
    Quality:=xlQualityMinimum, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
    OpenAfterPublish:=False
    
    Application.DisplayAlerts = True
        
    Dim OutApp As Object
    Dim OutMail As Object


    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)




    ' You may want to update To, CC, BCC, Subject, The Body to include any text you want
    ' you can also change .display to .send to just send the email rather than present you with the email to send
    On Error Resume Next
    With OutMail
        .to = ""
        .CC = ""
        .BCC = ""
        .Subject = ""
        .Body = ""
        .Attachments.Add MyFullPath
        .Display ' .Send
    End With
    On Error GoTo 0


    Set OutMail = Nothing
    Set OutApp = Nothing
        


    End Sub
 
Upvote 0

Forum statistics

Threads
1,215,438
Messages
6,124,873
Members
449,192
Latest member
MoonDancer

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