VBA Hyperlink (1 Viewer)

jacko6

Member
Local time
Today, 18:43
Joined
Jul 17, 2023
Messages
30
I'm trying to format a hyperlink string in vba and then use the hyperlink in the body of the email msg I am creating using the CDO library.

However the hyperlink displays as is, rather than just the text.

The easiest way to test what I mean is to create a string in VBA that contains the url and the text to display, then paste it into a mail client to see if it works. I can't get it to work.

Note: I would paste the vba string example into this post but it keeps getting flagged as spam and won't let me!
 
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Today, 03:43
Joined
Oct 29, 2018
Messages
21,682
Hi. A hyperlink is an HTML tag, which is just a text. To properly display it, the email client has to be in Rich Text or HTML mode. Have you tried that?
 

jacko6

Member
Local time
Today, 18:43
Joined
Jul 17, 2023
Messages
30
Yes I have tried that. Try pasting the text in the attached image into a mail client to see what I mean.

(I had to use an image as pasting the link into this post results in it getting blocked as spam)

hlinkissue.png
 

Mike Krailo

Well-known member
Local time
Today, 06:43
Joined
Mar 28, 2020
Messages
1,139
OK, it doesn't copy all of the text in the image for me. I get the following:
Code:
<a href=https://www.w3sch001s . com/>Visit

Code:
<a href=https://www.w3schools.com/>Visit W3Schools.com!</a>
If I copy and paste this text into an email client, it just pastes the html tag verbatim, it does not create a link. Usually there is an insert hyperlink function in an email client to handle that. This is exactly how you enter hyperlinks in an access hyperlink field. So I'm not sure what you are trying to do here.
 
Last edited:

jacko6

Member
Local time
Today, 18:43
Joined
Jul 17, 2023
Messages
30
Yes it pastes verbatim which illustrates the issue.

So referring to my original post, I am creating the hyperlink string in vba, then using it in the body of an email which I create via vba using the CDO library, and I get the same result as I do when pasting into a mail msg.

Is there a way of formatting the hyperlink string in vba so the email msg displays the "text to display" part only?
 

Mike Krailo

Well-known member
Local time
Today, 06:43
Joined
Mar 28, 2020
Messages
1,139
If we are talking about an access hyperlink field, then just pressing F2 on the field will convert the displayed text into the hashtag separated version. then copying that first part of the text string will get you exactly what you want. The code could split on the # symbol and you can easily get the display text only.

Code:
AWF Thread Text#https://www.access-programmers.co.uk/forums/threads/vba-hyperlink.331479/#
 

jacko6

Member
Local time
Today, 18:43
Joined
Jul 17, 2023
Messages
30
Thanks, but what I am talking about is creating an email msg in VBA that contains a hyperlink in the body of the email.

As an eg, in the email the hyperlink should look like this Visit W3Schools.com!

And when you click the hyperlink you would be taken to the url.
 

jacko6

Member
Local time
Today, 18:43
Joined
Jul 17, 2023
Messages
30
I solved it.

In the CDO library I was using the .TextBody parameter. I changed this to .htmlBody and the link now formats correctly. Of course this means you need to format the entire body as html, not just the link.

I would post the code but no matter how I try, I get a popup saying the post contains spam.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 03:43
Joined
Oct 29, 2018
Messages
21,682
I solved it.

In the CDO library I was using the .TextBody parameter. I changed this to .htmlBody and the link now formats correctly. Of course this means you need to format the entire body as html, not just the link.

I would post the code but no matter how I try, I get a popup saying the post contains spam.
Congratulations. Glad to hear you got it sorted out. Good luck with your project.
 

Users who are viewing this thread

Top Bottom