VBA Code for Bold Text & Hyperlink (1 Viewer)

stu_c

Registered User.
Local time
Today, 22:02
Joined
Sep 20, 2007
Messages
489
Hi all
I have got some VBA code that opens up outlook, inputs all the subject, To, and body of the email etc
I ideally trying to do it so certain text is bold how would I do this?
also I want a CLICK HERE link that is a hyper link rather than showing the whole website address any ideas?

the reason everything is done through VBA is because due to our security measures I was unable to use an actual template but only a blank one.

Set myOlApp = CreateObject("Outlook.Application")

Set myitem = myOlApp.CreateItemFromTemplate(CurrentProject.Path & "\TemplateFiles\EmailToHR.oft")

myitem.Subject = UCase("HR REPORT COMPLETED - " & "[" & [Forms]![FRM_TBLALL_FullDetails].[Form]![LblRef] & "]")

myitem.To = UCase([Forms]![FRM_TBLALL_FullDetails]![SSFRM_TBLALL_StaffDetails].[Form]![CmbSupervisor].Column(0))

myitem.display

myitem.body = "The HR Report has now been completed. USBs containing the download report will now be placed in HR Secure storage for your collection under the Ref numbers below." & Chr(13) & Chr(10) & _

"HR Report Reference Numbers:" & Chr(13) & Chr(10) & _

"Working Copy - " & [Forms]![FRM_TBLALL_FullDetails]![SSFRM_TBLALL_StaffDetails].[Form]![LblWorkingCopyRef] & Chr(13) & Chr(10) & _ MAKE THE REF NUMBER SHOW BUT AS A HYPER LINK

"Backup Copy - " & [Forms]![FRM_TBLALL_FullDetails]![SSFRM_TBLALL_StaffDetails].[Form]![LblBackupCopyRef] & Chr(13) & Chr(10) & _ MAKE THE REF NUMBER SHOW BUT AS A HYPER LINK

"Your USB BitLocker password is:" & Chr(13) & Chr(10) & _

[Forms]![FRM_TBLALL_FullDetails]![SSFRM_TBLALL_StaffDetails].[Form]![BitLockerPassword] & Chr(13) & Chr(10)

"If you have any issues / queries please contact the your Local HR hub." & Chr(13) & Chr(10)
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 14:02
Joined
Aug 30, 2003
Messages
36,126
For starters you need htmlBody instead of Body. Then you use HTML within the body text, like

<B>Bold text here</B>

Some research should show how to put in the hyperlink.
 

stu_c

Registered User.
Local time
Today, 22:02
Joined
Sep 20, 2007
Messages
489
Thank you, do i simply just change it to myitem.HTMLbody?
For starters you need htmlBody instead of Body. Then you use HTML within the body text, like

<B>Bold text here</B>

Some research should show how to put in the hyperlink.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 14:02
Joined
Aug 30, 2003
Messages
36,126
Well, as I said that's for starters. Then you need html tags in your text.
 

stu_c

