Outlook Task Snag

G

Guest

I am trying to progamatically create tasks in Outlook using VB.Net. I am
using the following code:
****
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim olFolder As Outlook.MAPIFolder
Dim olTaskItem As Outlook.TaskItem
Dim strbodyText As String
olApp = CreateObject("Outlook.Application")
olNS = olApp.GetNamespace("MAPI")
olFolder = olNS.GetDefaultFolder(olFolderTasks)
olTaskItem = olFolder.Items.Add("IPM.Task")
With olTaskItem
.Subject = "Rural Inspections "
.ReminderTime = "2:00:00 PM"
.ReminderSet = True
.Categories = "Rural Projects"
.Save()
End With
olTaskItem = Nothing
olFolder = Nothing
olNS = Nothing
olApp = Nothing
End Sub
****
VB tells me that "Name 'olFolderTasks' is not declared" Line 52.
I'm not sure what else to do to declare this variable. Should I delclare it
with "Dim" like the others above? If so suppose it would be something like:
Dim olFolderTasks As Outlook.??????
I'm pretty new to VB so sorry if this is a basic question.

Also, I would like to put the date on the subject line. In Excel I can use:
..Subject = "Rural Inspections " & Date$
but VB won't allow Date$, Date, or Date() here.

Thanks in advance.
 
G

Guest

Tosch,
Outlook.OlDefaultFolders.olFolderTasks show up as not being defined. If I
remove the .olFolderTasks and put quotes around the olFolderTasks to make the
line look like this:
olFolder = olNS.GetDefaultFolder("olFolderTasks")
I get the following error:
An unhandled exception of type 'System.InvalidCastException' occurred in
microsoft.visualbasic.dll
Additional information: Cast from string "olFolderTasks" to type 'Integer'
is not valid.

The line that is highlited is
olFolder = olNS.GetDefaultFolder("olFolderTasks")

Any ideas?
 
G

Guest

OK, so the code that finally worked is below. Note change in line 10 . No
need to declare "olFolderTasks". Still have one more problem. I want to
attach an excel document to the task. Using .Attachments.Add = <filepath>
does not work. I get:

Argument not specified for parameter 'Source' of 'Public Overridable
Function Add(Source As Object, [Type As Object], [Position As Object],
[DisplayName As Object]) As Outlook.Attachment'.

NO idea what that means.

Any ideas?
****
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim olFolder As Outlook.MAPIFolder
Dim olTaskItem As Outlook.TaskItem
Dim strbodyText As String
olApp = CreateObject("Outlook.Application")
olNS = olApp.GetNamespace("MAPI")
olFolder =
olNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks)
olTaskItem = olFolder.Items.Add("IPM.Task")
With olTaskItem
.Subject = "Rural Inspections "
.DueDate = Today
.ReminderTime = Now
.ReminderSet = True
.Categories = "Rural Projects"
.Save()
End With
olTaskItem = Nothing
olFolder = Nothing
olNS = Nothing
olApp = Nothing
Me.Close()
End Sub
End Class
****
 

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