Convert to late binding


Hi all,

I have the following code to delete an Outlook task in Acess 2003 vba.

Private Function DeleteCompletedTasks()
    Dim otaskitem As Outlook.TaskItem
    Dim ofolder As Outlook.Folder
     
    'Get the Task items
    Set ofolder = Outlook.Session.GetDefaultFolder(olFolderTasks)
    For Each Item In ofolder.Items
    Set otaskitem = Item
     
    'Check the Category of the specific Taskitem
    If otaskitem.Categories = "Test" Then otaskitem.Delete
    Next
End Function

Open in new window


It works perfectly but now I need to convert it to late binding instead of early
Can somehelp with the conversion please?

Thanks
Kelso
kelsoRAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mbizupNerdCommented:
Give this a try:

Private Function DeleteCompletedTasks()
    Dim otaskitem As Object
    Dim ofolder As Object
    Dim intOLFolderTasks as integer
    intOLFolderTasks = 13
     
    'Get the Task items
    Set ofolder = Outlook.Session.GetDefaultFolder(intOLFolderTasks)
    For Each Item In ofolder.Items
    Set otaskitem = Item
     
    'Check the Category of the specific Taskitem
    If otaskitem.Categories = "Test" Then otaskitem.Delete
    Next
End Function 

Open in new window

kelsoRAuthor Commented:

Thanks but it failed at:

Set ofolder = Outlook.Session.GetDefaultFolder(intOLFolderTasks)

Open in new window


So I included this:

Dim OutlookApp As Object
Set OutlookApp = CreateObject("Outlook.Application")

Open in new window


It passed and worked then, but I need to run the code 3 times to get rid of all the taks in the spesific Category.
Do you know why that is? I suppose the code is still not 100%
mbizupNerdCommented:
I missed that (You're right - you do need to create an outloojk application object)

See if this does the trick:

Private Function DeleteCompletedTasks()
    Dim otaskitem As Object
    Dim ofolder As Object
    Dim intOLFolderTasks as integer
    Dim OutlookApp As Object
    Set OutlookApp = CreateObject("Outlook.Application") 

    intOLFolderTasks = 13
     
    'Get the Task items
    ' Use the outlook application object here:
    Set ofolder = OutlookApp.Session.GetDefaultFolder(intOLFolderTasks)
    For Each Item In ofolder.Items
    Set otaskitem = Item
     
    'Check the Category of the specific Taskitem
    If otaskitem.Categories = "Test" Then otaskitem.Delete
    Next
End Function  

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

mbizupNerdCommented:
Also try this:

Private Function DeleteCompletedTasks()
    Dim otaskitem As Object
    Dim ofolder As Object
    Dim intOLFolderTasks as integer
    Dim OutlookApp As Object
    Set OutlookApp = CreateObject("Outlook.Application") 

    intOLFolderTasks = 13
     
    'Get the Task items
    ' Use the outlook application object here:
    Set ofolder = OutlookApp.Session.GetDefaultFolder(intOLFolderTasks)
    For Each otaskitem In ofolder.Items
     
    'Check the Category of the specific Taskitem
    If otaskitem.Categories = "Test" Then otaskitem.Delete
    Next
End Function   

Open in new window

kelsoRAuthor Commented:
Thanks for that the code works.
Dale FyeOwner, Developing Solutions LLCCommented:
Just my technique, but I generally do it this way:

    Dim otaskitem As Object                   'Outlook.TaskItem'
    Dim ofolder As Object                        'Outlook.Folder'

This gives me the abilitiy to simply cut and paste the actual early binding object declaration and put it back if I need to debug or modify the procedure, and want to get the advantage of intellisense.
kelsoRAuthor Commented:
Thanks a lot for that Fyed - will add it to my code.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.