+ Reply to Thread
Results 1 to 6 of 6

Excel email macro failing on sheet array

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-16-2008
    Location
    Mansfield, TX
    Posts
    324

    Excel email macro failing on sheet array

    Hello, I am trying to adapt a macro from Ron DeBruins website to email an array of sheets but it is failing when it is trying to determine which sheets to send. The error message says "Run-time error '9': Subscript out of range" A second question I have is how do I change this to only email the visible sheets?

    This is the line it is failing on:
    .Sheets(Array("Supplier Instruction Sheet", "R&D Request-Protein", "Sample Analysis", "Sample Arrival Summary", "Protein R&D Formula Pricing", "Protein-Prelim Spec")).Copy
    The whole macro:

    Sub Prot_Mail_Sheets_Array()
    'Working in 2000-2010
        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
        Dim cell As Range
        Dim strto As String
        
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
    
        Set Sourcewb = ActiveWorkbook
    
        'Copy the sheets to a new workbook
        '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(Array("Supplier Instruction Sheet", "R&D Request-Protein", "Sample Analysis", "Sample Arrival Summary", "Protein R&D Formula Pricing", "Protein-Prelim Spec")).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-2010, we exit the sub when your answer is
                'NO in the security dialog that you only see  when you copy
                'an 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 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 = "Part of " & Sourcewb.Name & " " _
                     & 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
            For Each cell In ThisWorkbook.Sheets("Ranges").Range("J3:L3")
            If cell.Value Like "?*@?*.?*" Then
                strto = strto & cell.Value & ";"
            End If
        Next cell
        If Len(strto) > 0 Then strto = Left(strto, Len(strto) - 1)
            With OutMail
                .To = strto
                .CC = ""
                .BCC = ""
                .Subject = "This is the Subject line"
                .Body = "Hi there"
                .Attachments.Add Destwb.FullName
                'You can add other files also like this
                '.Attachments.Add ("C:\test.txt")
                .display   'or use .Display
            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
    End Sub
    Thanks for any guidance you can give!

    Clayton Grove

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Excel email macro failing on sheet array

    Hello Clayton,

    Your syntax is correct for copying multiple sheets into a new workbook. The error then is caused by a sheet name either being misspelled or it doesn't exist. Hiding the sheet would not cause this error.

    I have change the macro to copy all the visible sheets in the source workbook. If you need only certain sheets to be copied then the macro need a small change.
    Sub Prot_Mail_Sheets_Array()
    'Working in 2000-2010
        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 N As Integer
        Dim OutApp As Object
        Dim OutMail As Object
        Dim sh As Worksheet
        Dim Sht As Object
        Dim Shts() As Variant
        Dim TheActiveWindow As Window
        Dim TempWindow As Window
        Dim cell As Range
        Dim strto As String
        
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
    
        Set Sourcewb = ActiveWorkbook
    
        'Copy the sheets to a new workbook
        '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
              For Each Sht In .Sheets
                If Sht.Visible = True Then
                   ReDim Preserve Shts(N)
                   Shts(N) = Sht.Name
                   N = N + 1
                End If
              Next Sht
            Shts.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-2010, we exit the sub when your answer is
                'NO in the security dialog that you only see  when you copy
                'an 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 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 = "Part of " & Sourcewb.Name & " " _
                     & 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
            For Each cell In ThisWorkbook.Sheets("Ranges").Range("J3:L3")
            If cell.Value Like "?*@?*.?*" Then
                strto = strto & cell.Value & ";"
            End If
        Next cell
        If Len(strto) > 0 Then strto = Left(strto, Len(strto) - 1)
            With OutMail
                .To = strto
                .CC = ""
                .BCC = ""
                .Subject = "This is the Subject line"
                .Body = "Hi there"
                .Attachments.Add Destwb.FullName
                'You can add other files also like this
                '.Attachments.Add ("C:\test.txt")
                .Display   'or use .Display
            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
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Contributor
    Join Date
    02-16-2008
    Location
    Mansfield, TX
    Posts
    324

    Re: Excel email macro failing on sheet array

    Leith, thank you for the assistance! I was hoping you would stumble into my thread! I ran your macro but it failed with an error that says "Compile Error: Invalid qualifier" on the section below:

     With Sourcewb
            Set TheActiveWindow = ActiveWindow
            Set TempWindow = .NewWindow
              For Each Sht In .Sheets
                If Sht.Visible = True Then
                   ReDim Preserve Shts(N)
                   Shts(N) = Sht.Name
                   N = N + 1
                End If
              Next Sht
            Shts.Copy
        End With
    On this line:
          Shts.Copy

    Any ideas what is causing this?

    Thanks again!

    Clayton Grove

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Excel email macro failing on sheet array

    Hello Clayton,

    That was fast! Sorry, got rushed and I made a typo. That line should read:
      .Sheets(Array(Shts)).Copy

  5. #5
    Forum Contributor
    Join Date
    02-16-2008
    Location
    Mansfield, TX
    Posts
    324

    Re: Excel email macro failing on sheet array

    No worries! I changed that line of code and now is give me an error that says "method 'copy'of object 'Sheets' failed" on that same line.

    .Sheets(Array(Shts)).Copy

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Excel email macro failing on sheet array

    Hello Clayton,

    Can you post a copy of your workbook so I can find out why this error keeps popping up?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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