Mail format getting changed in recepient's mailbox in outlook - vbscript

I have written some code in vbscript to automatically send mails to a recepient using outlook,Everything is working fine but i am having just one issue, The format what i have selected for the message body getting changed in the recepient's mail box.I used font color- blue and font face -calibri but it got changed into Times new roman without any color.
Any solution?
Set MyApp = CreateObject("Outlook.Application")
Set MyItem = MyApp.CreateItem(0) 'olMailItem
With MyItem
.To = "abc#com"
.Subject = ""
.ReadReceiptRequested = False
.HTMLBody = "<font size='3' face='Calibri' color='#151B54'>Hi,Whatever written here got changed into plain text.<font>"
.Attachments.Add "C:\Excels\"& objFso.GetFileName(objFile.path)
End With
MyItem.Display
End if
Or is there any setting that i have to change in the outlook for the sent mails?

This works perfectly fine with me:
MailItem sendMail = Globals.ThisAddIn.Application.CreateItem(OlItemType.olMailItem);
sendMail.Subject = "test";
sendMail.To = SenderName; //or email
sendMail.HTMLBody = "<font size='50' face='Verdana' color='#ff0000'>Hi,Whatever written here got changed into plain text.<font>";
sendMail.Send();
When I send this to myself I recieve a big red text in verdana
Note that this is c# so I'm not sure if it will help you
EDIT:
One more thing: font size='3' won't work with me either. Because it's too small to display decently

Related

Can i add custom icon to "outlook" item?

I am trying to add a custom icon to outlook items (inbox items list) using officejs addon for outlook.
If this is not possible with officejs then how can I achieve this using Exchange service or with any other tool or library?
You can change the icon, however, you have a choice of icons. You would need to use C# or VB.NET (eg VSTO Outlook-addin) or VBA.
I could not find a list of icon values you can use but here is an image of an old list - in case the link gets lost.
Source of the image and also partly answering your question.
To change the icon you need to use the MailItem.PropertyAccessor
Couple of Examples Const (these are hex values but you can use a Long as well)
Const OL_PHONE = &H15D
Const OL_GROUP = &H202
Const OL_NOTE = &H1BD
Const PR_ICON_INDEX As String = "http://schemas.microsoft.com/mapi/proptag/0x10800003"
Using the below helper methods
'use the Get to see the value of an icon
'prior to this code you would need to get a reference to an Outlook mailitem
Dim res As New Object
GetExtendedPropertyValue(oMailItem, PR_ICON_INDEX, res)
'check the value of res or call Hex(res) to see its hex value
'here you can set the icon eg OL_GROUP etc
SetExtendedPropertyValue(oMailItem, PR_ICON_INDEX, OL_PHONE)
Couple of helper methods I have made
Private Function GetExtendedPropertyValue(ByVal aMailItem As Outlook.MailItem, ByVal aProperty As String, ByRef res As Object) As Boolean
Dim oPropAcc As Outlook.PropertyAccessor = Nothing
Try
oPropAcc = DirectCast(aMailItem.PropertyAccessor, Outlook.PropertyAccessor)
res = oPropAcc.GetProperty(aProperty)
Return True
Catch ex As System.Exception
'Put your own logging here
Finally
If Not oPropAcc Is Nothing Then
Marshal.ReleaseComObject(oPropAcc)
oPropAcc = Nothing
End If
End Try
Return False
End Function
Private Function SetExtendedPropertyValue(ByVal aMailItem As Outlook.MailItem, ByVal aProperty As String, ByVal value As Integer) As Boolean
Dim oPropAcc As Outlook.PropertyAccessor = Nothing
Try
oPropAcc = DirectCast(aMailItem.PropertyAccessor, Outlook.PropertyAccessor)
oPropAcc.SetProperty(aProperty, value)
Return True
Catch ex As System.Exception
'Put your own logging here
Finally
If Not oPropAcc Is Nothing Then
Marshal.ReleaseComObject(oPropAcc)
oPropAcc = Nothing
End If
End Try
Return False
End Function
These three examples look like this
The PR_ICON_INDEX PidTagIconIndex Canonical Property can be found here and note that they do say
This property, if it exists, is a hint to the client. The client may ignore the value of this property.
However this does not seem to be the case.
Of course the icon change will not be permanent. If the user forwards the emails or replies then it would be changed.
EDIT
By the way here is a nice way to find out the possible icons. Make a temp folder and copy a rubbish email into this folder 2048 times. Then run this code
Public Sub PrIconIndex()
USE THIS MACRO WITH CARE!
Dim objItems As Items
Dim mail As MailItem
Dim i As Integer
Set objItems = Application.ActiveExplorer.CurrentFolder.Items
For i = 1 To 2048
Set mail = objItems(i)
Call mail.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x10800003", i)
mail.Body = CStr(i)
mail.Save
Next
End Sub
The above come from this forum link
Currently the feature to add a custom icon to outlook items is not there. We track Outlook add-in feature requests on our user-voice page. Please add your request there. Feature requests on user-voice are considered, when we go through our planning process.

