Send Attached excel sheet to varied email addresses

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • forrestgump
    New Member
    • Sep 2007
    • 34

    #1

    Send Attached excel sheet to varied email addresses

    I am currently trying to create VBA to send a specified excel sheet to varied email sources. I currently have the code below which sends the attachment in an email to a specified source e.g. mrqwe@hotmails. com, however I want to change this so the address is chosen from a specified cell on a worksheet. I have tried to reference a cell e.g. .sendmail Sheets("sheet3" ).range("A1"),_ but this does not work. Any ideas anyone?

    Code:
     Sub Mail_ActiveSheet() 
    'Working in 97-2007
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim Sourcewb As Workbook
    Dim Destwb As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
     
    With Application
    .ScreenUpdating = False
    .EnableEvents = False
    End With
     
    Set Sourcewb = ActiveWorkbook
     
    'Copy the sheet to a new workbook
    Sheets("Sheet2").Copy
    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
    'We exit the sub when your answer is NO in the security dialog that you only
    'see when you copy a sheet from a xlsm file with macro's disabled.
    If Sourcewb.Name = .Name Then
    With Application
    .ScreenUpdating = True
    .EnableEvents = True
    End With
    MsgBox "Your answer is NO in the security dialog"
    Exit Sub
    Else
    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 If
    End With
     
    ' 'Change all cells in the worksheet to values if you want
    ' With Destwb.Sheets(1).UsedRange
    ' .Cells.Copy
    ' .Cells.PasteSpecial xlPasteValues
    ' .Cells(1).Select
    ' End With
    ' Application.CutCopyMode = False
     
    'Save the new workbook/Mail it/Delete it
    TempFilePath = Environ$("temp") & "\"
    TempFileName = "Headcount " & Format(Now, "dd-mmm-yy h-mm-ss")
     
    With Destwb
    .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
    On Error Resume Next
    .SendMail "mrqwe@hotmails.com", _
    "Headcount&Turnover"
    On Error GoTo 0
    .Close SaveChanges:=False
    End With
     
    'Delete the file you have send
    Kill TempFilePath & TempFileName & FileExtStr
     
    With Application
    .ScreenUpdating = True
    .EnableEvents = True
    End With
    End Sub
    Last edited by Jim Doherty; Feb 10 '08, 09:37 AM. Reason: Code tags
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    #2
    Assuming Sheet3 is on the current workbook then try the following to reference the A2 ...

    ActiveWorkbook. Worksheets("She et3").Cells(2, 1)

    Comment

    • forrestgump
      New Member
      • Sep 2007
      • 34

      #3
      Originally posted by msquared
      Assuming Sheet3 is on the current workbook then try the following to reference the A2 ...

      ActiveWorkbook. Worksheets("She et3").Cells(2, 1)
      Unfortunately this did not work but thanks for your reply.

      Comment

      • MMcCarthy
        Recognized Expert MVP
        • Aug 2006
        • 14387

        #4
        Originally posted by forrestgump
        Unfortunately this did not work but thanks for your reply.
        Why didn't it work? The syntax for referencing the value in the cell is correct.

        Comment

        • forrestgump
          New Member
          • Sep 2007
          • 34

          #5
          Originally posted by msquared
          Why didn't it work? The syntax for referencing the value in the cell is correct.
          I don't know why this does not work I don't get an error messgae but the code does not connect to outlook. below is the VBA I now use with your suggestion:-
          .SendMail ActiveWorkbook. Worksheets("She et3").Cells(2, 1), _
          full code below. Am I doing something wrong?

          [CODE=vb]Sub Mail_ActiveShee t()
          'Working in 97-2007
          Dim FileExtStr As String
          Dim FileFormatNum As Long
          Dim Sourcewb As Workbook
          Dim Destwb As Workbook
          Dim TempFilePath As String
          Dim TempFileName As String

          With Application
          .ScreenUpdating = False
          .EnableEvents = False
          End With

          Set Sourcewb = ActiveWorkbook

          'Copy the sheet to a new workbook
          Sheets("Sheet2" ).Copy
          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
          'We exit the sub when your answer is NO in the security dialog that you only
          'see when you copy a sheet from a xlsm file with macro's disabled.
          If Sourcewb.Name = .Name Then
          With Application
          .ScreenUpdating = True
          .EnableEvents = True
          End With
          MsgBox "Your answer is NO in the security dialog"
          Exit Sub
          Else
          Select Case Sourcewb.FileFo rmat
          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 If
          End With

          ' 'Change all cells in the worksheet to values if you want
          ' With Destwb.Sheets(1 ).UsedRange
          ' .Cells.Copy
          ' .Cells.PasteSpe cial xlPasteValues
          ' .Cells(1).Selec t
          ' End With
          ' Application.Cut CopyMode = False

          'Save the new workbook/Mail it/Delete it
          TempFilePath = Environ$("temp" ) & "\"
          TempFileName = "Part of " & Sourcewb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")

          With Destwb
          .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=Fil eFormatNum
          On Error Resume Next
          .SendMail ActiveWorkbook. Worksheets("She et3").Cells(2, 1), _
          "This is the Subject line"
          On Error GoTo 0
          .Close SaveChanges:=Fa lse
          End With

          'Delete the file you have send
          Kill TempFilePath & TempFileName & FileExtStr[/CODE]
          Last edited by Scott Price; Feb 18 '08, 08:19 PM. Reason: code tags

          Comment

          • MMcCarthy
            Recognized Expert MVP
            • Aug 2006
            • 14387

            #6
            If Destwb is the Workbook where this cell is then change the code to ...

            .SendMail Destwb.Workshee ts("Sheet3").Ce lls(2, 1), "This is the Subject line"

            Comment

            • forrestgump
              New Member
              • Sep 2007
              • 34

              #7
              Originally posted by msquared
              If Destwb is the Workbook where this cell is then change the code to ...

              .SendMail Destwb.Workshee ts("Sheet3").Ce lls(2, 1), "This is the Subject line"
              Unfortunately i still can get this to work. Do you know if you can definiently reference a cell with the .Send mail function?

              Comment

              • MMcCarthy
                Recognized Expert MVP
                • Aug 2006
                • 14387

                #8
                Originally posted by forrestgump
                Unfortunately i still can get this to work. Do you know if you can definiently reference a cell with the .Send mail function?
                1. Test what this is returning

                Destwb.Workshee ts("Sheet3").Ce lls(2, 1)

                2. If it is returning the email address then declare a string variable and pass the value to that first. Then use the variable in the sendmail function.

                Comment

                • forrestgump
                  New Member
                  • Sep 2007
                  • 34

                  #9
                  Originally posted by msquared
                  1. Test what this is returning

                  Destwb.Workshee ts("Sheet3").Ce lls(2, 1)

                  2. If it is returning the email address then declare a string variable and pass the value to that first. Then use the variable in the sendmail function.
                  I'm sorry I am not very strong with VBA I am not quite sure what I need to do?

                  Comment

                  • MMcCarthy
                    Recognized Expert MVP
                    • Aug 2006
                    • 14387

                    #10
                    Originally posted by forrestgump
                    I'm sorry I am not very strong with VBA I am not quite sure what I need to do?
                    Put it in the immediate window and press return.

                    Comment

                    • forrestgump
                      New Member
                      • Sep 2007
                      • 34

                      #11
                      I get the following message:-

                      compile error:
                      Expected:=

                      Comment

                      • MMcCarthy
                        Recognized Expert MVP
                        • Aug 2006
                        • 14387

                        #12
                        Originally posted by forrestgump
                        I get the following message:-

                        compile error:
                        Expected:=
                        Try this ...

                        Destwb.SendMail Destwb.Workshee ts("Sheet3").Ce lls(2, 1), _
                        "This is the Subject line"

                        Comment

                        • forrestgump
                          New Member
                          • Sep 2007
                          • 34

                          #13
                          Originally posted by msquared
                          Try this ...

                          Destwb.SendMail Destwb.Workshee ts("Sheet3").Ce lls(2, 1), _
                          "This is the Subject line"
                          Unfortunately it still does not work. Whenever I try and refer to a cell on the workbook it does not connect to outlook. Strange!

                          Regards,

                          Forrest

                          Comment

                          Working...