I have a macro which takes the body of an email, splits it into an array and places it into excel. It then uses colons to split rows into [label] and [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. 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
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. Try adding oXLWs.
before each of those functions, or add a With oXLWs
line and add .
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. If you run this through Outlook, it may not work if the sheet is not active.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.