Hoppa till huvudinnehåll

Hur skickar jag e-post från Excel med hyperlänk i e-postkroppen?

Har du någonsin försökt skicka ett e-postmeddelande med VBA-kod i Excel? Och hur lägger man till hyperlänkar till e-postmeddelandets brödtext när man skickar e-post? Den här artikeln hjälper dig att lösa detta problem.

Infoga hyperlänk i e-postkroppen med VBA-kod


Infoga hyperlänk i e-postkroppen med VBA-kod

Nedanstående VBA-kod kan hjälpa till att skicka e-post från Excel och infoga specifik hyperlänk i e-postkroppen. Gör så här.

1. Starta din arbetsbok, tryck på andra + F11 nycklar för att öppna Microsoft Visual Basic för applikationer fönster.

2. I Microsoft Visual Basic för applikationer fönstret klickar Insert > Modul, och kopiera sedan VBA-koden nedan till modulredigeraren.

VBA-kod: Infoga hyperlänk i e-postkroppen

Sub EmailHyperlink()
'updated by Extendoffice 20190815
Dim xOtl As Object
Dim xOtlMail As Object
Dim xStrBody As String
    xStrBody = "Hi there:" & "<br>" _
              & "Please click " & "<a href=" & "http://www.extendoffice.com"">Here</a> to open the page" & "<br>" _
              & "Thank you."
    On Error Resume Next
    Set xOtl = CreateObject("Outlook.Application")
    Set xOtlMail = xOtl.CreateItem(olMailItem)
    With xOtlMail
        .To = "Email Address"
        .CC = "Email Address "
        .BCC = " Email Address "
        .Subject = "Subject line"
        .HTMLBody = .HTMLBody & xStrBody
        .Display
    End With
    Set xOtl = Nothing
    Set xOtlMail = Nothing
End Sub

Anmärkningar:

  • Ändra kroppsinnehållet och hyperlänken i xStrBody linje.
  • Ersätt "E-postadress"I . Till, .DC och.BCC rader med de faktiska e-postadresserna du skickar e-post till. Om du inte behöver CC- och BCC-raderna tar du bara bort dem från hela koden eller lägger till en enda offert före raderna, t.ex. 'CC = "E-postadress".
  • Ersätt "Ämnesrad"I .Ämne linje med ditt e-postämne eget.

3. tryck på F5 för att köra koden. Sedan skapas e-postmeddelandet med angivna fält och brödtext med hyperlänk listad inuti, klicka på knappen Skicka för att skicka den.


Relaterade artiklar

Skicka automatiskt e-post baserat på cellvärde i Excel
Antag att du vill skicka ett e-postmeddelande via Outlook till en viss mottagare baserat på ett angivet cellvärde i Excel. Till exempel, när värdet för cell D7 i ett kalkylblad är större än 200, skapas ett e-postmeddelande automatiskt. Den här artikeln introducerar en VBA-metod för dig att snabbt lösa detta problem.

Skicka e-post om du klickar på knappen i Excel
Om du antar att du måste skicka e-post via Outlook genom att klicka på en knapp i Excel-kalkylbladet, hur kan du göra det? Denna artikel kommer att introducera en VBA-metod för att uppnå den i detaljer.

Skicka e-post om förfallodatum har uppnåtts i Excel
Anta att förfallodatum i kolumn C är mindre än eller lika med 7 dagar (nuvarande datum är 2017/9/13), skicka sedan en e-postpåminnelse till den angivna mottagaren i kolumn A med specificerat innehåll i kolumn B. Hur uppnår du det? Metoden i den här artikeln kan göra dig en tjänst.

Bästa kontorsproduktivitetsverktyg

