Logan
Logan

Reputation: 31

References in Microsoft Visual Studio not working

Currently, I am attempting to send an email using VB.NET. Now, I have added a reference with this code: (I have added placeholders)

Module Module1

    Sub Main()
        ' Create an Outlook application.
        Dim oApp As Outlook._Application
        oApp = New Outlook.Application()

        ' Create a new MailItem.
        Dim oMsg As Outlook._MailItem
        oMsg = oApp.CreateItem(Outlook.OlItemType.olMailItem)
        oMsg.Subject = "Send Attachment Using OOM in Visual Basic .NET"
        oMsg.Body = "Hello World" & vbCr & vbCr

        ' TODO: Replace with a valid e-mail address.
        oMsg.To = "[email protected]"

        ' Add an attachment
        ' TODO: Replace with a valid attachment path.
        Dim sSource As String = "C:\Temp\Hello.txt"
        ' TODO: Replace with attachment name
        Dim sDisplayName As String = "Hello.txt"

        Dim sBodyLen As String = oMsg.Body.Length
        Dim oAttachs As Outlook.Attachments = oMsg.Attachments
        Dim oAttach As Outlook.Attachment
        oAttach = oAttachs.Add(sSource, , sBodyLen + 1, sDisplayName)

        ' Send
        oMsg.Send()

        ' Clean up
        oApp = Nothing
        oMsg = Nothing
        oAttach = Nothing
        oAttachs = Nothing
    End Sub

End Module

How can I get the references to work, for all of the Outlook items (Outlook.Application, Outlook._MailItem, Outlook, Outlook.Attachments, Outlook.Attachment) are either undeclared or undefined.

Thanks in advance.

Upvotes: 0

Views: 1724

Answers (3)

dbasnett
dbasnett

Reputation: 11773

Imports Microsoft.Office.Interop

'On the Project menu, click Add Reference.
'On the COM tab, Double click ->  Microsoft Outlook xx.0 Object Library

Module Module1

    Sub Main()
        ' Create an Outlook application.
        Dim oApp As Outlook._Application
        oApp = New Outlook.Application()

        ' Create a new MailItem.
        Dim oMsg As Outlook._MailItem
        oMsg = CType(oApp.CreateItem(Outlook.OlItemType.olMailItem), Outlook._MailItem)
        oMsg.Subject = "Send Attachment Using OOM in Visual Basic .NET"
        oMsg.Body = "Hello World" & vbCr & vbCr

        ' TODO: Replace with a valid e-mail address.
        oMsg.To = "[email protected]"

        ' Add an attachment
        ' TODO: Replace with a valid attachment path.
        Dim sSource As String = "C:\Temp\Hello.txt"
        ' TODO: Replace with attachment name
        Dim sDisplayName As String = "Hello.txt"

        Dim sBodyLen As Integer = oMsg.Body.Length
        Dim oAttachs As Outlook.Attachments = oMsg.Attachments
        Dim oAttach As Outlook.Attachment
        oAttach = oAttachs.Add(sSource, , sBodyLen + 1, sDisplayName)

        ' Send
        oMsg.Send()

        ' Clean up
        oApp = Nothing
        oMsg = Nothing
        oAttach = Nothing
        oAttachs = Nothing
    End Sub

End Module

Upvotes: -1

Kon
Kon

Reputation: 27431

Add a reference to the "Microsoft Outlook 11.0 Object Library":

  1. On the Project menu, click Add Reference.
  2. On the COM tab, click Microsoft Outlook 11.0 Object Library, and then click Select.
  3. Click OK in the Add References dialog box to accept your selections. If you are prompted to generate wrappers for the library that you selected, click Yes.

And in code you'll have to add this:

Imports Outlook = Microsoft.Office.Interop.Outlook

More info found here: Handy Tasks Using Microsoft Office Outlook 2003 and Visual Basic .NET

But if you're in .NET, why not use System.Net.Mail?

Upvotes: 1

NoAlias
NoAlias

Reputation: 9193

In Solution Explorer right click on your project and select "Add Reference" and scroll down until you see Microsoft.Office.Interop.Outlook and select that one. Then add "Imports Microsoft.Office.Interop" at the top of your VB file.

Upvotes: 1

Related Questions