从HYPERLINK运行VBA()

我试图让我的用户从Excel电子表格中轻松发送电子邮件,他们喜欢用它作为数据input和操作的前端。 我的意图是写一个函数:

generateEmail(name, manager, cc) 

并且使得该function可以由用户高兴地调用。

在这种情况下,Excel调用Outlook,创build一个新的邮件,并转储包含格式化的电子邮件发送的文本,其中将包含一些值,从他们目前正在工作的表中dynamic地改变每行。 这是VBAfunction:

 Function generateEmail(name, manager, cc) Dim OutApp As Object Dim OutMail As Object Dim strbody As String Set OutApp = CreateObject("Outlook.Application") Set OutMail = OutApp.CreateItem(0) strbody = "To Service Desk:" & vbNewLine & vbNewLine & _ "Please open a ticket for CS/oe/ns/telecom/dal to request a new extension. Please find the details attached:" & vbNewLine & vbNewLine & _ "Name: " & name & vbNewLine & _ "Manager: " & manager & vbNewLine & _ "CC: " & cc On Error Resume Next With OutMail .To = "helpdesk@test.com" .cc = "" .BCC = "" .Subject = "New Extension Request" .Body = strbody .Display End With On Error GoTo 0 Set OutMail = Nothing Set OutApp = Nothing End Function 

然后,在表格的最右边一栏中,我添加了一个链接:

 =HYPERLINK(generateEmail(H2, I2, M2), "Generate Email") 

其中H2,I2和M2是我需要注入到将生成的电子邮件中的值。 用户也将有H3,I3,M3,H4,I4,M4等数据。

这实际上按预期工作,但是当我使超链接显示在电子表格上时, 只要Excel检测到链接上的鼠标hover ,就会触发事件 。 我希望这样做,所以链接是可点击的, 只有在用户点击后才能激活该function。

有没有办法做到这一点?

我的search已经空了,基本上build议人们从VBA本身手动创build每个单元的链接。 由于我的用户存储的数据集将随着时间的推移而增长,因此每当用户添加logging时,都需要创build一个新的“发送电子邮件”链接。

要仅在点击时运行该function,您可以尝试input为子地址:

 =HYPERLINK("#generateEmail(H2, I2, M2)", "Generate Email") 

然后在代码中添加一行以返回当前地址:

 Function generateEmail(name, manager, cc) Set generateEmail = Selection 'Paste Outlook Code Here End Function 

注意这个方法并没有在主vba线程上执行,所以为了testing你可以在代码中插入语句而不是单步执行,例如:

 Range("A10:C10") = Array(name, manager, cc) 

我遇到了一个项目类似的限制。 我不得不放弃使用超链接,并使用Worksheet_SelectionChange事件创build一个解决方法。 假设你的公式应该在N列上,请考虑在目标工作表上插入以下代码(不是在模块中):

 Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Target.Column = 14 Then Call generateEmail(Target.Offset(0, -6).Value2, Target.Offset(0, -5).Value2, Target.Offset(0, -1).Value2) End If End Sub 

然后,你可以在该列添加任何文本(即“发送电子邮件”),当有人点击该单元格时,你的代码将专门用该行的数据进行激发,希望这有助于。

使用超链接来触发操作有点棘手:

  • HYPERLINK()链接不会触发Worksheet_FollowHyperlink事件处理程序
  • 当使用“插入超链接”types的链接时,你需要一个有效的目标地址,当用户点击一个链接时,这会导致select跳转。

这里有一种方法 – 将这些代码放在工作表模块中:

 Option Explicit Dim oldAdd As String 'When a link is clicked, this gets triggered first ' if the clicked cell has a hyperlink, remember its address Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim c As Range 'only want single-cell selections... If Target.Cells.CountLarge > 1 Then Exit Sub Else 'cell has a link? Remember the address... If Target.Hyperlinks.Count > 0 Then oldAdd = Target.Address() End If End If End Sub 'This is called immediately after the SelectionChange event, ' so we can use the stored address to reset the selection back ' to the clicked cell, instead of wherever its link was pointing to Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink) Dim c As Range If oldAdd <> "" Then Set c = Me.Range(oldAdd) c.Select 'undo any navigation 'Here you could switch on the link text if you have ' links which need to trigger different actions If c.Value = "Send Mail" Then MsgBox "Sending mail!" 'generateEmail [use values from cells relative to c] End If End If End Sub 

假定所有的链接都指向与链接所在的页面相同的页面。

您的电子邮件的正文与mailto超链接使用的时间不会过长。 考虑http://www.datapigtechnologies.com/blog/index.php/emailing-from-excel-using-the-hyperlink-function/

这种超链接只会在点击时触发。