send image behind text word using VBS

i'm using VBS to create a title screen and i have a problem trying to send a image behind te text.
Here is my code:
function page()
Set oWord = CreateObject("Word.Application")
oWord.Visible = True
Set objDoc = oWord.Documents.Open("C:\xxx.docx")
objDoc.Sections.PageSetup.DifferentFirstPageHeaderFooter = true
Set head = objDoc.Shapes.AddPicture("C:\img.png")
head.PictureFormat.Brightness = 0.7
head.ZOrder msoSendBehindText 'I try to use msoSendToBack, SendBack, SendBehindText, Back and others and not work
end function
i just want send "head" to the back , the actual result is the image is not in the back of text, and the version of word is 2013.
If someone, know how to solve this, thanks in advance.
The solution was, the utilization of numeration
head.ZOrder 5
https://bettersolutions.com/vba/enumerations/msozordercmd.htm
with this change, i solve my problem.

Outlook add-in mailitem.display not working Visual Basic

I'm in the process of converting some outlook VBA macros to an Add-in. I am having difficulty with my macros that create emails based on a template. I decided to code a simple button to create and display a new email with the subject test.
Everything is working up to displaying the email which doesn't happen.
Private Sub ButtonGenEmail_Click(sender As Object, e As EventArgs) Handles ButtonGenEmail.Click
Me.Close()
Dim objApp As Outlook.Application
Dim objMail As Outlook.MailItem
objApp = Globals.ThisAddIn.Application
objMail = objApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem)
objMail.Subject = "test"
objMail.Save()
objMail.Display(False)
End Sub
At one point I added msgbox "Done" after objMail.Display(False) and the message never appears.
Thanks in advance
This should obviously work. I'm not so skilled in Outlook but I guess the problem here is that you run it from a form (I see the Me.Close)
Isn't then Outlook blocking to display the mail because the form is still open (I guess modal = ShowDialog())?
I think you will have to either change the form to be not modal (use Show() instead of ShowDialog()) or handle the event after the form is closed, something like
All code below written from top of my mind, so I may miss something
A method from where you initialize the form
Dim frm as new YourForm()
frm.ShowDialog()
if frm.MyState = TheyClickOnButton Then
' Run your mailItem code here
End If
in the form code
Public Enum State
Unknown = 0
TheyClickedOnButton
End Enum
Public Property MyState as State
Private Sub ButtonGenEmail_Click(sender As Object, e As EventArgs) Handles ButtonGenEmail.Click
MyState = TheyClickedOnButton
Me.Close()
End Sub

vba email embed image not showing

