Howdy!
This VBA code has been running strong for a year, then all of a sudden I got a call today that it's started giving an error, and I'm really not sure what to do to fix it, as I essentially backwards engineered some other code to get this to work and I have no actual clue what I'm doing.
In a nutshell, I have a workbook with an email macro that, when clicked, auto-creates an email for the user with the to line and subject filled in basis the worksheet, and then includes a .pdf of the worksheet.
However, as of today, I've started getting a run-time error '52': Bad file name or number, and debug highlights the row:
Any help for how to patch this up would be greatly appreciated, as I'm really not too experienced with all this so I'm not even really sure where to start in fixing it.
This VBA code has been running strong for a year, then all of a sudden I got a call today that it's started giving an error, and I'm really not sure what to do to fix it, as I essentially backwards engineered some other code to get this to work and I have no actual clue what I'm doing.
In a nutshell, I have a workbook with an email macro that, when clicked, auto-creates an email for the user with the to line and subject filled in basis the worksheet, and then includes a .pdf of the worksheet.
VBA Code:
Sub EmailVesselLog()
Dim pdfName As String
Dim Sht As Worksheet
Dim LastRow As Long
Dim LastColumn As Long
Dim StartCell As Range
Dim DataCell As Range
Dim myrange As String
Dim PathFileName As String
Set Sht = Worksheets("Vessel Log")
Set StartCell = Range("A1")
Set DataCell = Range("B27")
LastRow = Sht.Cells(Sht.Rows.Count, DataCell.Column).End(xlUp).Row
LastColumn = Sht.Cells(StartCell.Row, Sht.Columns.Count).End(xlToLeft).Column
pdfName = Range("AA2").Text
FullName = ThisWorkbook.Path & "\" & pdfName & ".pdf"
Sht.Range(StartCell, Sht.Cells(LastRow, LastColumn)).Select
With ActiveSheet.PageSetup
.PrintArea = Selection.Address
.Orientation = xlPortrait
.Zoom = False
.FitToPagesTall = False
.FitToPagesWide = 1
End With
If Dir(FullName) <> vbNullString Then
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisWorkbook.Path & "\" & pdfName & " - " & Format(Now, "mm.dd.yyyy hh mm") & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=False, IgnorePrintAreas:=False, OpenAfterPublish:=False
Else
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisWorkbook.Path & "\" & pdfName & " - " & Format(Now, "mm.dd.yyyy hh mm") & ".pdf" _
, Quality:=xlQualityStandard, IncludeDocProperties:=False, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
End If
Dim xCell As Range
Dim xRg As Range
Dim xEmailAddr As String
Dim xTxt As String
On Error Resume Next
xTxt = ActiveWindow.RangeSelection.Address
Set xRg = Range("R5")
If xRg Is Nothing Then Exit Sub
Set xOTApp = CreateObject("Outlook.Application")
For Each xCell In xRg
If xCell.Value Like "*@*" Then
If xEmailAddr = "" Then
xEmailAddr = xCell.Value
Else
xEmailAddr = xEmailAddr & ";" & xCell.Value
End If
End If
Next
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = xEmailAddr
.CC = ""
.Subject = Range("AA1").Value
.HTMLBody = ""
PathFileName = ThisWorkbook.Path & "\" & pdfName & " - " & Format(Now, "mm.dd.yyyy hh mm") & ".pdf"
.Attachments.Add PathFileName
.Display 'or use .Display
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
Kill ThisWorkbook.Path & "\" & pdfName & " - " & Format(Now, "mm.dd.yyyy hh mm") & ".pdf"
End Sub
However, as of today, I've started getting a run-time error '52': Bad file name or number, and debug highlights the row:
VBA Code:
If Dir(FullName) <> vbNullString Then
Any help for how to patch this up would be greatly appreciated, as I'm really not too experienced with all this so I'm not even really sure where to start in fixing it.