I developed one vb.net form that have all mail of my Outlook inbox with "Next" and "Prev" button.I am able to fetch all details like "SenderName","SenderEmail","Body" etc.Now problem i am facing is that the body of mail not coming in HTML format.I need body in HTML format to show on vb.net form.
here is my current code.
myItems.Item(currentindexparam).BodyFormat = Outlook.OlBodyFormat.olFormatHTML
lblsub.Text = myItems.Item(currentindexparam).Subject
lblform.Text = myItems.Item(currentindexparam).SenderEmailAddress
txt_body.Text = myItems.Item(currentindexparam).Body
lbldate.Text = myItems.Item(currentindexparam).ReceivedTime
_currentindex = currentindexparam
Dim Atmt As Outlook.Attachment
For Each Atmt In myItems.Item(currentindexparam).Attachments
Dim filename As String = "C:\Email Attachments\" + Atmt.FileName
Atmt.SaveAsFile(filename)
Next Atmt
let me know if you have any idea about it.
Thanks
Use MailItem.HTMLBody Property (Outlook).
Related
I want to encrypt whenever an attachment with PDF extension is added to mail with Outlook vba.
Is there a way to write such a macro?
Thanky you?
You can handle the MailItem.AttachmentAdd event which is fired when an attachment has been added to an Outlook item. So, you could check the attached file - the Attachment that was added to the item is passed as a parameter and encrypt it if required. For example:
Public WithEvents newItem As Outlook.MailItem
Private Sub newItem_AttachmentAdd(ByVal newAttachment As Attachment)
If newAttachment.Type = olByValue Then
newItem.Save
If newItem.Size > 500000 Then
MsgBox "Warning: Item size is now " & newItem.Size & " bytes."
End If
End If
End Sub
Public Sub TestAttachAdd()
Dim atts As Outlook.Attachments
Dim newAttachment As Outlook.Attachment
Set newItem = Application.CreateItem(olMailItem)
newItem.Subject = "Test attachment"
Set atts = newItem.Attachments
Set newAttachment = atts.Add("C:\Test.txt", olByValue)
End Sub
Note, you can find the cached file on disk. You can find the actual path in the windows registry:
I would like to reply to a message inline with a outlook template using a macro.
I am currently using the below code to perform the reply with a template but this opens a pop out reply window.
Sub Reply_Scripting()
Dim origEmail As MailItem
Dim replyEmail As MailItem
Set origEmail = Application.ActiveWindow.Selection.Item(1)
Set replyEmail = Application.CreateItemFromTemplate("C:\Test.oft")
replyEmail.HTMLBody = replyEmail.HTMLBody & origEmail.reply.HTMLBody
replyEmail.Display
End Sub
I have searched and found there has been similar question answered here. However, I was not able to modify the code to make it work successfully in my case.
Thanks.
I have noticed the following line of code:
replyEmail.HTMLBody = replyEmail.HTMLBody & origEmail.reply.HTMLBody
Note, you need to get a well formed HTML markup and assign it to the HTMLBody property. But it looks like you try to merge two HTML pages into a single one by adding one to another.
Instead, you need to paste the body content of the template you are loading in the code at the beginning of the body section of the existing item. I.e. right after the <body> tag.
This is what I have for an "auto reply". This DOES NOT allow editing before sending, but easily modified to do so. See comments in code.
Sub ReplyMSG()
Dim olItem As Outlook.MailItem
Dim olReply As MailItem ' Reply
For Each olItem In Application.ActiveExplorer.Selection
olItem.UnRead = False '<<----This marks the email as Read
Set olReply = olItem.ReplyAll '<<----This replies to all recipients
olReply.HTMLBody = "Insert a message or template here" & olReply.HTMLBody
olReply.Display '<<-----Use this to display the email before sending
olReply.Send '<<-----Comment this out if you want to edit before sending
Next olItem
End Sub
Once i have opened an appointment in outlook I am then wanting to insert a jpg into the body of the invite by using a vba script, these will be phone details in the form of a jpg.
Const MyPath = "C:\diallist\"
Const MyPicture = "TestDialList.jpg"
Dim myItem As Object
Set myItem = Application.ActiveInspector.CurrentItem()
myItem.MeetingStatus = olMeeting
.Attachments.Add MyPath & MyPicture
.HTMLBody = "<html><p>This is a picture</p>" & "<img src=cid:" & _ Replace(MyPicture, " ", "%20") & " height=240 width=180>"
.Display
End With
Any help is gratefully received.
Firstly, the ApppointmentItem object does not expose the HTMLBody property, only MailItem does. For the mail item, you need to add the image as an attachment and set its PR_ATTACH_CONTENT_ID property using Attachment.PropertyAccessor.SetProperty to the cid used by the img tag in th HTML body. Again, that will bot work for appointments sicne they only support RTF.
To add a picture to an item currently being displayed, use Application.ActiveInspector.WordEditor.Shapes.AddPicture.
I have some VBA that checks the subject of every message as soon as it hits my inbox, and submits certain emails' contents over http to a server for processing.
This works great for messages with no attachments, but fails if there is an attachment on the email. I am using a http GET to submit the text.
What is the effect of the presence of an attachment on the body property of the message, and how can I ignore the attachment and submit only the email body text?
The VBA (trimmed for clarity but complete and functional):
Declarations:
Option Explicit
Private WithEvents olInboxItems As Items
On startup:
Private Sub Application_Startup()
Set olInboxItems = Session.GetDefaultFolder(olFolderInbox).Items
End Sub
On item added to inbox:
Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
On Error Resume Next
Dim olMailItem As MailItem
Dim strAttachmentName As String
Dim submitResult As String
If TypeOf Item Is MailItem Then
Set olMailItem = Item
If ((InStr(olMailItem.subject, "test subject") > 0)
Dim subject As String
subject = olMailItem.subject
Dim contents As Variant
contents = olMailItem.body
Dim submitURL As String
submitURL = "http:// ... " & subject & "..." & contents & "..."
XMLHttpSynchronous submitURL
End If
End If
Set Item = Nothing
Set olMailItem = Nothing
End Sub
Http submit:
Sub XMLHttpSynchronous(ByVal URL As String)
Dim XMLHttpRequest As XMLHTTP
Set XMLHttpRequest = New MSXML2.XMLHTTP
XMLHttpRequest.Open "GET", URL, False
XMLHttpRequest.Send
End Sub
edit: I am now stripping attachments with the below code (tested and working) but the url still isn't submitting correctly.
Set myattachments = olMailItem.Attachments
While myattachments.Count > 0
myattachments.Remove 1
Wend
edit 2: I set contents=subject and the url submitted correctly, still no luck with the email's body, even after the message has been stripped of attachments
In order to get this working I stripped the attachments with the code in my first edit, than split the string of the body's text around the first and last character. I'm not sure why this works
I have a created folder in my outlook named "Reports". This folder contains emails with one attachment in each email. I would like to use ACCESS VBA to save the attachments from the "Reports" folder in Outlook to a local drive in my computer. here is the code I have so far, but gives me errors. Please help:
Sub GetAttachments()
Dim ns As NameSpace
Dim Inbox As Outlook.MAPIFolder
Dim folder As Outlook.MAPIFolder
Dim Item As Object
Dim Atmt As Attachment
Dim FileName As String
Dim i As Integer
Set ns = GetNamespace("MAPI")
Set Inbox = ns.Folders.Item("Reports") // I get an error in this line says an object could not be found
i = 0
If Inbox.Items.Count = 0 Then
MsgBox "There are no messages in the Inbox.", vbInformation, _
"Nothing Found"
Exit Sub
End If
For Each Item In Inbox.Items
For Each Atmt In Item.Attachments
FileName = "C:\Automation\" & Atmt.FileName
Atmt.SaveAsFile FileName // here is another error says method is not found
i = i + 1
Next Atmt
Next Item
Is your Reports folder within your Inbox folder? You may need to do something like this:
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
Set RptFolder = Inbox.Folders("Reports")
Your syntax for saving attachments looks correct (apart from your comments not being correct for VBA). You could print out the Filename that you are creating to see if it's a valid name. And I assume that you have created the Automation folder that you mention.
Update:
Try declaring your Atmt as an Outlook.Attachment. There is such a thing as an Access.Attachment which does not have a SaveAsFile method, and it's probably picking that one up first. If you include the library name, you should get the one you need.
Update 2:
To get to your Reports folder, one way is to get the Inbox folder as you are currently doing, then get its parent, then get the Reports folder under that.
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
Set Mailbox = Inbox.Parent
Set RptFolder = Mailbox.Folders("Reports")
Another way would be to scan the items under "ns" to find the one that starts with "Mailbox", then get the Reports folder under that. It seems a little more cumbersome than getting the parent of the inbox. That also seems cumbersome, but I couldn't find a way to get to the Mailbox folder directly.
Replace
For Each Item In Inbox.Items
For Each Atmt In Item.Attachments
FileName = "C:\Automation\" & Atmt.FileName
Atmt.SaveAsFile FileName // here is another error says method is not found
i = i + 1
Next Atmt
With.....
For Each Item In Inbox.Items
For Each Atmt In Item.Attachments
FileName = "C:\Automation\" & Atmt.FileName
Attachments.SaveAsFile FileName // here is another error says method is not found
i = i + 1
Next Atmt
Outlook does not have a problem with atmt in the reference however, MS Access does. This should fix your problem.
Davis Rogers
Replace
Dim Atmt As Attachment
with
Dim Atmt As Outlook.Attachment
It'll make Access find the correct Class for atmt.