I have an odd experience here. I have had loads of problems embedding a logo or image to emails using the src=cid... found out it wont show if not setting size for example.
I have an Access application to send from, but have broken it down to the code below using Excel.
BUT
It now works fine, if I display the email and then send it. Not doing anything else at all. Just display then send.
If I send directly from vba, the image will not display properly. The attachment symbol shows and Outlook itself will put the image inline, but say, gmail, wont. It's not gmails fault, as the attachmet symbol shows in Outlook. It doesn't if I display and then send.
I suspect it's still something with sizing or placing. Without the width part, Outlook will still show the image at correct place, but still show as attachment. So when you display and press send then there must me another attribute set or something. I cannot find out what!
Hope someone can help or has an idea! I'm not the strongest in HTLM, so it's probably something simple...
Thanks
John
Sub test()
Dim oApp As Outlook.Application
Dim oEmail As MailItem
Dim colAttach As Outlook.Attachments
Dim oAttach As Outlook.Attachment
'create new Outlook MailItem
Set oApp = CreateObject("Outlook.Application")
Set oEmail = oApp.CreateItem(olMailItem)
'add graphic as attachment to Outlook message
'change path to graphic as needed
Set colAttach = oEmail.Attachments
Set oAttach = colAttach.Add("C:\temp\logo.jpg")
oEmail.Close olSave
'change the src property to 'cid:your picture filename'
'it will be changed to the correct cid when its sent.
oEmail.HTMLBody = "<BODY><IMG src=""cid:logo.jpg"" width=200> </BODY>"
oEmail.Save
oEmail.To = "someemailtogoinhere#gmail.com"
oEmail.Subject = "test"
oEmail.Display
'oEmail.Send
Set oEmail = Nothing
Set colAttach = Nothing
Set oAttach = Nothing
Set oApp = Nothing
End Sub
Just to post the simple form of code that works. Big thanks to #Eugene Astafiev.
Sub test()
Dim oApp As Outlook.Application
Dim oEmail As MailItem
Dim colAttach As Outlook.Attachments
Dim oAttach As Outlook.Attachment
Dim olkPA As Outlook.PropertyAccessor
Const PR_ATTACH_CONTENT_ID = "http://schemas.microsoft.com/mapi/proptag/0x3712001F"
'create new Outlook MailItem
Set oApp = CreateObject("Outlook.Application")
Set oEmail = oApp.CreateItem(olMailItem)
'add graphic as attachment to Outlook message
'change path to graphic as needed
Set colAttach = oEmail.Attachments
Set oAttach = colAttach.Add("C:\temp\logo.jpg")
Set olkPA = oAttach.PropertyAccessor
olkPA.SetProperty PR_ATTACH_CONTENT_ID, "logo.jpg"
oEmail.Close olSave
'change the src property to 'cid:your picture filename'
'it will be changed to the correct cid when its sent.
oEmail.HTMLBody = "<BODY><IMG src=""cid:logo.jpg""> </BODY>"
oEmail.Save
oEmail.To = "someemail#gmail.com"
oEmail.Subject = "test"
oEmail.Send
Set oEmail = Nothing
Set colAttach = Nothing
Set oAttach = Nothing
Set oApp = Nothing
End Sub
You need to set PR_ATTACH_CONTENT_ID property (DASL - http://schemas.microsoft.com/mapi/proptag/0x3712001F) on the attachment using Attachment.PropertyAccessor. Be aware, the PropertyAccessor property of the Attachment class was added in Outlook 2007.
You may find the How do I embed image in Outlook Message in VBA? link helpful.

Unbound imagecontrol ONLY show First image when imagelink is added in a 1:N relation

Background:
Entry is via a subform for adding/showing/linking images.
I do not want to store the image files within my DB, the image folder is separate. The DB will grow rather large in time.
I have created a click-control enabling a popup for user to browse and click on the imagePATH to be added in a Bound Textfield (called Bildadress, no not misspelled in My country, Grin ) in the subform.
See code below.
Then I add a new unbound Image-control and specify its Controlsource = the Textfield mentioned above.
For the firs image this works wonderful, but for the following the Image-control returns NULL (not show att all). The data in the Textfield updates as it should.
Will the 2nd stage only work in a 1:1 relationship OR can I (with your help) use VBA code to make this work?
OPTIMAL would be to get this to work and also a 2nd Bound Textfield just displaying the actual image file name. .
I hope someone out there have encountered this problem who also didnt want to use Attachment to store the files within the databae.
CODE:
Private Sub AddFilePath_Click()
Call Selectfile
End Sub
Public Function Selectfile() As String
Dim Fd As FileDialog
Set Fd = Application.FileDialog(msoFileDialogOpen)
With Fd
.AllowMultiSelect = False
.Title = "Välj önskad fil"
If .Show = True Then
Selectfile = .SelectedItems(1)
Me.Bildadress = Selectfile
Else
Exit Function
End If
End With
Set Fd = Nothing
End Function
If you use a bound textbox that holds the image-path then you can use
Me.Imagecontrol.Picture = Me.BoundTextControl.Value
to load the picture into an unbound image control. In your case that would be something like
If .Show = True Then
Me.Bildadress.value = .SelectedItems(1)
Me.Bild.Picture = Me.Bildadress.value
Else
It would be best to also load the respective picture in the OnCurrent Event.
Private Sub Form_Current()
Me.Bild.Picture = Me.Bildadress.value
End Sub
However, keep in mind that access is a one-file-database and you break that paradigm when using links to external files where the files would belong into the DB.

Resources