+ Reply to Thread
Results 1 to 3 of 3

Macro to Double Click on Cell in Col E to create Email to attach file

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2024
    Posts
    2,888

    Macro to Double Click on Cell in Col E to create Email to attach file

    I have the path name and file names in Col E

    I have tried to write code which is pasted in the sheet module to be able to double click on any of the cells and to generate an email and attah the file

    When I double click a cell nothing happens

    See my code below


     Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
        Dim objOutlook As Object
        Dim objMail As Object
        Dim filePath As String
        Dim fileName As String
        Dim subject As String
        Dim body As String
        
        'Check if the double-clicked cell is in column E
        If Target.Column = 5 Then ' Change to 5 for column E
            'Get the file path from the double-clicked cell
            filePath = "'" & Target.Value & "'"
    
            
            'Check if the file path exists
            If Dir(filePath) <> "" Then
                'Initialize Outlook application
                Set objOutlook = CreateObject("Outlook.Application")
                Set objMail = objOutlook.CreateItem(0) 'Create a new email
                
                'Extract the file name from the file path
                fileName = Right(filePath, Len(filePath) - InStrRev(filePath, "\"))
                MsgBox filePath
                'Set email subject and body
                subject = "Bank Confirmation letter"
                body = "Dear Sirs," & vbCrLf & vbCrLf & "Please find attached the bank confirmation letter." & vbCrLf & vbCrLf & "Regards," & vbCrLf & "Howard"
                
                'Create the email and attach the file
                With objMail
                    .To = ""
                    .subject = subject
                    .body = body
                    .Attachments.Add filePath 'Attach the file
                    
                    'Display the email
                    .Display
                End With
                
                'Clean up
                Set objMail = Nothing
                Set objOutlook = Nothing
                
                Cancel = True 'Prevent default double-click behavior
            End If
        End If
    End Sub

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,524

    Re: Macro to Double Click on Cell in Col E to create Email to attach file

    I moved your Cancel=true and removed the apostrophes for the filePath

        If Target.Column = 5 Then                    ' Change to 5 for column E
            Cancel = True            'Prevent default double-click behavior
            'Get the file path from the double-clicked cell
            filePath = Target.Value

  3. #3
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2024
    Posts
    2,888

    Re: Macro to Double Click on Cell in Col E to create Email to attach file

    Many Thanks Dave

    Code Works 100%

+ 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] Macro to create an email and allow user to attach a workbook/s
    By Howardc1001 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-13-2023, 07:02 AM
  2. Replies: 1
    Last Post: 08-05-2016, 02:33 PM
  3. Double click cell - copy data to new sheet - create a list from double clicking
    By kakky in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-04-2015, 11:14 PM
  4. [SOLVED] Create an email in Excel and attach the file as a PDF
    By MissDB in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-21-2014, 09:50 AM
  5. [SOLVED] Trying to create a .pdf and attach it to email, but get .xls file instead
    By sbrnard in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-19-2013, 01:21 PM
  6. [SOLVED] Macro to create a pdf and attach to an email (Outlook)
    By jsedore in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 02-01-2013, 03:16 PM
  7. Create macro upon double click cell display filter in new sheet
    By kamalhilmi in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-14-2012, 07:40 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