+ Reply to Thread
Results 1 to 3 of 3

Not creating Email if there is no file in location

Hybrid View

  1. #1
    Registered User
    Join Date
    08-02-2017
    Location
    London, England
    MS-Off Ver
    2013
    Posts
    4

    Not creating Email if there is no file in location

    Hi,

    I have a macro which adds a file to and email and then sends it.

    However, If there is no file it still creates the emails.

    Please can you let me know if there is anyway for the macro to not do this. Whether that is to delete the email after it has created if it doesn't have an attachment. Or to skip that email if there is no attachment (Would prefer this).


     Dim OutApp As Object
        Dim OutMail As Object
        Dim sh As Worksheet
        Dim cell As Range
        Dim FileCell As Range
        Dim rng As Range
        Dim File As Object
        
    
        With Application
            .EnableEvents = False
            .ScreenUpdating = False
        End With
    
        Set sh = Sheets("Sheet1")
    
        Set OutApp = CreateObject("Outlook.Application")
    
        For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)
    
            Set rng = sh.Cells(cell.Row, 1).Range("C1:Z1")
            If cell.Value Like "?*@?*.?*" And _
               Application.WorksheetFunction.CountA(rng) > 0 Then
           
                Set OutMail = OutApp.CreateItem(0)
    
                With OutMail
                    .to = cell.Value
                    .Subject = "Testfile"
                    .Body = "Hi " & cell.Offset(0, -1).Value
                        
                     For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
                            If Trim(FileCell) <> "" Then
                            If Dir(FileCell.Value) <> "" Then
                            .Attachments.Add FileCell.Value
       
                            End If
                        End If
                    Next FileCell
    
                    .Display  
                End With
    
                Set OutMail = Nothing
            End If
        Next cell
    
        Set OutApp = Nothing
        With Application
            .EnableEvents = True
            .ScreenUpdating = True
        End With
    End Sub

    Thanks,

    cwb1
    Attached Files Attached Files
    Last edited by cwb1; 08-10-2017 at 10:23 AM.

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2504 (Windows 11 Home 24H2 64-bit)
    Posts
    91,087

    Re: Not creating Email if there is no file in location

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Click on Edit to open your thread, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  3. #3
    Registered User
    Join Date
    08-02-2017
    Location
    London, England
    MS-Off Ver
    2013
    Posts
    4

    Re: Not creating Email if there is no file in location

    Updated. Thanks for letting me know

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] creating a link from a set location to a location specified in another cell (googlemaps)
    By texcel_tom in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 11-26-2016, 09:48 AM
  2. Hyperlink to file location in an email body
    By subbby in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-28-2016, 03:22 PM
  3. [SOLVED] Macro to Save Excel File to a SharePoint location and then e-mail that location in Outlook
    By NickyHavey in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-11-2016, 09:27 AM
  4. [SOLVED] [Help]macro to open file browser, to select a location, and save the location to a cell
    By zhuleijia in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-06-2013, 09:56 AM
  5. Creating sequential numbers & save to specific file location
    By ajaaay in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-22-2012, 06:19 PM
  6. Default file location lost after send a Email from Excel
    By OldCityCat in forum Excel General
    Replies: 0
    Last Post: 09-23-2011, 09:37 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1