🤖 Kutools AI Aide: Revolutionera dataanalys baserat på: Intelligent utförande   |  Generera kod  |  Skapa anpassade formler  |  Analysera data och generera diagram  |  Anropa Kutools funktioner.
Populära funktioner: Hitta, markera eller identifiera dubbletter   |  Ta bort tomma rader   |  Kombinera kolumner eller celler utan att förlora data   |   Rund utan formel ...
Superuppslag: Flera kriterier VLookup    VLookup med flera värden  |   VSök över flera ark   |   Fuzzy Lookup ....
Avancerad rullgardinslista: Skapa snabbt en rullgardinslista   |  Beroende rullgardinslista   |  Flervals-rullgardinslista ....
Kolumnhanterare: Lägg till ett specifikt antal kolumner  |  Flytta kolumner  |  Växla synlighetsstatus för dolda kolumner  |  Jämför intervall och kolumner ...
Utvalda funktioner: Rutnätsfokus   |  Designvy   |   Stor formelbar    Arbetsbok & Bladhanterare   |  Resursbibliotek (Automatisk text)   |  Datumväljare   |  Kombinera arbetsblad   |  Kryptera/Dekryptera celler    Skicka e-postmeddelanden efter lista   |  Superfilter   |   Specialfilter (filtrera fet/kursiv/genomstruken...) ...
Topp 15 verktygssatser12 text verktyg (lägga till text, Ta bort tecken, ...)   |   50+ Diagram Typer (Gantt Chart, ...)   |   40+ Praktiskt Formler (Beräkna ålder baserat på födelsedag, ...)   |   19 Införande verktyg (Infoga QR-kod, Infoga bild från sökväg, ...)   |   12 Konvertering verktyg (Siffror till ord, Valutaväxling, ...)   |   7 Slå ihop och dela verktyg (Avancerade kombinera rader, Dela celler, ...)   |   ... och mer

Uppgradera dina Excel-färdigheter med Kutools för Excel och upplev effektivitet som aldrig förr. Kutools för Excel erbjuder över 300 avancerade funktioner för att öka produktiviteten och spara tid.  Klicka här för att få den funktion du behöver mest...

Beskrivning


Fliken Office ger ett flikgränssnitt till Office och gör ditt arbete mycket enklare

  • Aktivera flikredigering och läsning i Word, Excel, PowerPoint, Publisher, Access, Visio och Project.
  • Öppna och skapa flera dokument i nya flikar i samma fönster, snarare än i nya fönster.
  • Ökar din produktivitet med 50 % och minskar hundratals musklick för dig varje dag!
Comments (17)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hallo, das läuft soweit sehr gut.
Ich möchte jedoch, das ein Link generiert wird zu der Datei mit dem Dateinamen der aktuell geöffneten Datei. ThisWorkbook.FullName oder so ähnlich.
Wie würde der Code dann aussehen???

Vielen Dank
This comment was minimized by the moderator on the site
Does anyone know how to combine/ add this with a code I am already using to send an email? The one bellow?

Public Sub CheckAndSendMail()
'Updated by Extendoffice 2018/11/22
Dim xRgDate As Range
Dim xRgSend As Range
Dim xRgText As Range
Dim xRgDone As Range
Dim xOutApp As Object
Dim xMailItem As Object
Dim xLastRow As Long
Dim vbCrLf As String
Dim xMailBody As String
Dim xRgDateVal As String
Dim xRgSendVal As String
Dim xMailSubject As String
Dim i As Long
On Error Resume Next
Set xRgDate = Application.InputBox("Please select the due date column:", "KuTools For Excel", , , , , , 8)
If xRgDate Is Nothing Then Exit Sub
Set xRgSend = Application.InputBox("Please select the recipients?email column:", "KuTools For Excel", , , , , , 8)
If xRgSend Is Nothing Then Exit Sub
Set xRgText = Application.InputBox("Select the column with reminded content in your email:", "KuTools For Excel", , , , , , 8)
If xRgText Is Nothing Then Exit Sub
xLastRow = xRgDate.Rows.count
Set xRgDate = xRgDate(1)
Set xRgSend = xRgSend(1)
Set xRgText = xRgText(1)
Set xOutApp = CreateObject("Outlook.Application")
For i = 1 To xLastRow
xRgDateVal = ""
xRgDateVal = xRgDate.Offset(i - 1).Value
If xRgDateVal <> "" Then
If CDate(xRgDateVal) - Date <= 7 And CDate(xRgDateVal) - Date > 0 Then
xRgSendVal = xRgSend.Offset(i - 1).Value
xMailSubject = xRgText.Offset(i - 1).Value & " on " & xRgDateVal
vbCrLf = "<br><br>"
xMailBody = "<HTML><BODY>"
xMailBody = xMailBody & "Dear " & xRgSendVal & vbCrLf
xMailBody = xMailBody & "Text : " & xRgText.Offset(i - 1).Value & vbCrLf
xMailBody = xMailBody & "</BODY></HTML>"
Set xMailItem = xOutApp.CreateItem(0)
With xMailItem
.Subject = xMailSubject
.To = xRgSendVal
.HTMLBody = xMailBody
.Display
'.Send
End With
Set xMailItem = Nothing
End If
End If
Next
Set xOutApp = Nothing
End Sub
This comment was minimized by the moderator on the site
Hi Crystal, no problem, thank you anyway.
This comment was minimized by the moderator on the site
Hallo,

