+ Reply to Thread
Results 1 to 9 of 9

Macro to select Files to attach

Hybrid View

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

    Macro to select Files to attach

    I have the following macro which generates and email from Excel.

    I need the code the code amended to that it allows the user to attach selected files in c:\my documents

    Your assistance in this regard is most appreciated



     Sub Email_Reports()
    Dim FileExtStr As String
        Dim FileFormatNum As Long
        Dim Sourcewb As Workbook
        Dim Destwb As Workbook
        Dim TempFilePath As String
        Dim TempFileName As String
        Dim OutApp As Object
        Dim OutMail As Object
        Dim sh As Worksheet
        Dim TheActiveWindow As Window
        Dim TempWindow As Window
    
        ztext = [bodytext]                              'read in text from named cell
    Zsubject = [subjectText]
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
        
        
        
       
    
       Set Sourcewb = ActiveWorkbook
    
            'We add a temporary Window to avoid the Copy problem
        'if there is a List or Table in one of the sheets and
        'if the sheets are grouped
        With Sourcewb
            Set TheActiveWindow = ActiveWindow
            Set TempWindow = .NewWindow
            '.Sheets("Sales1").Copy
        End With
    
    
        
        
        
    
        'Close temporary Window
        TempWindow.Close
    
        Set Destwb = ActiveWorkbook
    
        'Determine the Excel version and file extension/format
        With Destwb
            If Val(Application.Version) < 12 Then
                'You use Excel 97-2003
                FileExtStr = ".xls": FileFormatNum = -4143
            Else
                'You use Excel 2007-2016
                Select Case Sourcewb.FileFormat
                Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
                Case 52:
                    If .HasVBProject Then
                        FileExtStr = ".xlsm": FileFormatNum = 52
                    Else
                        FileExtStr = ".xlsx": FileFormatNum = 51
                    End If
                Case 56: FileExtStr = ".xls": FileFormatNum = 56
                Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
                End Select
            End If
        End With
    
        '    'Change all cells in the worksheets to values if you want
        '    For Each sh In Destwb.Worksheets
        '        sh.Select
        '        With sh.UsedRange
        '            .Cells.Copy
        '            .Cells.PasteSpecial xlPasteValues
        '            .Cells(1).Select
        '        End With
        '        Application.CutCopyMode = False
        '        Destwb.Worksheets(1).Select
        '    Next sh
    
        'Save the new workbook/Mail it/Delete it
    
    
    
      
    
        
        TempFilePath = Environ$("temp") & "\"
    '  TempFileName = Range("B1") & Format(Now, "dd-mmm-yy h-mm-ss")
       TempFileName = Format(Range("B1"), "mmm-yy ") & Format(Now, "dd-mmm-yy h-mm-ss")
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
    
    
    
        With Destwb
            .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
            On Error Resume Next
            With OutMail
    
    
                .To = Join(Application.Transpose(Range("F1:F4").Value), ";")
    
                .CC = ""
                .BCC = ""
                 .Subject = Zsubject
               .Body = ztext
    
                '.Body = strBody
                ..Attachments.Add ("C:\my documents")
                'You can add other files also like this
                           .display   'Use .send to send automatically or  .Display to check email before sending
               End With
            On Error GoTo 0
            .Close savechanges:=False
        End With
    
           
          
        'Delete the file you have send
        Kill TempFilePath & TempFileName & FileExtStr
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
        
        Application.ScreenUpdating = True
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    
    
    End Sub

  2. #2
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,347

    Re: Macro to select Files to attach

    This will give you an idea...
    Sub Test()
    Dim FileArr, i As Long
    With Application.FileDialog(msoFileDialogFilePicker)
        .InitialFileName = "C:\my documents"
        If .Show = -1 Then
            ReDim FileArr(1 To .SelectedItems.Count)
            For i = 1 To .SelectedItems.Count
                FileArr(i) = .SelectedItems(i)
            Next i
        Else
            MsgBox "NO FILES SELECTED", vbInformation, ""
            Exit Sub
        End If
    End With
    With CreateObject("outlook.application").CreateItem(0)
        .To = "[email protected]"
        .Subject = "Whatever"
        For i = 1 To UBound(FileArr)
            .Attachments.Add FileArr(i)
        Next i
        .HTMLBody = .HTMLBody & "Whatever"
        .Display 'Change to Send after testing
    End With
    End Sub
    Last edited by sintek; 04-12-2020 at 05:03 AM.
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

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

    Re: Macro to select Files to attach

    Hi Sintek

    Thanks for the help. I am able to select the workbooks I want to attach to outlook


    I have named B1 as SubjectText & B2 as Bodytext (see range name in Code Ztext = [bodytext] & Zsubject = [subjectText]


    I am battling to get the subject named Subject Text to show in the subject line and Bodytext to show the contents in the body off my email


    It would be appreciated if you could assist -have attached sample data

     Sub Attach_Reportsto_Email()
    
    
    Dim FileArr, i As Long
    Ztext = [bodytext]                              'read in text from named cell
    Zsubject = [subjectText]
     With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
    
    
    With Application.FileDialog(msoFileDialogFilePicker)
        .InitialFileName = "C:\my documents"
        If .Show = -1 Then
            ReDim FileArr(1 To .SelectedItems.Count)
            For i = 1 To .SelectedItems.Count
                FileArr(i) = .SelectedItems(i)
            Next i
        Else
            MsgBox "NO FILES SELECTED", vbInformation, ""
            Exit Sub
        End If
    End With
    With CreateObject("outlook.application").CreateItem(0)
          .To = Join(Application.Transpose(Range("E1:E2").Value), ";")
    
        .Subject = "& Zsubject"
              ' .Body = ztext
    
        For i = 1 To UBound(FileArr)
            .Attachments.Add FileArr(i)
        Next i
        .HTMLBody = .HTMLBody = Ztext
        
        .Display 'Change to Send after testing
    End With
    End Sub

  4. #4
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,347

    Re: Macro to select Files to attach

    Quick question...Do you have a Email signature set up in outlook...

    I also see your variable declaration is not set...As suggested...Otherwise an error would have popped up...
    Are you doing it this way because you are going to be looping down the list for multiple emails...

    Option Explicit
    
    Sub Attach_Reportsto_Email()
    Dim FileArr, i As Long, zText As String, zSubject As String
    zText = Range("bodytext")
    zSubject = Range("subjectText")
    Application.ScreenUpdating = False
    With Application.FileDialog(msoFileDialogFilePicker)
        .InitialFileName = "C:\my documents"
        If .Show = -1 Then
            ReDim FileArr(1 To .SelectedItems.Count)
            For i = 1 To .SelectedItems.Count
                FileArr(i) = .SelectedItems(i)
            Next i
        Else
            MsgBox "NO FILES SELECTED", vbInformation, ""
            Exit Sub
        End If
    End With
    With CreateObject("outlook.application").CreateItem(0)
        .Display '! Allows for Email Signature to become visible....If you have one available
        .To = Join(Application.Transpose(Range("E1:E2").Value), ";")
        .Subject = zSubject
        For i = 1 To UBound(FileArr)
            .Attachments.Add FileArr(i)
        Next i
        .HTMLBody = zText & "<br>" & .HTMLBody
        .Display 'Change to Send after testing
    End With
    Application.ScreenUpdating = True
    End Sub
    Last edited by sintek; 04-12-2020 at 08:51 AM.

  5. #5
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,767

    Re: Macro to select Files to attach

    Hi Sintek

    Thanks for the reply

    The Variable declaration has been set -see attached screen print

    1) I do not have email signature set up in outlook
    2) I do not need to loop down the list for multiple emails-Once emails attached I just need the subject to be inserted from the named range "subjectText" and the body of the email to be inserted from the named range "Bodytext"


    It would be appreciated if you could assist with the above so that the Subject line in inserted as well as the body i.e contents of the email
    Attached Images Attached Images
    Last edited by Howardc1001; 04-12-2020 at 10:09 AM.

  6. #6
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,347

    Re: Macro to select Files to attach

    post 4 does that...Why named ranges for subject and body??

    Why not just...
     .HTMLbody = "Hi " & Range("D1") & "<br><br>" & "Attached please find Sales Report for March 2020." & "<br><br>" & "Regards" & "<br><br>" & "Howard"
    Last edited by sintek; 04-12-2020 at 10:13 AM.

  7. #7
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,767

    Re: Macro to select Files to attach

    Hi Sintek

    Code from # 4 is working, except the are no line spaces for the body-see below

    Hi Dave Attached please find Sales Report for March 2020. Regards Howard

     .HTMLBody = zText & "<br>" & .HTMLBody & ""


    I decided to use range names as I find it easier to amend the content on the sheet

  8. #8
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,347

    Re: Macro to select Files to attach

    Never seen that used before hence post 6 suggestion...

  9. #9
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,767

    Re: Macro to select Files to attach

    Thanks for your input, which is much appreciated


    Will use your suggestion in post # 6

+ 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 attach all files in a folder
    By Howardc1001 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-27-2019, 09:01 AM
  2. Macro to create emails, attach files, add recipient and add subject line
    By dw_22801 in forum Outlook Programming / VBA / Macros
    Replies: 8
    Last Post: 05-08-2017, 05:11 PM
  3. Attach files created by macro to an email
    By JesseSingh in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-04-2016, 12:12 AM
  4. [SOLVED] Macro to Import Multiple TXT Files into workbook - User to select files/directory
    By saber007 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-15-2013, 08:43 PM
  5. Excel Email Macro - HELP! - Need to be able to attach two different files
    By benwahchang in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-29-2013, 08:41 AM
  6. Application.GetOpenFilename, Attach files to Email, Move files, Delete Original.
    By D_Rennie in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-08-2009, 12:11 AM
  7. [SOLVED] I want to attach word files or PDF files to an excel database
    By Dianne Munro in forum Excel General
    Replies: 1
    Last Post: 03-22-2006, 08:15 PM

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