对象_global的方法(一切)都失败了

时间:2014-12-09 10:24:03

标签: excel vba outlook

我有一个宏,它接收电子邮件的正文,将其拆分为数组并将其放入Excel中。然后使用冒号将行拆分为[label]和[data]。

由于某种原因,它已停止工作。我有一些很好的帮助here但它现在在第二个子程序中失败了,我无法理解错误。我确信这很简单,可能与从outlook运行或不正确的范围定义有关。使用行,单元格,范围对象等的所有内容都会产生此错误。

确切错误为Runtime 1004 error. Method [cells, rows] of object Global failed

我使用评论来标记问题开始的地方:

Private oXLApp As Object, oXLWb As Object, oXLWs As Object
Sub Thermo_to_excel()
    Dim myOlApp As Object, mynamespace As Object
    Dim ThermoMail As Object
    Dim msgText, delimtedMessage, Delim1 As String



    Set myOlApp = Outlook.Application
    Set mynamespace = myOlApp.GetNamespace("mapi")
    Set ThermoMail = Application.ActiveInspector.CurrentItem

    delimtedMessage = ThermoMail.Body

    '~~> Establish an EXCEL application object
    On Error Resume Next
    Set oXLApp = GetObject(, "Excel.Application")

    '~~> If not found then create new instance
    If Err.Number <> 0 Then
        Set oXLApp = CreateObject("Excel.Application")
    End If
    Err.Clear
    On Error GoTo 0

    Set oXLWb = oXLApp.Workbooks.Add
    Set oXLWs = oXLWb.Sheets("Sheet1")

    'Truncated [Array definition goes here]

    With oXLWs
        .Range(.Cells(1, 1), .Cells(lastRow, 1)).Value = _
        oXLApp.WorksheetFunction.Transpose(messageArray)
    End With

    Call splitAtColons
    ThermoMail.Close (olDiscard)
End Sub

Sub splitAtColons()

Dim Roows As Integer
'PROBLEMS start here now
Roows = Cells(oXLWs.Rows.Count & "," & oXLWs.ActiveCell.Column).End(xlUp).Row 

Range("A1").Select
Range("A1:B" & Roows).Font.Name = Range("B1").Font.Name
Range("A1:B" & Roows).NumberFormat = "@"



'Application.ScreenUpdating = False
Do Until Z = Roows

If Not InStr(ActiveCell.Value, ":") = 0 Then
    Cells(ActiveCell.Row, 2).Value = Trim(Mid(ActiveCell.Value, InStr(ActiveCell.Value, ":") + 1))
    ActiveCell.Value = Left(ActiveCell.Value, InStr(ActiveCell.Value, ":"))
Else
    Cells(ActiveCell.Row, 2).Value = Trim(ActiveCell.Value)
    ActiveCell.Value = ""
End If
If ActiveCell.Value = "" And Range("B" & ActiveCell.Row).Value = "" Then
ActiveCell.EntireRow.Delete
Roows = Roows - 1
Z = Z - 1
End If


    Range("A" & Z + 2).Select
    Z = Z + 1
Loop
Columns("B:B").EntireColumn.AutoFit
Columns("A:A").EntireColumn.AutoFit
'Application.ScreenUpdating = True
End Sub

1 个答案:

答案 0 :(得分:0)

我相信您的问题是您没有完全定义RangeCells和&amp; Columns功能。尝试在每个功能之前添加oXLWs.,或添加With oXLWs行并在每个功能之前添加.,看看会发生什么。

如果您在Excel中运行此功能,RangeCells和&amp; Columns函数将起作用,因为它们将引用活动工作表。如果通过Outlook运行此操作,则在工作表未处于活动状态时可能无效。