Code doesn't run after PDF tweak

rickblunt

Well-known Member
Joined
Feb 18, 2008
Messages
609
Office Version
  1. 2019
Platform
  1. Windows
Hello,
I have used the following code for quite a while and it has worked fine. Now I made a tweak to the code below to save a worksheet file as a PDF instead of an XLSX file and for some reason it will no longer run completely. It is stopping right after it has saved the pdf file. I do not get any error messages, all the excel pages go blank with grayed out toolbars.

The interesting thing is that the file does show up in the folder, but I cannot open it because it is corrupted. So it sounds like it is not actually saving the file correctly or completely? I disabled the display alerts box thinking maybe it was a popup but that did not change anything. I can't step into anything after this point because of the windows going blank.

Does anyone see anything amiss in the code? I appreciate any input at all - thank you

VBA Code:
Sub SendFallProtectionReport()

    Dim wb As Workbook
    Dim myName As Variant
  
  ' This subset creates the log entry for the completed inspection and then sends out the email.
  
       myName = InputBox("NOTICE" & vbCrLf & "By entering your name you are affirming that you have reviewed the report and that" _
         & " you agree with all of the findings as noted." & vbCrLf & vbCrLf & "Enter Name: ", "Inspection Report Submittal")
        
        If myName = "" Then
    
        Exit Sub
        
    Else
    End If
    
        Range("'Fall Protection Report'!J2").Value = myName
        Range("'Fall Protection Report'!K2").Value = Now()
        
        myTime = Range("'Fall Protection Report'!J3").Value
    
            Worksheets("Inspection Log").Activate
  
        Application.EnableEvents = False
        
        Sheets("Fall Protection Report").Shapes("Group 16").Visible = False
          
        Sheets("Fall Protection Report").ExportAsFixedFormat _
        Type:=xlTypePDF
                
        Set wb = ActiveWorkbook
        With wb
            Application.DisplayAlerts = False
            .SaveAs _
            "\\MESSDV002.na.infineon.com\Facilities\DeptData\EH&S\Compliance and EHS\Facilities Inspections\Fall Protection\Fall Protection Inspection Report  " _
            & Format(Now, "yy_mmdd") & ".pdf" 
            .Close True
        
    'The code stops here, screens go blank....

    Application.DisplayAlerts = True
        
        End With
                Sheets("Fall Protection Report").Shapes("Group 16").Visible = True
        With Sheets("Inspection Log")
 
     nextrow = .Range("A" & Rows.Count).End(xlUp).Row + 1
 
     .Range("A" & nextrow) = "Fall Protection"
     .Range("B" & nextrow) = myTime
     .Range("C" & nextrow) = myName
     .Range("D" & nextrow).Formula = "=Hyperlink(" & Chr(34) & "\\MESSDV002.na.infineon.com\Facilities\DeptData\EH&S\Compliance and EHS\Facilities Inspections\Fall Protection\Fall Protection Inspection Report  " _
          & Format(myTime, "yy_mmdd") & ".xlsx" & Chr(34) & ")"
     
    End With
     
    Worksheets("Fall Protection Report").Activate
 
    ActiveSheet.Range("C1:H30").Select
    
    Range("K1").Select

' Show the envelope on the ActiveWorkbook.
    
    ActiveWorkbook.EnvelopeVisible = True
   
   With ActiveSheet.MailEnvelope
      .Introduction = " The following facility inspection has been completed. "
      .Item.To = Range("Coding!A22").Value
      .Item.Subject = "Fall Protection Inspection Completed"
      .Item.Send
      
   End With
      
    Application.DisplayAlerts = True

End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
You can't save a workbook as a pdf, you need to export as pdf.
Try using the macro recorder to saveas pdf to get an idea what the code looks like. Then you can edit it if required.
It will look something like this

VBA Code:
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "C:\Users\davem\OneDrive\Documents\SaveAsPDF_Sample.pdf", Quality:= _
        xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
        OpenAfterPublish:=True

You can use activesheet, activeworkbook, a specific range a namedworksheet
 
Upvote 0
Solution
Correct, that is why I was using the ExportAsFixedFormat. So it sounds like I haven't defined the save parameters correctly - thank you for the input, I will define that better.
 
Upvote 0
You can't save a workbook as a pdf, you need to export as pdf.
Try using the macro recorder to saveas pdf to get an idea what the code looks like. Then you can edit it if required.
It will look something like this

VBA Code:
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "C:\Users\davem\OneDrive\Documents\SaveAsPDF_Sample.pdf", Quality:= _
        xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
        OpenAfterPublish:=True

You can use activesheet, activeworkbook, a specific range a namedworksheet
Thanks Dave, once I defined the parameters more accurately, and rearranged/tweaked the lines a little bit it worked perfectly. Thanks for pointing me in the right direction, it is really appreciated.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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