References in Microsoft Visual Studio not working

℡╲_俬逩灬. 提交于 2019-12-12 02:08:10

问题


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 = "user@example.com"

        ' 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.


回答1:


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.




回答2:


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?




回答3:


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 = "user@example.com"

        ' 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


来源:https://stackoverflow.com/questions/6603287/references-in-microsoft-visual-studio-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!