简体   繁体   English

全局对象失败的方法(一切)

[英]Method (everything) of object Global failed

I have a macro which takes the body of an email, splits it into an array and places it into excel. 我有一个宏,它接收电子邮件的正文,将其拆分为一个数组并将其放入excel。 It then uses colons to split rows into [label] and [data]. 然后,它使用冒号将行拆分为[label]和[data]。

For some reason it has stopped working. 由于某种原因,它已停止工作。 I had some good help here but it has now failed in the second subroutine and I can't get my head around the error. 我在这里有一些很好的帮助但现在它在第二个子例程中失败了,我无法回避该错误。 I am sure it's something simple, possibly related to running from outlook or incorrect definition of ranges. 我确信这很简单,可能与从Outlook运行或错误的范围定义有关。 Everything using rows, cells, range object etc gives this error. 使用行,单元格,范围对象等的所有操作都会出现此错误。

The exact error is Runtime 1004 error. Method [cells, rows] of object Global failed 确切的错误是Runtime 1004 error. Method [cells, rows] of object Global failed Runtime 1004 error. Method [cells, rows] of object Global failed

I have used a comment to mark the point where problems begin: 我已使用注释来标记问题开始的地方:

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

I believe your problem is that you are not fully defining your Range , Cells , & Columns functions. 我相信您的问题是您没有完全定义RangeCellsColumns函数。 Try adding oXLWs. 尝试添加oXLWs. before each of those functions, or add a With oXLWs line and add . 在每个函数之前,或添加With oXLWs行并添加. before each of those functions and see what happens. 在每个功能之前,先看看会发生什么。

If you run this in Excel, the Range , Cells , & Columns functions will work because they will refer to the active worksheet. 如果在Excel中运行,则RangeCellsColumns函数将起作用,因为它们将引用活动的工作表。 If you run this through Outlook, it may not work if the sheet is not active. 如果您通过Outlook运行此功能,则如果工作表处于非活动状态,则可能无法正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM