I have Windows Forms application which stores lots of documents and other attachments — i.e. a document management system. One of its features is a facility to email a bunch of attachments to a recipient. It does this by simply automating Outlook to compose a new email and we attach all the files to that message, and the user sends the email as normal through Outlook 2010.
Something weird happens. Some of the attachments in the system are actually .msg files (i.e. other emails that have been stored in the document management system). If the email we create contains .msg attachments, when the email arrives at the destination, all attachments are stripped. The really odd thing is that this affects some recipients but not all. But crucially, it only happens when .msg files are in the email's attachments. Provided the email doesn't contain .msg files, it works fine every time.
Any ideas?
here is the code:
Public Shared Sub CreateEmail(ByVal emailAddressTo As String, ByVal subject As String, ByVal bodyText As String, ByVal docs As DespatchDocumentFileList)
' create the email
Dim olApp As Object = CreateObject("Outlook.Application") ' New Outlook.Application
Dim olMail As Object = olApp.CreateItem(0) ' Outlook.OlItemType.olMailItem
' init the email
With olMail
.BodyFormat = 2 ' Outlook.OlBodyFormat.olFormatHTML
.Subject = subject
.To = emailAddressTo
.Body = bodyText
For Each doc As DespatchDocumentFileInfo In docs
Dim DespatchFile As InternalClaimsLibrary.Despatch.DespatchDocumentFile = InternalClaimsLibrary.Despatch.DespatchDocumentFile.GetDespatchDocumentFile(doc.ID)
Dim FileName As String
FileName = InternalClaimsLibrary.FileUtils.FileHelper.CreateFileFromByteArray(DespatchFile.DespatchedImageData, IO.Path.GetFileName(DespatchFile.DespatchedFileName))
.Attachments.Add(FileName)
Next
End With
olMail.Display()
End Sub
Related
I am trying to run a VBScript that searches all the Incoming messages for a specific string on the subject field and replaces it with something else but keeping the rest of the subject content. So far, this is my code but im not getting any results.
Incoming mails subject: [EXTERNAL] abcdfed ghijk lmno
What i need: [*] abcdfed ghijk lmno
Sub RunAScriptRuleRoutine(MyMail As MailItem)
Dim strID As String
Dim olNS As Outlook.NameSpace
Dim msg As Outlook.MailItem
Dim rply As Outlook.MailItem
strID = MyMail.EntryID
Set olNS = Application.GetNamespace("MAPI")
Set msg = olNS.GetItemFromID(strID)
' do stuff with msg, e.g.
msg.Subject = Replace(msg.Subject, "[EXTERNAL]", "[*]")
msg.Save
Set msg = Nothing
Set olNS = Nothing
End Sub
I will appreciate your help
Changes to the subject for received messages will only be reflected in the header UI. You also have to change the MailItem.ConversationTopic value, but it is read-only. However, you can use PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x0070001F", "New subject") to update it.
Using Outlook Add-In (VSTO) can I open an EML file from disk and "display" it?
I've tried this...
Dim filename As String = "c:\test\_test.eml"
Dim mail As Outlook.MailItem = CType(Globals.ThisAddIn.Application.Session.OpenSharedItem(filename), Outlook.MailItem)
mail.Display()
But Outlook throws an exception saying path is not valid even though it is.
Any idea?
OpenSharedItem does not work with EML files.
Outlook Object Model would not let you access EML files. You can either
Parse the EML file (or use an available component), create new item in Outlook and set all properties one at a time
If you were using C++ or Delphi, you could have used IConverterSession MAPI interface to import the data
If using Redemption is an option, you can use something like the following (off the top of my head):
dim Session as Redemption.RDOSession = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = Me.Application.Session.MAPIOBJECT
dim Drafts as Redemption.RDOFolder = Session.GetDefaultFolder(olFolderDrafts)
dim mail as Redemption.RDOMAil = Drafts.Items.Add
mail.Sent = true
mail.Import "c:\temp\test,eml", 1024 'olRfc922
mail.Save
'now reopen in OOM
dim oMail as Outlook.MailItem = Me.Application.Session.GetItemFromID(mail.EntryId)
dim forwardedMail as Outlook.MailItem = oMail.Forward
forwardedMail.Display()
I want to use a macro in outlook 2013. This macro is supposed to mark any emails arriving a specific folder ('work' folder) as read. I'm not familiar with vb. Any help/guidance is much appreciated!
No sure, I have heard this one before of wanting emails automatically read. You have two options:
a) Use Ctrl-A (select all mail in folder), Ctrl-Q (mark selection as read)
b) Use New Email Event something like:
Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
vID = Split(EntryIDCollection, ",")
Dim i as Long, objMail as Outlook.MailItem
For i = 0 To UBound(vID)
Set objMail = Application.Session.GetItemFromID(vID(i))
objMail.Unread = False
Next i
End Sub
Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
' version to select folder
Dim i As Long, objMail As Outlook.MailItem, mpfInbox As Outlook.Folder
Set mpfInbox = Application.GetNamespace("MAPI").Folders("YOURACCOUNT").Folders("[Gmail]").Folders("Sent Mail")
For i = 1 To mpfInbox.Items.Count
If mpfInbox.Items(i).Class = olMail Then
Set objMail = mpfInbox.Items.Item(i)
objMail.UnRead = False
End If
Next i
End Sub
You can set up a rule which can trigger your macro.
I'd not suggest working with the NewMailEx event because it is not fired in some case and may introduce issues. See Outlook NewMail event unleashed: the challenge (NewMail, NewMailEx, ItemAdd) for more information.
I have rules set up to copy emails containing certain keywords to specific folders and mark as read.
The problem i'm having is when it copies those emails to the folders it marks the original email in the inbox as read, and which can cause me to miss the message.
If i don't mark it as read then when i read it in the Inbox it stays unread in the specific folder.
I cant find any rule properties to accomplish this, anyone have any ideas?
Set the rules to copy to the target folders but not mark as read.
Put this untested code in the ThisOutlookSession module. Assumes the target folders are directly under the Inbox. If buried deeper, add .Folders as necessary.
Option Explicit
' one line for each target folder
Private WithEvents myOlItemsA As Outlook.Items
Private WithEvents myOlItemsB As Outlook.Items
Private Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
' one line for each target folder
Set myOlItemsA = objNS.GetDefaultFolder(olFolderInbox).Folders("targetfoldernameA").Items
Set myOlItemsB = objNS.GetDefaultFolder(olFolderInbox).Folders("targetfoldernameB").Items
End Sub
' one copy of ItemAdd code for each target folder
Private Sub myOlItemsA_ItemAdd(ByVal item As Object)
On Error GoTo ErrorHandler
Dim Msg As Outlook.MailItem
If TypeName(item) = "MailItem" Then
Set Msg = item
Msg.Unread = False
End If
ProgramExit:
Set Msg = Nothing
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description
Resume ProgramExit
End Sub
Private Sub myOlItemsB_ItemAdd(ByVal item As Object)
' same code as for myOlItemsA
End Sub
Code based on this post Using VBA to read new Outlook Email?
The rules move mail to target folders. The ItemAdd code acts on the items added to the target folders.
I was wondering if it was possible for when you enter a recipient's address for Outlook 2010 to automatically detect this address and change the signature accordingly? Just a general question.
I've had the same question and so far have not found the answer. As a nice workaround, I've successfully used the solution provided here: https://superuser.com/a/228633/74819. In the end you get a button on the toolbar allowing you to create a new message with a custom To address and a pre-defined body text (including signature) of your choice.
Now I actually find this method nicer than what I was looking for because it is more predictable. If the signature (and thus the message body) was changing based on the list of recipients, you would loose control over your text. Also, with a tool of your own, you can set more than just a signature.
Are you looking for a setting to do this or are you willing to work with a macro? If you're open to working with macros, see below and reply back with questions.
Public WithEvents goInspectors As Outlook.Inspectors
Public WithEvents myMailItem As Outlook.MailItem
Private Sub Application_Startup()
Initialize_Inspector
End Sub
Private Sub Initialize_Inspector()
Set goInspectors = Outlook.Application.Inspectors
End Sub
Private Sub goInspectors_NewInspector(ByVal Inspector As Inspector)
If Inspector.currentItem.Class = olMail Then
Set myMailItem = Inspector.currentItem
End If
End Sub
Private Sub myMailItem_PropertyChange(ByVal Name As String)
'The variable below should be modified for your situation.
'If you are in an Exchange environment, then you can use "last name, firstname"(caps-sensitive).
'If the the recipient is not in Outlook's address list, use "person#email.com"
customSignatureFor = "Lastname, Firstname"
'Use vbCrLf to account for enter/returns
oldSignature = "Respectfully," & vbCrLf & vbCrLf & "Phillip"
newSignature = "v/r," & vbcrlf & "Phil"
If Name = "To" Then
For i = 1 To myMailItem.Recipients.count
If InStr(myMailItem.Recipients(i), customSignatureFor) > 0 Then
tempstring = Replace(myMailItem.Body, oldSignature, newSignature)
myMailItem.Body = tempstring
End If
Next
End If
End Sub