对象不支持属性或方法-工作表数组

时间:2019-07-20 09:36:33

标签: arrays excel vba outlook

我有一个具有不同帐户详细信息的主文件。我正在尝试编写一个代码,该代码将在删除不必要的帐户详细信息并将其邮寄后发送该工作簿的副本。一张纸工作正常,但是当我在多张纸上使用数组时,它给我的对象不支持此代码行“ .DisplayPageBreaks = False ”中的方法。 这是我的代码:

Sub Mail_Sheets_Array()
'Working in Excel 2000-2016
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
    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

    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("REC_INT", "REC_EXT")).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 = "Part of " & Sourcewb.name & " " & Format(Now, "dd-mmm-yy h-mm-ss")

'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 Dim Firstrow As Long
    Dim Lastrow As Long
    Dim Lrow As Long
    Dim prow As Long
    Dim r As Long
    Dim x As Long
    Dim y As Long
    Dim CalcMode As Long
    Dim ViewMode As Long

r = 0
    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
    End With

    'We use the ActiveSheet but you can replace this with
    With Destwb.Sheets(Array("REC_INT", "REC_EXT"))
   ' With ActiveSheet

        'We select the sheet so we can change the window view
        .Select

        'If you are in Page Break Preview Or Page Layout view go
        'back to normal view, we do this for speed
        ViewMode = ActiveWindow.View
        ActiveWindow.View = xlNormalView

        'Turn off Page Breaks, we do this for speed
        .DisplayPageBreaks = False

        'Set the first and last row to loop through
        Firstrow = .UsedRange.Cells(1).Row
        Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row

        'We loop from Lastrow to Firstrow (bottom to top)
        For Lrow = 9 To Lastrow Step 1

            'We check the values in the A column in this example
            With .Cells(Lrow, "C")

                If Not IsError(.Value) Then

                If Cells(Lrow, "D").Value = "Total" Then
                GoTo y
                End If

                If .Value = Sheet1.Cells(2, 6) Then
                r = r + 1
                End If

                    If .Value <> Sheet1.Cells(2, 6) Then

                     If .Value = "" Then
                     r = 0
                     End If

                     prow = Lrow - r


                     If Cells(prow, "C").Value = Sheet1.Cells(2, 6) Then
                     r = r + 1
                     GoTo x

                     End If

                     .EntireRow.ClearContents
                    'This will delete each row with the Value "ron"
                    'in Column A, case sensitive.
x:
                End If
                End If

            End With

        Next Lrow

    End With

    ActiveWindow.View = ViewMode
    With Application
        .ScreenUpdating = True
        .Calculation = CalcMode
    End With
y:
Range(Cells(9, 3), Cells(Lrow, 3)). _
    SpecialCells(xlCellTypeBlanks).EntireRow.Delete



 'xxxxxxxxxxxxxxxxxx

    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 = "hadi@siemens.com"
            .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")
            '.Send   '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

1 个答案:

答案 0 :(得分:1)

您正在尝试运行一次仅在指定工作表上支持的property,一次在多个工作表上运行。不支持此功能,因此会出现错误438。

您可以尝试循环这些工作表:

For Each ws In Destwb.Sheets
    If ws.Name = "REC_INT" or ws.Name = "REC_EXT" then
        Destwb.Worksheets(ws.Name).DisplayPageBreaks = False
    End if
Next ws

我认为您无需检查工作表名称,因为您只需将它们中的两个复制到新的工作簿中即可。