VBA Outlook - Error Trapping

Lewzerrrr

Active Member
Joined
Jan 18, 2017
Messages
256
Hi, I have the following piece of code, I'm cautious that when users save down a certain .pdf that they won't spell it properly for the macro to catch it. I need to add an on error .. msgbox "BS30 not found, please attach manually." and then carry on next line.

See bold line on where I need to error trap.

Thanks,

Code:
Sub EmailDocument()

Dim OApp As Object, OMail As Object, signature As String, PDFfile As String, Commercial As String, Team As String, VM As String
Dim WBA As Workbook
Dim FPath As String, PPath As String, BSR As String, BSRATP As String, PDFBS30 As String
Dim Season As Range, Yr As Range, Wk As Range, Day As Range, FName As Range, RngCopied As Range


Set OApp = CreateObject("Outlook.Application")
Set OMail = OApp.CreateItem(0)
Set WBA = ActiveWorkbook
Set Season = ActiveWorkbook.Sheets(3).Range("T12")
Set Yr = ActiveWorkbook.Sheets(3).Range("T13")
Set Wk = ActiveWorkbook.Sheets(3).Range("T14")
Set Day = ActiveWorkbook.Sheets(3).Range("T15")
Set FName = ActiveWorkbook.Sheets(3).Range("AB9")
Set RngCopied = ActiveWorkbook.Sheets(4).Range("A2:D7")


PPath = "XXXX"
BSR = PPath & Season & Yr & "\"
'BSRATP = BSR & "XXX"
FPath = BSR & "Week " & Wk & "\"


'Wrap quotes marks and connect with & if starting new line e.g "xxx@xxx.com " & "yyy@yyy.com " & "zzz@zzz.com "
'Please update add to the bottom of commercial chain if new email addresses.
Commercial = XXXX
            
'XXXX
VM = XXXX


'XXX team.
Team = XXXX


    With OMail
    .Display
    End With
    
    PDFfile = FPath & FName & ".pdf"
    PDFBS30 = "XXXXX"
    
    signature = OMail.htmlbody
        
    With OMail
    .To = Commercial & VM
    .cc = Team
    .Subject = FName
    .attachments.Add PDFfile
[B]    .attachments.Add PDFBS30[/B]
    .htmlbody = "<p style='font-family;calibri;font-size:13'>" & "Please find attached." & "</p" & vbNewLine & RangetoHTML(RngCopied) & vbNewLine & signature
    .Display
    End With
    
Set OMail = Nothing
Set OApp = Nothing


End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Your code sort of ends before the End Sub, but before you start setting references, you could use Dir() to see if the file(s) exist. Something like:

Rich (BB code):
Dim MyFileFullName As String


  MyFileFullName = Dir("C:\Users\stumpm\Desktop\vbax\Some.pdf")
  If MyFileFullName = vbNullString Then
  
    MsgBox "'Some.pdf' does not exist...", vbExclamation, vbNullString
    Exit Sub
    
  End If

Hope that helps,

Mark
 
Upvote 0
Your code sort of ends before the End Sub, but before you start setting references, you could use Dir() to see if the file(s) exist. Something like:

Rich (BB code):
Dim MyFileFullName As String


  MyFileFullName = Dir("C:\Users\stumpm\Desktop\vbax\Some.pdf")
  If MyFileFullName = vbNullString Then
  
    MsgBox "'Some.pdf' does not exist...", vbExclamation, vbNullString
    Exit Sub
    
  End If

Hope that helps,

Mark

Oh yes apologies, the bottom code is all that's at the bottom lol.

I'll see if this works. I WOULD prefer an on error resume next tho, do I place it before this bit of code? As it's not a problem if it carries onto the next line?

Hmm.. I just tried this without the PDFBS30 being available but then it won't add the PDFfile.

Any further help?

Code:
    On Error Resume Next        With OMail
        .To = Commercial & VM
        .cc = Team
        .Subject = FName
        .attachments.Add PDFfile
        .attachments.Add PDFBS30
        .htmlbody = "" & "Please find attached." & "        .Display
        End With

Code:
.Display
    End With
    
Set OMail = Nothing
Set OApp = Nothing


End Sub
 
Last edited:
Upvote 0
...I WOULD prefer an on error resume next tho,...

Why? If we cannot avoid an expected error from happening at least occasionally, the On Error Resume Next and checking to see if an error exists might be the way to go. In this case though, we can easily check to see if the file exists rather than blowing past errors.

As it's not a problem if it carries onto the next line?

Hmm.. I just tried this without the PDFBS30 being available but then it won't add the PDFfile.

I am not clear on what you are saying. If you mean you want the email created regardless of whether there's a file to attach to it, then we could add a simple If...Then...End If.

Rich (BB code):
Dim PDFfile As String
Dim Path As String
  
  Path = "C:\Users\stumpm\Desktop\vbax\"
  PDFfile = "Some.pdf"
  
   With OMail
    .To = Commercial & VM
    .cc = Team
    .Subject = FName
    If UCase$(Dir(Path & PDFfile)) = UCase$(PDFfile) Then
      .attachments.Add PDFfile
    End If
  End With

Hope that helps,

Mark
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,310
Members
452,634
Latest member
cpostell

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