I receive everyday reports with tables in outlook, I want to create a macro that can copy the table in a new excel file and save it to a folder in my desktop, I have this code that it is working with small tables, 100 or 200 rows, but it fails with bigger tables with 2000 rows or more, the error is this:
Run-time error '-2147417848 (80010108)
Method 'Copy' of object 'Range' failed
but if I run the macro with the email open it works with the big tables, so I think the problem is maybe with the time that takes to copy the tables, but dont know what to do, can someone give me a suggestion please?
Sub test5()
Dim item As MailItem, x%
Dim r As Object 'As Excel.Range
Dim doc As Object 'As Excel.Document
Dim xlApp As Object, wkb As Object
Dim FTW As String
For Each item In Application.ActiveExplorer.Selection
Set xlApp = CreateObject("Excel.Application")
Set wkb = xlApp.Workbooks.Add
xlApp.Visible = True
Dim wks As Object
Set wks = wkb.Sheets(1)
Set doc = item.GetInspector.WordEditor
For x = 1 To doc.tables.Count
Set r = doc.tables(1)
doc.tables(1).Range.Copy
wks.Paste
wks.Cells(wks.Rows.Count, 1).End(3).Offset(1).Select
FTW = wks.Cells(1, 1)
FTW = Replace(FTW, ":", " ")
wkb.SaveAs filename:="C:\temp" & FTW & Format(item.CreationTime, "yyyymmdd_hhnnss_") & ".xlsx"
wkb.Close
Next
Next
End Sub
PS: I am not a programmer and dont know about VBA, I was just reading other forums and trying to adapt the code, so I am not sure if this is the best option.
Run-time error '-2147417848 (80010108)
Method 'Copy' of object 'Range' failed
but if I run the macro with the email open it works with the big tables, so I think the problem is maybe with the time that takes to copy the tables, but dont know what to do, can someone give me a suggestion please?
Sub test5()
Dim item As MailItem, x%
Dim r As Object 'As Excel.Range
Dim doc As Object 'As Excel.Document
Dim xlApp As Object, wkb As Object
Dim FTW As String
For Each item In Application.ActiveExplorer.Selection
Set xlApp = CreateObject("Excel.Application")
Set wkb = xlApp.Workbooks.Add
xlApp.Visible = True
Dim wks As Object
Set wks = wkb.Sheets(1)
Set doc = item.GetInspector.WordEditor
For x = 1 To doc.tables.Count
Set r = doc.tables(1)
doc.tables(1).Range.Copy
wks.Paste
wks.Cells(wks.Rows.Count, 1).End(3).Offset(1).Select
FTW = wks.Cells(1, 1)
FTW = Replace(FTW, ":", " ")
wkb.SaveAs filename:="C:\temp" & FTW & Format(item.CreationTime, "yyyymmdd_hhnnss_") & ".xlsx"
wkb.Close
Next
Next
End Sub
PS: I am not a programmer and dont know about VBA, I was just reading other forums and trying to adapt the code, so I am not sure if this is the best option.