Outlook not Defined/Declared

lclift61

New member
Joined
Nov 19, 2013
Messages
3
Programming Experience
Beginner
Hello
First of all apologies for cross posting as I have posted this question on the MSDN network; I'm in a real bind and need some help to meet a deadline.

I'm writing a sub to create an email in Outook 2010 from a vb application designed in Visual Basic 2010 Express. I have added the Microsoft Outlook 14.0 Object Library, Microsoft Outlook View Control and Microsoft Office 14.0 Object Library to my references but the code below returns a number of errors along the lines of:

outlook._Namespace is not defined
outlook.Application is not defined.
outlook is not declared. It may be inaccessible due to its protection level.

Code is as follows:

Dim OutlookMessage As outlook.MailItem
Dim AppOutlook As New outlook.Application

Dim objNS As outlook._NameSpace = AppOutlook.Session
Dim objFolder As outlook.MAPIFolder
objFolder
= objNS.GetDefaultFolder(outlook.OlDefaultFolders.olFolderDrafts)

Try
OutlookMessage
= AppOutlook.CreateItem(outlook.OlItemType.olMailItem)
Dim Recipents As outlook.Recipients = OutlookMessage.Recipients
Recipents
.Add("myemail@hotmail.com")
OutlookMessage
.Subject = "Sending through Outlook"
OutlookMessage
.Body = "Testing outlook Mail"
OutlookMessage
.BodyFormat = outlook.OlBodyFormat.olFormatHTML
OutlookMessage
.Save()
OutlookMessage
.Move(objFolder)
Catch ex As Exception
MessageBox
.Show("Mail could not be sent")
Finally
OutlookMessage
= Nothing
AppOutlook
= Nothing
End Try
Not sure what still remains to be referenced here? I'm having the same problem using Word classes as well. The program I'm building depends on using outlook to send emails and creating Word documents therefore I'm really stuck until I can work out how to get them working.
 
Referencing the assembly is first step, that makes the library available to your application. Next step is using the types defined in it, these types are organized in namespaces. When you use a type you can either qualify it by its namespace, or you can import the namespace and use the type directly in code.

The Application type is defined in interop library in Microsoft.Office.Interop.Outlook namespace, fully qualified type code example:
Dim app As Microsoft.Office.Interop.Outlook.Application

To import the namespace add to top of code file:
Imports Microsoft.Office.Interop.Outlook

code example where namespace now is known:
Dim app As Application

With the Imports statement you can also define an alias to make it easier to filter the types in this library:
Imports Outlook = Microsoft.Office.Interop.Outlook

Now you can qualify the type with its namespace alias:
Dim app As Outlook.Application

You can import parts of namespace also, and qualify the rest, for example:
Imports Microsoft.Office.Interop

Using type by qualifying rest of namespace:
Dim app As Outlook.Application

It is also possible to import namespaces globally from project properties, References page. Browse and check the namespaces you want to import for all code files.
 
Back
Top