简体   繁体   中英

VBA: Change Outlook “From” and font size

I'm trying to send an Outlook email from VBA Excel. I've got everything declared correctly, as far as I can tell. I'm having issues with changing the sender and the font size.

The from is a secondary email, also on Outlook, which I have access to. The font issue is that I can't seem to achieve font size 11 when using the below code.


Sender:

With OutMail
        .Display
        .Sender = "someone@example.com"
        '.SentOnBehalfOfName = "someoneelse@example.com"
        .To = origintext
        .Subject = "Location Verification"

        .BodyFormat = 2 'olFormatHTML
        .HTMLBody = fMsg & fMsg2 & fMsg3 & signature
        '.Body = signature

        .Display
End With

Where fMsg , fMsg2 , and fMsg3 are strings. The signature is declared earlier in the code with:

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
OutMail.Display
signature = OutMail.HTMLBody

I do this to get the signature as it seems to be impossible to use something such as OutMail.Signature = *Signature1* .

I understand there is an OutMail.SentOnBehalfOf = hello@world.com but that does not seem to work with OutMail.BodyFormat = 2 which sets the body to HTML format.


Font:

An example of my HTML body is as follows:

fMsg = "<p><font face = ""Calibri(Body)"" font size=""3"" color=""black"">Hello,</font></p>"

However, the issue that comes with this is font size=""3"" does not actually give font size 3, it gives font size 10. I'm trying to get to 11. font size=""4"" produces size 13.5.


TL;DR:

What's the VBA code to send an Outlook email from my second email account?

What's the VBA code to get font size 11 using HTML format?

SentOnBehalfOfName is a little tricky. See here where it works when it precedes Display. SentOnBehalfOf not working in Excel 2010 VBA Code

You can use "style=font-size:11pt" as described here Change HTML email body font type and size in VBA

The SendUsingAccount property of the MailItem class allows to set an Account object that represents the account under which the MailItem is to be sent. For example:

 Sub SendUsingAccount() 
  Dim oAccount As Outlook.account  
  For Each oAccount In Application.Session.Accounts 
  If oAccount.AccountType = olPop3 Then  
   Dim oMail As Outlook.MailItem  
   Set oMail = Application.CreateItem(olMailItem)  
   oMail.Subject = "Sent using POP3 Account"  
   oMail.Recipients.Add ("someone@example.com")  
   oMail.Recipients.ResolveAll  
   oMail.SendUsingAccount = oAccount  
   oMail.Send  
  End If  
 Next  
End Sub 

Try to use the Word object model to change the font size in emails. See Chapter 17: Working with Item Bodies for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM