0

I've got a simple VBA function that sends an email with an attached PDF. About every 50 or times the code is called, instead of attaching the PDF, it sends a link to the file where it's saved in SharePoint. Has anyone else ever seen this behavior and has an answer for why it occasionally send a link to the file instead of the actual file?

Sub SendEmail()
    Dim rng As Range, c As Range
    Dim nRow As Integer
    Dim rw As ListRow
    Dim Emails As String
    Dim Outlook As Object
    Dim OutlookMail As Object
    
    
    'Send report via Outlok
    Set Outlook = CreateObject("Outlook.Application")
    Set OutlookMail = Outlook.CreateItem(0)
    
    With OutlookMail
        .To = "Red-Scheduling"
        .CC = ""
        .BCC = ""
        .Subject = "Schedules - " & Format(ActiveSheet.Range("ScheduleDate"), "mm/dd/yyyy")
        .htmlbody = RangetoHTML(Worksheets("Schedule").Range("EmailReport"))
        .Attachments.Add PDFActiveSheet
        .Display
         
    End With

End Sub




Function PDFActiveSheet() As String
Dim wbA As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant

On Error GoTo errExit

    Set wbA = ActiveWorkbook
    strTime = Format(Range("ScheduleDate").Value2, "mm-dd-yyyy")
    
    'get active workbook folder, if saved
      strPath = "https://****.sharepoint.com/sites/RedmondShared/Shared%20Files/Schedules/"
    
    'replace spaces and periods in sheet name
    strName = Replace(strName, ".", "_")
    
    'create default name for savng file
    strFile = strName & "_" & strTime & ".pdf"
    strPathFile = strPath & strFile
    
    Range("EmailReport").ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=strPathFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
    PDFActiveSheet = strPathFile

exitSub:
    Exit Function
    
errExit:
    MsgBox "Could not create PDF file"
    PDFActiveSheet = ""
    Resume exitSub
End Function
Frank Ball
  • 1,039
  • 8
  • 15

0 Answers0