从excel发送outlook电子邮件并在电子邮件中添加超链接,在打开电子表格时会转到行号

时间:2015-10-20 10:02:18

标签: excel vba excel-vba outlook outlook-vba

我有一个excel电子表格,使用vba发送电子邮件:

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

    strbody = "This is email text, click the link <a href='C:\test.xlsm & Range("F" & ActiveCell.Row).Address'></a>"

    On Error Resume Next


    With OutMail
        .To = "####"
        .CC = ""
        .BCC = ""
        .Subject = "Sales Tracker: A New Task has been added to the tracker"
        .HTMLBody = strbody & strbody2 & strbody3 & strbody4
        .Send 'displays outlook email
        'Application.SendKeys "%s" 'presses send as a send key
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing 

当用户点击活动行中的特定单元格时,会发送电子邮件。

我将excel电子表格包含在带有超链接的电子邮件中,但现在我想能够在超链接上添加一些东西,这将允许我包含用户点击的行的单元格引用当他们发送电子邮件时。

这个想法是打开时的超链接将打开电子表格,并将用户带到链接引用的行并突出显示它。

有人可以告诉我怎么做吗?提前致谢

2 个答案:

答案 0 :(得分:1)

您错过了链接中工作表的引用(即使我不确定这是否足够),请尝试以下操作:

href='[C:\test.xlsm]" & ActiveSheet.Name & "!" & Range("F" & ActiveCell.Row).Address & "'></a>"

匹配此格式:

href='[C:\test.xlsm]SheetName!A1'

更重要的是,你忘了正确地关闭引号,所以在这里:

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

strbody = "This is email text, click the link <a href='[C:\test.xlsm]" & _
            ActiveSheet.Name & "!" & Range("F" & ActiveCell.Row).Address & "'></a>"

On Error Resume Next


With OutMail
    .To = "####"
    .CC = ""
    .BCC = ""
    .Subject = "Sales Tracker: A New Task has been added to the tracker"
    .HTMLBody = strbody & strbody2 & strbody3 & strbody4
    .Send 'displays outlook email
    'Application.SendKeys "%s" 'presses send as a send key
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing

答案 1 :(得分:0)

我不确定是否可以使用超链接进行,很可能不是。我想到的只是将Worksheet_Activate()事件添加到您附加的电子表格中,并指向您希望的范围,但不包含超链接。