0

Microsoft outlines how to leverage the Microsoft.Office.Interop.Outlook namespace quite well. example

I currently have a window form app created in VB, with a series of text fields requiring input. Upon submission of form, I have a text file that is created locally, but I also want to chain that event with the creation of an email, in a template fashion, having the user values embedded in the body of said email.

In my initial testing, to simply just generate the email, I have imported the appropriate function based on MS's documentation.

Imports Outlook = Microsoft.Office.Interop.Outlook

However when I enter the code provided after my text file creation code, there are many errors highlighted. I was prompted to create a friendclass for "Office."

Build does not like the CType(Application.CreateItem(Outlook.OlItemType - I observe the error CreateItem is not a member of Application.

Is this due to the windows form I selected when building this project?

Dim mail As Outlook.MailItem = CType(Application.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem)

Any help would be appreciated..

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
E_xodus
  • 1
  • 3
  • why VBA tag? VBA is not VB.Net. – nagarajannd Jul 27 '18 at 12:33
  • That was an error on my part. I have removed the improper tags. – E_xodus Jul 27 '18 at 12:49
  • No, you haven't removed the incorrect tags. If you're using `Imports` this is *not* VBA! This is VB.NET. – Cindy Meister Jul 27 '18 at 12:54
  • Save yourself some pain and change this `Imports Outlook = Microsoft.Office.Interop.Outlook` to `Imports OL = Microsoft.Office.Interop.Outlook` to avoid the confusion over which Outlook you're referring to. – Andrew Mortimer Jul 27 '18 at 13:08
  • So I added Microsoft Outlook 16.0 Object Library as a reference to my project, which it appears I have missed...... This cleared many errors, but the "CreateItem is not a member of Application" error continues.. – E_xodus Jul 27 '18 at 13:47

1 Answers1

0

this appears to work - different approach but appears to work..

    Dim oApp As Outlook.Application
    oApp = New Outlook.Application

    Dim oMsg As Outlook.MailItem
    oMsg = oApp.CreateItem(Outlook.OlItemType.olMailItem)

    oMsg.Recipients.Add("test@test.com")

    oMsg.Subject = "test"
    oMsg.HTMLBody = "<HTML><BODY>test - test</BODY></HTML>"
    oMsg.Display()

now it is to figure out how to define a variable within the htmlbody section...

E_xodus
  • 1
  • 3
  • _"now it is to figure out how to define a variable within the htmlbody section..."_ - You mean regular string concatenation, like in [your last question](https://stackoverflow.com/questions/51539252/vb-dynamically-create-file-then-write-to-file/51539480#51539480)? – Visual Vincent Jul 27 '18 at 14:48
  • similar, for instance.. test - within the html brackets, i am not able to append a variable. I assume such as this function differs from my last post. – E_xodus Jul 27 '18 at 14:58
  • did the trick.. oMsg.HTMLBody = "test - test" & variable.text & "test" – E_xodus Jul 27 '18 at 15:25
  • String concatenation can be done wherever a string is expected. The contents of the strings doesn't matter, nor where in your code they are located, you can always concatenate them and it is always done in the same way. – Visual Vincent Jul 27 '18 at 16:33