Registered User.
Local time
Today, 22:02
Joined
Sep 20, 2007
Messages
489
hello
I know
Well, as I said that's for starters. Then you need html tags in your text.
not having much luck to be honest :(

150 myitem.body = owoFormatHTML
160 myitem.HTMLBody = _
<B>"The HR Report has now been completed. USBs containing the download report will now be placed in HR Secure storage for your collection under the Ref numbers below"</B>
'End With
260 Set oEmailItem = Nothing
270 Set oOutlook = Nothing
 

Minty

AWF VIP
Local time
Today, 22:02
Joined
Jul 26, 2013
Messages
10,371
The HTML Tags have to be INSIDE the string to be used:

myitem.HTMLBody = _
"<B>The HR Report has now been completed. USBs containing the download report will now be placed in HR Secure storage for your collection under the Ref numbers below</B>"
 

stu_c

Registered User.
Local time
Today, 22:02
Joined
Sep 20, 2007
Messages
489
Hi Minty
I tried the following code
Set myOlApp = CreateObject("Outlook.Application")
Set myitem = myOlApp.CreateItemFromTemplate(CurrentProject.Path & "\TemplateFiles\EmailToHR.oft")

myitem.Subject = UCase("HR REPORT COMPLETED - " & "[" & [Forms]![FRM_TBLALL_FullDetails].[Form]![LblRef] & "]")

myitem.To = UCase([Forms]![FRM_TBLALL_FullDetails]![SSFRM_TBLALL_StaffDetails].[Form]![CmbSupervisor].Column(0))

myitem.display

myitem.HTMLbody = "<B>The HR Report has now been completed. USBs containing the download report will now be placed in HR Secure storage for your collection under the Ref numbers below.</B>"

When I do this the error message Run-time 438, object doesn't support this property shows
The HTML Tags have to be INSIDE the string to be used:

myitem.HTMLBody = _
"<B>The HR Report has now been completed. USBs containing the download report will now be placed in HR Secure storage for your collection under the Ref numbers below</B>"
 

Minty

AWF VIP
Local time
Today, 22:02
Joined
Jul 26, 2013
Messages
10,371
Sorry I hadn't read the whole thread.
You need

Code:
myItem.BodyFormat = olFormatHTML

to switch it to an HTML format, before you can assign something to the HTMLBody property
 

stu_c

Registered User.
Local time
Today, 22:02
Joined
Sep 20, 2007
Messages
489
Hi Minty
thank you again for your help, seems to be showing the error message
message Run-time 5, invalid call argument :

Code:
Set myOlApp = CreateObject("Outlook.Application")
Set myitem = myOlApp.CreateItemFromTemplate(CurrentProject.Path & "\TemplateFiles\EmailToHR.oft")

myitem.Subject = UCase("HR REPORT COMPLETED - " & "[" & [Forms]![FRM_TBLALL_FullDetails].[Form]![LblRef] & "]")

myitem.To = UCase([Forms]![FRM_TBLALL_FullDetails]![SSFRM_TBLALL_StaffDetails].[Form]![CmbSupervisor].Column(0))

myitem.display
myItem.BodyFormat = olFormatHTML
myItem.HMTLbody = _
myitem.HTMLbody = "<B>The HR Report has now been completed. USBs containing the download report will now be placed in HR Secure storage for your collection under the Ref numbers below.</B>"
 

Minty

AWF VIP
Local time
Today, 22:02
Joined
Jul 26, 2013
Messages
10,371
And on which line is the error ??

I suspect this one:
Code:
myItem.HMTLbody = _

Remove it, it's not doing anything. Except breaking things.
 

stu_c

Registered User.
Local time
Today, 22:02
Joined
Sep 20, 2007
Messages
489
And on which line is the error ??

I suspect this one:
Code:
myItem.HMTLbody = _

Remove it, it's not doing anything. Except breaking things.
Hello
its highlighting
Code:
myItem.BodyFormat = olFormatHTML

I have removed the
Code:
myItem.HMTLbody = _
still showing the same error
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 14:02
Joined
Aug 30, 2003
Messages
36,126
Maybe it has to do with my defaults but I don't set the BodyFormat property. Just:

MyMail.htmlBody = strHeader & "<BR><BR>" & strBody & "<BR><BR>" & strSig

Where the various string variables could have html codes within, as well as the <BR> included on this line.
 

June7

AWF VIP
Local time
Today, 13:02
Joined
Mar 9, 2014
Messages
5,473
I have BodyFormat in my code and the property does show up in Intellisense.
 

Minty

AWF VIP
Local time
Today, 22:02
Joined
Jul 26, 2013
Messages
10,371
Can you show your current code and highlight the line that has an error?
 

Neros

New member
Local time
Today, 18:02
Joined
Oct 14, 2023
Messages
6
Hello
its highlighting
Code:
myItem.BodyFormat = olFormatHTML

I have removed the
Code:
myItem.HMTLbody = _
still showing the same error
Try changing the constant olFormatHTML to number 2.
 

cheekybuddha

AWF VIP
Local time
Today, 22:02
Joined
Jul 21, 2014
Messages
2,280
Since you are using late-binding your VBA has no knowledge of what olFormatHTML is.

You need to declare the constant yourself.

Add, along with you variable declarations:
Code:
Const olFormatHTML As Integer = 2
 

cheekybuddha

AWF VIP
Local time
Today, 22:02
Joined
Jul 21, 2014
Messages
2,280
BTW, do you have Option Explicit declared at the top of every code module (above or below Option Compare Database)?
 

Users who are viewing this thread

Top Bottom