What is the Option Strict On replacement for CreateItem?

  • Thread starter Thread starter Dean Slindee
  • Start date Start date
D

Dean Slindee

There are two statements below marked 'ERROR that I would like to recode to
be acceptable to Option Strict On. I tried the two statements at the
bottom, but that was not acceptable. Can anyone Help?

Dim outlook As Outlook.Application

Dim olns As Outlook.NameSpace

Dim apptItem As Outlook.AppointmentItem

Dim mailItem As Outlook.MailItem

outlook = New Outlook.ApplicationClass

olns = outlook.GetNamespace("MAPI")

mailItem = outlook.CreateItem(olMailItem) 'ERROR

apptItem = outlook.CreateItem(olAppointmentItem) 'ERROR



Cannot use statements below because Sub New() is Private:

Dim apptItem As New Outlook.AppointmentItem

Dim mailItem As New Outlook.MailItem



Thanks,

Dean Slindee
 
I think you may have a name space imports problem.

'Assuming:
Imports Microsoft.Office.Core
Imports Microsoft.Office.Interop

'Then:
mailItem = outlook.CreateItem(Outlook.olMailItem)
apptItem = outlook.CreateItem(Outlook.olAppointmentItem)

//I also would suggest changing the variable name (and references to it)
below to something besides outlook to avoid confusion between the variable
name and the Outlook namespace.

Dim outlook As Outlook.Application
 
For Options Strict on, try:

mailItem = CType(outlook.CreateItem(Global.Outlook.OlItemType.olMailItem),
Global.Outlook.MailItem)
apptItem =
CType(outlook.CreateItem(Global.Outlook.OlItemType.olAppointmentItem),
Global.Outlook.AppointmentItem)

HTH
Lee
 
Dean Slindee said:
There are two statements below marked 'ERROR that I would like to recode
to
be acceptable to Option Strict On. I tried the two statements at the
bottom, but that was not acceptable. Can anyone Help?

Dim outlook As Outlook.Application

Dim olns As Outlook.NameSpace

Dim apptItem As Outlook.AppointmentItem

Dim mailItem As Outlook.MailItem

outlook = New Outlook.ApplicationClass

olns = outlook.GetNamespace("MAPI")

mailItem = outlook.CreateItem(olMailItem) 'ERROR

\\\
mailItem = DirectCast(outlook.CreateItem(...), Outlook.AppointmentItem)
///
apptItem = outlook.CreateItem(olAppointmentItem) 'ERROR

\\\
.... = DirectCast(..., Outlook.MailItem)
///

In addition to that, I suggest to import the 'Outlook' namespace.
 
Herfried,
| In addition to that, I suggest to import the 'Outlook' namespace.

With Outlook (and other Office apps) I normally use an Import Alias, to
avoid conflicts between System.Windows.Forms.Application and the Application
type in the respective office app.

For example with Outlook I normally use:

Imports Outlook = Microsoft.Office.Interop.Outlook

Then my code looks similiar to Dean's...

Hope this helps
Jay



| > There are two statements below marked 'ERROR that I would like to recode
| > to
| > be acceptable to Option Strict On. I tried the two statements at the
| > bottom, but that was not acceptable. Can anyone Help?
| >
| > Dim outlook As Outlook.Application
| >
| > Dim olns As Outlook.NameSpace
| >
| > Dim apptItem As Outlook.AppointmentItem
| >
| > Dim mailItem As Outlook.MailItem
| >
| > outlook = New Outlook.ApplicationClass
| >
| > olns = outlook.GetNamespace("MAPI")
| >
| > mailItem = outlook.CreateItem(olMailItem) 'ERROR
|
| \\\
| mailItem = DirectCast(outlook.CreateItem(...), Outlook.AppointmentItem)
| ///
|
| > apptItem = outlook.CreateItem(olAppointmentItem) 'ERROR
|
| \\\
| ... = DirectCast(..., Outlook.MailItem)
| ///
|
| In addition to that, I suggest to import the 'Outlook' namespace.
|
| --
| M S Herfried K. Wagner
| M V P <URL:http://dotnet.mvps.org/>
| V B <URL:http://classicvb.org/petition/>
|
 

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

Back
Top