Trying to export Tasks to Excel. My code works well, except the code for my last row: It works "most" times, but "some" times it gives me a "Run time error:'1004': Method 'Range' of object '_Global' failed". I've searched and can't figure out what the problem is, so am appealing for help. My code is below and any help is greatly appreciated.
Sub MyTaskList()
Dim strReport As String
Dim olnameSpace As Outlook.NameSpace
Dim TaskFolder As Outlook.MAPIFolder
Dim tasks As Outlook.Items
Dim tsk As Outlook.TaskItem
Dim objExcel As New Excel.Application
Dim exWb As Excel.Workbook
Dim sht As Excel.Worksheet
Dim lngRow As Long
Dim strMyName As String
Dim x As Integer
Dim y As Integer
Set exWb = objExcel.Workbooks.Open("\\12AUST1001FS01\SHARE10011\Budget\SOBUDGET\Direct Delivery Staffing\Team Tracking Tools\Tasks.xlsm")
Set sht = exWb.Sheets("Full List")
sht.Activate
'Here's my problem row *******************************************
lngRow = sht.Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Row
'*****************************************************************
Set olnameSpace = Application.GetNamespace("MAPI")
Set TaskFolder = olnameSpace.GetDefaultFolder(olFolderTasks)
Set tasks = TaskFolder.Items
strReport = ""
y = lngRow
For x = 1 To tasks.Count
Set tsk = tasks.Item(x)
'Fill in Data
'If Not tsk.Complete Then
With sht
.Cells(y, 1) = tsk.Subject
.Cells(y, 2) = tsk.CreationTime
.Cells(y, 3) = tsk.StartDate
.Cells(y, 4) = tsk.DueDate
.Cells(y, 5) = tsk.DateCompleted
.Cells(y, 6) = tsk.PercentComplete
.Cells(y, 7) = GetDelegationState(tsk.DelegationState)
.Cells(y, 8) = tsk.Delegator
.Cells(y, 9) = tsk.Owner
.Cells(y, 11) = tsk.Role
End With
y = y + 1
Next x
'Autofit all column widths
sht.Columns("A:I").EntireColumn.AutoFit
exWb.Save
exWb.Close
Set exWb = Nothing
End Sub
Function GetDelegationState(intState As Integer) As String
Select Case intState
Case 0
GetDelegationState = "Not delegated"
Case 1
GetDelegationState = "Unknown"
Case 2
GetDelegationState = "Accepted"
Case 3
GetDelegationState = "Declined"
End Select
End Function
F
Function GetOwnership(intState As Integer) As String
'Not used
Select Case intState
Case 0
GetOwnership = "New Task"
Case 1
GetOwnership = "Delegated Task"
Case 2
GetOwnership = "Own Task"
End Select
End Function
Function GetResponseState(intState As Integer) As String
'Not used
Select Case intState
Case 0
GetResponseState = "Simple"
Case 1
GetResponseState = "Reassigned"
Case 2
GetResponseState = "Accepted"
Case 3
GetResponseState = "Declined"
End Select
End Function
Bookmarks