VB: Generate E-Mail via outlook - vb.net

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

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

Related

What does CreateItem do in VBA for outlook emails?

Why do we use CreateItem to create a MailItem in VBA as opposed to the New keyword? More generally, what’s the difference between the two methods? I noticed that to create an instance of the app we do:
Set OutlookApp = New Outlook.Application
However for the MailItem we do:
Set OutlookEmail = OutlookApp.CreateItem(olMailItem)
Tried the above method and it worked but am wondering why it works like that.
They are entirely different lines of code. In the first case a new Outlook Application instance is created:
Set OutlookApp = New Outlook.Application
That is actually all what you could do with the New operator and Outlook object model. Everything else is retrieved from using methods and properties available in the Outlook object model. For example, there are several ways of creating a new MailItem instance:
Set OutlookEmail = OutlookApp.CreateItem(olMailItem)
And
Set OutlookEmail = folder.Items.Add(olMailItem)
The difference is where the item will be saved when calling Save().
You can read more about possible ways of creating Outlook items in the article which I wrote for the technical blog, see How to create and show a new Outlook mail item programmatically: C#, VB.NET.

Outlook VSTO Add-In using a Form and Button - Forward Email Won't Display and Can't get Button on Home Ribbon

I'm new to creating Add-In's for Outlook, but have managed to crash and bash my way through most of what I'm trying to do. I have been trying in C# as most examples and documentation use C#, however I'm not that comfortable writing in C#. I have used VBA quite a lot in the past so VB.Net is easier for me to use.
So far my Add-In does the following:
Adds a button when reading an email (OfficeID set to TabReadMessage).
When you click the button a form is displayed which asks for some information.
There's a button on the form to continue which validates the form and identifies the current MailItem.
Using the current MailItem, it appends some text and then attempts to Forward the email with the revised text.
Where I'm stuck is with the Forward() function. I can update the To and Subject, but I can't Display() it.
I think I'm ok up to here
' Create an application object which is needed to get to the ActiveInspector
Dim application As Outlook.Application '
application = Globals.ThisAddIn.Application
' Variables to inspector, needed to get the current item, current mail item, the forward email (so we can update the subject)
Dim mailItem As Microsoft.Office.Interop.Outlook.MailItem
Dim forwardMailItem As Microsoft.Office.Interop.Outlook.MailItem
Dim oInspector As Microsoft.Office.Interop.Outlook.Inspector
Dim currentSubject As String
Dim forwardSubject As String
oInspector = application.ActiveInspector
mailItem = oInspector.CurrentItem
' forward email gets created here
forwardMailItem = mailItem.Forward()
currentSubject = mailItem.Subject
forwardSubject = "some other stuff added - " & currentSubject
If I do a MsgBox at this point and reference forwardMailItem.Subject, the new MailItem must be there as it displays the revised subject.
Once I add the following line, it's like the MailItem object has gone - ie if I do MsgBox(forwardMailItem.Subject) nothing happens.
forwardMailItem.Display()
If I remove the above line and just put the following, the email gets sent.
forwardMailItem.Send()
Part 1 - I can't get the forwardMailItem to display and don't understand why?
Part 2 - I don't know how to get the new Ribbon I created to also show when on the Home Ribbon. I found how to change the OfficeID to show it on TabReadMessage instead of the default TabAddIn, but don't know what to search for to find how to add it to the Home Ribbon.

Extract email metadata with VBA script