wie würde der Code aussehen, wenn im Email-Text genau der Link zu der Datei stehen soll, aus der heraus die Email generiert wurde? Nach dem Motto: "Gehe zu Datei xxx (als Link) und trage dort etwas ein.

Vielen Dank und schöne Grüße
Florian
This comment was minimized by the moderator on the site
Hi Florian Mußler,
Please change the following lines
 xStrBody = "Hi there:" & "<br>" _
              & "Please click " & "<a href="/ & "http://www.extendoffice.com"">Here</a> to open the page" & "<br>" _
              & "Thank you."

to
 xStrBody = "Hi there:" & "<br>" _
              & "Go to the file " & "<a href="/ & "I:\Work\Crystal\2023\Sales Report.xlsx"">Sales Report</a> and enter the content there" & "<br>" _
              & "Thank you."

where "I:\Work\Crystal\2023\Sales Report.xlsx" is the path of the file.
This comment was minimized by the moderator on the site
Thank you for your help. This already works great. Do you know how to write the code, if I have to extract the file address out of a cell?
This comment was minimized by the moderator on the site
Hi crystal,

thank you very much for your reply. This is what I came up with at first. The thing is, that the file we are talking about is based on a template. That means, that the file name is changed to an individual file name, that is always different. So the file name (link) in the code should be a variable, always leading to this exacte file (in which the code is running).

I hope I could describe this correctly, so you understand.

Thank you.
This comment was minimized by the moderator on the site
Hi Florian,

I understand what you mean now. But I'm sorry I don't know how to create a hyperlink to a workbook that contains a variable name.
This comment was minimized by the moderator on the site
Bonjour,
Merci pour cette procédure.
Comment puis-je remplacer le lien (qui est fixe) par un lien variable ?
...
Dim xStrBody As String
Dim Lien As String
Lien = "https://www.cyclodetente.be/"

xStrBody = "Hi there:" & "<br>" _
& "Please click " & "<a href="/ & "http://www.extendoffice.com"">Here</a> to open the page" & "<br>" _
& "Thank you."
...
Donc, remplacer "http://www.extendoffice.com" par la variable "Lien"

Un tout grand merci pour le retour,
This comment was minimized by the moderator on the site
Hi Christian,

Sorry I don't quite understand what you mean. For clarity, please attach a sample file or a screenshot with your data and desired results.
This comment was minimized by the moderator on the site
my link isn't clickable and I'm not sure why.

"Please Print <a href="/"http://codes" & Range("JobNumber") & "&ReportId=1"">" & "PhoneCodes</a>"

The link appears but I have to right-click on the email for it to open up. Any suggestions? Also, I don't really know much about VBA. This is something new I'm learning on my own.
This comment was minimized by the moderator on the site
Hi Maryanne,
By default, when you composing an email, hyperlinks are opened by clicking with pressing or holding the Ctrl key.
If you want to open a hyperlink with one click only, please turn off this function as follows.
1. In your Outlook, click File > Options.
2. In the Outlook Options dialog box, click Mail in the left pane, and then click the Editor Options button in the Compose Messages section;
3. In the Editor Options dialog box, click Advanced in the left pane, and then uncheck the Use CTRL + Click to follow hyperlink checkbox. See screenshot:
https://www.extendoffice.com/images/stories/comments/comment-picture-zxm/hyperlink.png
This comment was minimized by the moderator on the site
please i need help it doesnt work when the link have spaces in itfor ex file path MsgBox "C:\Users\Desktop\fs caché\Fusion fichier\TENDERING.xlsm"
xstrbody = "Request for an approval, <br> You can access to the file from " & "<a href= " & link & ">here</a>"
the link will be in this case : C:\Users\Desktop\fsany idea?
This comment was minimized by the moderator on the site
You can use: xstrbody = "Request for an approval, <br> You can access to the file from " & "<a href="/"" & link & """> here</a >"
This comment was minimized by the moderator on the site
this code is great, but how to send the current excel sheet in the email?
This comment was minimized by the moderator on the site
you use "xStrBody" part in your existing code, other search Excel to outlook email code online and use "xStrBody" this part.
This comment was minimized by the moderator on the site
you can use "xStrBody" part in other email code, which you are using now. otherwise search "excel to outlook email code".
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations