Forum Discussion

Cowtown689's avatar
Cowtown689
Copper Contributor
Mar 14, 2026

Attachment.Add issue

I have the following sub routine

Private Sub Command6_Click()

Dim Msg As String

Msg = & ",<P> Please find attached the quote you requested." 

Dim O As Outlook.Application

Dim M As Outlook.MailItem

Dim Brochure As String

Set O = New Outlook.Application

Set M = O.CreateItem(olMailItem)

If Option1 = -1 Then OrderEntryProcedure = "File location"

With M

    .BodyFormat = olFormatHTML

    .HTMLBody = Msg

    .To = "email"

   .Subject = "Test"

       .Attachments.Add OrderEntryProcedure

   .Display

End With

 

Set M = Nothing

Set O = Nothing

 

End Sub

My problem is that I cannot return a null value for OrderEntryProcedure which is often the case.   As far as I can tell the Attachments.Add must have a value.

What type of work around exists so that I can simply not attach a file to the email if Option1 is <> than -1? 

1 Reply

  • George_Hepworth's avatar
    George_Hepworth
    Silver Contributor

    There are a few different ways to handle this. 

    One might be:

     ...
        .Attachments.Add Iif(Option1 =-1, OrderEntryProcedure, "")
    ...

    Another might be:

    ...
    If Option1 = -1 Then 
        OrderEntryProcedure = "File location"
    Else
        OrderEntryProcedure = ""
    End If

    Both assume that a ZLS would satisfy the requirement .Attachments.Add must have a value. 

     

    As an alternative, you could wrap the line  .Attachments.Add OrderEntryProcedure in a conditional and only include it when Option1 <> -1.

    Try those options and pick one that works in this context, or let us know if you need further refinement.