Excel to Schedule Task in Outlook

R

ryguy7272

I get this message:
Automation error. The specified module could not be found.

When I run this code:
Sub CreateTask()
Dim objApp As Outlook.Application
Dim objTask As Outlook.TaskItem

Set objApp = CreateObject("Outlook.Application")
Set objTask = objApp.CreateItem(olTaskItem)
With objTask
.Subject = "Subject"
.StartDate = CDate(ProximaFechaAcumulada) - CDate(15)
.DueDate = CDate(ProximaFechaAcumulada)
.Importance = olImportanceHigh

.Body = "Body" 'Here I need Right to Left

.Save
.ReminderSet = True
End With
End Sub

It fails on this line:
Set objApp = CreateObject("Outlook.Application")

I don’t understand this behavior. I have checked the Reference to MS
Outlook Object Library on my system and I’m having a problem with the code.
I tried to create a small tool to simplify things in our office, but it just
doesn’t seem to work right. When I go to a few other machines in the office
it runs fine. I can’t tell what’s going on. Perhaps there is some kind of
esoteric setting that is different on my workstation... I’d appreciate it if
someone could share some insight.


Regards,
Ryan--
 
M

Matthew Pfluger

By chance do you have Outlook running when trying to run this macro? If so,
the code will fail because only one instance of OUtlook can be open at any
time.

Instead, try this code:

Sub GetOutlookReference()

'Outlook objects
Dim olApp As Outlook.Application

'Obtain a reference to Outlook
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")

'If Outlook isn't running, start it and remember
If olApp Is Nothing Then
Set olApp = CreateObject("Outlook.Application")
End If

' If Outlook still isn't running, Outlook cannot open or is not installed
If olApp Is Nothing Then
Call MsgBox("Outlook could not be opened. Exiting macro.", _
vbCritical, Application.Name)
End If

End Sub

I cannot test this code since we do not have Outlook at work, but if you
still need help, I can test it at home. Otherwise, here's a link to the
Outlook VBA Newsgroup:

http://msdn.microsoft.com/newsgroups/default.aspx?dg=microsoft.public.office.developer.outlook.vba

Matthew Pfluger
 
R

ryguy7272

Thanks for looking at this Matt!

If I open Outlook and run your coed, nothing happens. If I close Outlook
and run your code, I get an error message: 'Outlook could not be opened.
Exiting macro'. I guess olApp = Nothing...

This is the code that I am trying to run now:
Sub GetOutlookReference()

'Outlook objects
Dim olApp As Outlook.Application

'Obtain a reference to Outlook
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")

'*********************************************
Dim objApp As Object
Dim OutTask As Object

Set objApp = CreateObject("Outlook.Application")
Set OutTask = objApp.CreateItem(olTaskItem)
With OutTask


.StartDate = Range("C1")
.DueDate = Range("C2")
.Importance = olImportanceHigh
.Display
.ReminderSet = True
End With
'*********************************************

'If Outlook isn't running, start it and remember
If olApp Is Nothing Then
Set olApp = CreateObject("Outlook.Application")
End If

' If Outlook still isn't running, Outlook cannot open or is not installed
If olApp Is Nothing Then
Call MsgBox("Outlook could not be opened. Exiting macro.", _
vbCritical, Application.Name)
End If

End Sub

Basically, nothing happens when the code fires.
Not sure what to make of all this. Got any other thoughts?

Regards,
Ryan---
 
J

JP

Try this, it is a full replacement for your code. i.e delete your
existing code and replace it with this.

Sub GetOutlookReference()

Dim olApp As Outlook.Application
Dim OutTask As Outlook.TaskItem

On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
If Err.Number <> 0 Then
Set olApp = CreateObject("Outlook.Application")
End If
On Error GoTo 0

If olApp Is Nothing Then
MsgBox "Cannot start Outlook.", vbExclamation
Exit Sub
End If

Set OutTask = olApp.CreateItem(olTaskItem)

With OutTask
.StartDate = range("C1")
.DueDate = range("C2")
.Importance = olImportanceHigh
.Display
.ReminderSet = True
End With

End Sub


HTH,
JP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top