I have a folder full of emails that are a custom message class (iXOS-Archive, related to OpenText Enterprise Archive). Each email has a custom metadata property, visible within Outlook, called "Document Identifier". I'm trying to extract this from the emails using a VBA script. I found a script that extracts common metadata (To, From, Subject etc.) from the emails and writes it to Excel. This works well.
http://spreadsheetpage.com/index.php/tip/getting_a_list_of_file_names_using_vba/
I've tried debugging the script and looking within the email properties, but I cannot find any collection that contains custom metadata.
Does anyone know how I can access the custom metadata through the VBA script?
You will probably not be able to do this using a FileSystemObject or DIR function (as given in the code you linked to, above).
I am unable to test without a suitable example, but this might work:
Bind Outlook to Excel
Open the MSG file in Outlook
Use the Outlook object model to review the MSG file's .ItemProperties
Practically speaking you will set this up in a loop, similar to your example code, but for the sake of testing, try it out on a single file and see if this will help you.
'Requires reference to Outlook object model
Sub foo()
Dim olApp As Outlook.Application
Dim msg As Outlook.MailItem
Dim properties As Outlook.ItemProperties
Dim p As Long
Set olApp = GetObject(, "Outlook.Application")
Set msg = olApp.CreateItemFromTemplate("C:\your filename.msg")
Set properties = msg.ItemProperties
For p = 0 To properties.Count - 1
Debug.Print properties(p).Name
Next
Set msg = Nothing
Set olApp = Nothing
End Sub
This should print the list of ItemProperties in the Immediate window, scroll through that list and check to see if the one you're looking for -- "Document Identifier" -- is included. If so, then this should work and you can modify as needed to do whatever it is you want to do with that information.
I cannot be of further assistance unless you can provide a test/sample version of this email format.
Cheers.

Outlook code is working when manually called but giving trouble from Application_ItemSend

I have a code that checks the recipient of the mail, looks what organization is set in the address book for the recipient and dependent on that sets the "SentOnBehalfOfName"-property of the item. If the recipient is working for client2, he will get the mail from "we_love_to_serve_client2#domain.com".
I call the code either before sending the mail via a button in my ribbon, that calls this Sub:
Sub Signatur()
Dim olApp As Outlook.Application
Dim objMail As Outlook.MailItem
Set olApp = Outlook.Application
Set objMail = Application.ActiveInspector.CurrentItem
Call Signatur_auto(objMail)
End Sub
I do this if I want to know which mail-adress is going to be chosen.
In the itemSend-section of thisOutlookSession I also call the same sub
Call Signatur_auto(Item)
Part of the Signatur_auto (i do not copy that in, the question is too long already...) is dealing with the SentOnBehalfOfName-property, the other part is putting the item into the right folder. The Folder is chosen depending on the SentOnBehalfOfName-property.
Now comes the interesting part: Although the folder-part is always working (which can only be when the SentOnBehalfOfName has worked before), the SentOnBehalfOfName only works "half". In the preview-line the mail sent is shown as from "we_serve_client2#domain.com", but when I open the mail it says it was sent by me. The Client always only sees my address, and also answers to my address - which I do not want....
How cant be, that the same code is having different results dependent on where it is called? Is it a Problem to change the sendonbehalf-field in the item send-section?
Thanks for any Inputs!
Max
Why it does not work?
Try this in ItemSend.
Dim copiedItem As mailItem
Set copiedItem = Item.Copy
copiedItem.SentOnBehalfOfName = "we_love_to_serve_client2#domain.com"
copiedItem.Send
Item.delete
Cancel = True ' In case your setup generates an error message as described in the comments
Why it works? Appears "copiedItem.Send" bypasses ItemSend.

VB.net 2005 Sending Emails With Outlook 2003

We currently use the following code to create an email in Outlook so that the user can type what they want in Outlook, then when the email is sent, the system prompts them to see if they would like to save the email.
Dim objOutlook As Object
Dim objMessage As Object
Dim objInspector As Object
If strEMail <> "" Then
objOutlook = CreateObject("Outlook.Application")
objMessage = objOutlook.CreateItem(0)
objMessage.To = strEMail
objInspector = objMessage.GetInspector
objInspector.Display()
While Not objInspector.CurrentItem Is Nothing
End While
frmSaveSentEmail.BringToFront()
frmSaveSentEmail.ShowDialog()
The code works fine on Outlook 2003 as long as they are not using Word as their email editor. However, with Word set up as the email editor, the while loop that tests to see if the email object is closed never ends.
Is there a way to handle this differently so that it will work even with Word as the editor?
I am not terribly experienced with programming Outlook via VB.NET, but that loop certainly looks suspicious. Perhaps you should try taking advantage of the inspector's Close event instead of repeatedly checking its CurrentItem property. If I am not mistaken, you should be able to present your dialog within the event handler.
Ended up changing the loop to:
While Not objOutlook.ActiveInspector Is Nothing
End While
This resolved the issue.