Creating a Task in Outlook with Access VBA

eschrob

Registered User.
Local time
Yesterday, 18:16
Joined
Apr 28, 2019
Messages
24
I've found this code on-line and tried to adapt it to my Access Database.

I want to Create an Outlook Task. But for some reason this code is creating an EMAIL! I can't figure out why. Any clue?

Private Sub Command202_Click()
Dim OlApp As Object
Dim OlTask As Object

Set OlApp = CreateObject("Outlook.Application")
Set OlTask = OlApp.CreateItem(olTaskItem)

With OlTask
.Subject = Me.Account & " " & Me.WBPN & " " & Me.WBNextAction
.body = Me.WBStatus
.Display
End With

End Sub

Thank you!
 
Do you have a reference set to Outlook?
 
Great question. I don't know how to create a reference to Outlook.

I did have this in my code but the debugger stopped this.

Dim OLApp As Outlook.Application
Dim OLTask As Outlook.TaskItem

Is that what you mean?

Also, the code below creates an email. Why would it do that?
 
Great question. I don't know how to create a reference to Outlook.

I did have this in my code but the debugger stopped this.

Dim OLApp As Outlook.Application
Dim OLTask As Outlook.TaskItem

Is that what you mean?

Also, the code below creates an email. Why would it do that?

If the compiler complains about that code, then you probably don’t have a reference to Outlook. Your previous code indicates late binding, but I’m not sure how you can use olTaskItem unless you declared it as a constant. Can you post your complete code, so we can tell?
 
I don't know how to create a reference to Outlook.

While I agree with TheDBGuy that the code uses the concept of late binding which affects the way you reference things, I'll just answer the other question - how to make a reference.

When you want to create a reference, you are doing that because you want to use something from that reference in some code segment that you want to write. So... you first open up the VBA code window, which you do by clicking on the "Database Tools" tab in the ribbon. On that ribbon you will see the Visual Basic icon, which you click. That brings up a VB Code page. In the menu bar click Tools and then from the drop-down, click References.

You get a little complex dialog box with a scrollable section that contains check boxes and names. Each of those names is a reference library module. Scroll down far enough and you will find the Microsoft Outlook nn.n Object Library. There will be a number between "Outlook" and "Object" that depends on your version of Office. Check that box then close the References dialog. You have just made a reference to Outlook. Note that there are other Outlook references available, but all you want is the Object Library.
 
JHB has nailed it pretty well. The default of creating an item is 0 which is a Mail item. So that says that the olTaskItem didn't resolve. By any chance do you NOT have Option Explicit in your code's declaration area? Because if you do not, then the mechanism is that the VBA compiler would treat symbol olTaskItem as an undefined variable and would simply instantiate it as zero. If you had Option Explicit, then you would have gotten a definition error.
 
Thanks The_Doc_Man and JHB. That fixed it. As I noted in my intro, I've not really learned VBA, I'm just trying to find examples and apply them to my need. This is a great example of the example I found got me close. And you helped me get all the way there. I realize my code may not be perfect (and I don't know if it will stand the test of time).

The_Doc_Guy,
I'll have look at the VBA Tools and References page to try to follow your suggestion as well.

For now, this code seems to work:

Private Sub Command202_Click()
Dim OlApp As Object
Dim OlTask As Object

Set OlApp = CreateObject("Outlook.Application")
Set OlTask = OlApp.CreateItem(3)

With OlTask
.Subject = Me.Account & " " & Me.WBPN & " " & Me.WBNextAction
.body = Me.WBStatus
.Display
End With


It puts the "Account", "WBPN" and "WBNext Action" into the Subject Line of the task and "WBSTatus" into the Body of the task. I can take it from here.

Thanks again,
 
The_Doc_Man-
I did as you suggested and found the Microsoft Outlook nn.n Object Library and checked it.
Is there anything else I should add to my code?
Should I add the Option Explicit in my declaration area too?
 
I found another error which I didn't expect. Sometimes, some of the fields I'm using for my Task may be blank (maybe nothing in the body). It seems if there is a blank field, MS Access is returning an error. For example in the below, if WBStatus has nothing, I get the error- Run-time error '214735571 (80020005)': Type Mismatch- Cannot coerce parameter value. Outlook cannot translate your string.

Private Sub Command202_Click()
Dim OlApp As Object
Dim OlTask As Object

Set OlApp = CreateObject("Outlook.Application")
Set OlTask = OlApp.CreateItem(3)

With OlTask
.Subject = "[WB]" & " " & Me.Account & " " & Me.WBPN & " " & Me.WBNextAction
.body = Me.WBStatus
.StartDate = Date
.DueDate = Me.WBDateNextAction
.Display
End With

End Sub
 
Hi. For empty or null data, you can use the Nz() function to provide a default value.
 
Post 8 was moderated, I'm posting to trigger email notifications.
 

Users who are viewing this thread

Back
Top Bottom