Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88835

Finding last row throws an object required error

$
0
0

What I am trying to do.

  1. I highlight some text in an email then run my macro.
  2. It 'copies' the highlighted text and stores it in variable strText.
  3. Then it creates a file called Artwork List.xlsx if it does not exist and if it exists it opens it.
  4. After that it copies the text into the file in column A row 1 if the lastrow is 1, and if not, it appends to lastrow + 1

My code throws

'Run-time error 424, Object required'

To narrow down, the error should be coming from:

lastrow = .Cells(Rows.Count, 1).End(xlUp).Row

or anything related to this line.

Sub copyArts2File()
MsgBox "ok"
Dim OutApp As Object
Dim OutMail As Object
Dim olInsp As Object
Dim wdDoc As Object
Dim strText As String

Dim xlApp As Object
Dim xlBook As Object
Dim xlSheet As Object

Dim strTextArr As Variant

On Error Resume Next
'Get Outlook if it's running
Set OutApp = GetObject(, "Outlook.Application")

'Outlook wasn't running, so cancel
If Err <> 0 Then
    MsgBox "Outlook is not running so nothing can be selected!"
    GoTo lbl_Exit
End If
On Error GoTo 0

Set OutMail = OutApp.ActiveExplorer.Selection.Item(1)
With OutMail
    Set olInsp = .GetInspector
    Set wdDoc = olInsp.WordEditor
    strText = wdDoc.Application.Selection.Range.Text
End With
'MsgBox strText

lbl_Exit:
Set OutMail = Nothing
Set OutApp = Nothing
Set olInsp = Nothing
Set wdDoc = Nothing

Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False

FileName = "Artwork List.xlsx"
fileDoesExist = Dir("C:\Users\quaer\Desktop\DL Arts\"& FileName) > ""' Check for existing file
If fileDoesExist Then
    ' Open Excel file
    Set xlBook = xlApp.Workbooks.Open("C:\Users\quaer\Desktop\DL Arts\"& FileName)
    Set xlSheet = xlBook.Sheets(1)
Else
    ' Add Excel file
    Set xlBook = xlApp.Workbooks.Add
    With xlBook
        .SaveAs FileName:="C:\Users\quaer\Desktop\DL Arts\"& FileName
    End With
    Set xlSheet = xlBook.Sheets(1)
End If

' Do stuff with Excel workbook
Dim i As Integer
Dim lastrow As Long
With xlApp
    With xlBook
        With xlSheet
        strTextArr = Split(strText, "Adding file")
        lastrow = .Cells(Rows.Count, 1).End(xlUp).Row
        MsgBox lastrow
        If lastrow = 1 Then

            For i = 1 To UBound(strTextArr)
                .Range("A"& i).Value = strTextArr(i)
            Next i

        Else
            For i = 1 To UBound(strTextArr)
                .Range("A"& (i + lastrow)).Value = strTextArr(i)
            Next i

        End If
        .Close SaveChanges:=True
        End With
    End With
End With
xlApp.Visible = True
Exit Sub
End Sub

Viewing all articles
Browse latest Browse all 88835

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>