带有活动单元格偏移量的 运行 宏的超链接或按钮
Hyperlink or button to run macro with active cell offsets
我正在寻找一种轻松发送电子邮件的方法。我有一个 excel 文件,每天添加大约 20 行客户编号和订单编号。还有电子邮件地址、不同的主题和机构,具体取决于国家/地区。
我使用 Lotus Notes 并设置了所有代码以发送和附加文件。我的用于发送电子邮件的宏使用活动单元格上的偏移量。所以目前,我正在点击某个单元格,然后按下一个键位来发送电子邮件。
但是,我想对其进行更改,以便人们可以单击超级link 或每行上的按钮来创建电子邮件。我尝试使用表单和 activex 控件中的按钮,但这使我的文件太慢了。
然后我研究了一种在您单击超级时激活宏的方法link。
这是我在网上找到的。
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Select Case Target.Range.Address
Case "$B"
Call myMacro
Case Else
End Select
End Sub
但这只适用于单元格 B3 中的 link。我该怎么做,如果我单击 B 列中的任何 link,宏将 运行?
如果还有其他解决方案,请随时告诉我。
亲切的问候,
编辑 1:
这是电子邮件的代码
Sub myMacro(Target As Range)
'Send an e-mail & attachment using Lotus Not(s)
'Original Code by Nate Oliver (NateO)
'Declare Variables for file and macro setup
Dim UserName As String, MailDbName As String, Recipient As String, ccRecipient As String, Attachment1 As String
Dim Maildb As Object, MailDoc As Object, AttachME As Object, Session As Object
Dim EmbedObj1 As Object
With Application
.ScreenUpdating = False
.DisplayAlerts = False
' Open and locate current LOTUS NOTES User
Set Session = CreateObject("Notes.NotesSession")
UserName = Session.UserName
MailDbName = _
Left$(UserName, 1) & Right$(UserName, (Len(UserName) - InStr(1, UserName, " "))) & ".nsf"
Set Maildb = Session.GETDATABASE("", MailDbName)
If Maildb.IsOpen = True Then
Else
Maildb.OPENMAIL
End If
Set MailDoc = Maildb.CreateDocument
MailDoc.Form = "Memo"
Recipient = Target.Offset(0, 1).Value
MailDoc.SendTo = Recipient
ccRecipient = Target.Offset(0, 2).Value
MailDoc.CopyTo = ccRecipient
MailDoc.Subject = Target.Offset(0, 3).Value
MailDoc.Body = Target.Offset(0, 4).Value
Dim Orderno
Dim myPath
Dim myFile
Orderno = Target.Offset(0, 5).Value
myPath = ThisWorkbook.Path & "D:\Berry\Order Confirmations\VBAtest\"
myFile = Dir(myPath & "*" & Orderno & "*.pdf*")
Attachment1 = (myPath & myFile)
MsgBox (Attachment1)
If Attachment1 <> "" Then
On Error Resume Next
Set AttachME = MailDoc.CREATERICHTEXTITEM("attachment1")
Set EmbedObj1 = AttachME.embedobject(1454, "attachment1", (myPath & myFile), "")
On Error Resume Next
End If
Set workspace = CreateObject("Notes.NotesUIWorkspace")
Call workspace.EDITDOCUMENT(True, MailDoc).GOTOFIELD("Body")
Set Maildb = Nothing
Set MailDoc = Nothing
Set AttachME = Nothing
Set Session = Nothing
Set EmbedObj1 = Nothing
.ScreenUpdating = True
.DisplayAlerts = True
End With
errorhandler1:
Set Maildb = Nothing
Set MailDoc = Nothing
Set AttachME = Nothing
Set Session = Nothing
Set EmbedObj1 = Nothing
要在单击 B 列上的任何内容时将宏设为 运行:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Select Case Target.Range.Column
Case 2 'Two being the column number
Call myMacro
Case Else
End Select
End Sub
当您使用 Offset 来获取电子邮件的值时,您将偏移目标以获得正确的值,因此如果您要向您的宏传递一个参数,例如:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Select Case Target.Range.Column
Case 2 'Two being the column number
Call myMacro(Target.Range)
Case Else
End Select
End Sub
然后在您的宏中您可以执行以下操作:
Sub myMacro(Target as Range)
Target.offset(0,1).value 'to get the value to the right of the clicked cell
.....
End sub
我正在寻找一种轻松发送电子邮件的方法。我有一个 excel 文件,每天添加大约 20 行客户编号和订单编号。还有电子邮件地址、不同的主题和机构,具体取决于国家/地区。 我使用 Lotus Notes 并设置了所有代码以发送和附加文件。我的用于发送电子邮件的宏使用活动单元格上的偏移量。所以目前,我正在点击某个单元格,然后按下一个键位来发送电子邮件。
但是,我想对其进行更改,以便人们可以单击超级link 或每行上的按钮来创建电子邮件。我尝试使用表单和 activex 控件中的按钮,但这使我的文件太慢了。
然后我研究了一种在您单击超级时激活宏的方法link。
这是我在网上找到的。
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Select Case Target.Range.Address
Case "$B"
Call myMacro
Case Else
End Select
End Sub
但这只适用于单元格 B3 中的 link。我该怎么做,如果我单击 B 列中的任何 link,宏将 运行?
如果还有其他解决方案,请随时告诉我。
亲切的问候,
编辑 1:
这是电子邮件的代码
Sub myMacro(Target As Range)
'Send an e-mail & attachment using Lotus Not(s)
'Original Code by Nate Oliver (NateO)
'Declare Variables for file and macro setup
Dim UserName As String, MailDbName As String, Recipient As String, ccRecipient As String, Attachment1 As String
Dim Maildb As Object, MailDoc As Object, AttachME As Object, Session As Object
Dim EmbedObj1 As Object
With Application
.ScreenUpdating = False
.DisplayAlerts = False
' Open and locate current LOTUS NOTES User
Set Session = CreateObject("Notes.NotesSession")
UserName = Session.UserName
MailDbName = _
Left$(UserName, 1) & Right$(UserName, (Len(UserName) - InStr(1, UserName, " "))) & ".nsf"
Set Maildb = Session.GETDATABASE("", MailDbName)
If Maildb.IsOpen = True Then
Else
Maildb.OPENMAIL
End If
Set MailDoc = Maildb.CreateDocument
MailDoc.Form = "Memo"
Recipient = Target.Offset(0, 1).Value
MailDoc.SendTo = Recipient
ccRecipient = Target.Offset(0, 2).Value
MailDoc.CopyTo = ccRecipient
MailDoc.Subject = Target.Offset(0, 3).Value
MailDoc.Body = Target.Offset(0, 4).Value
Dim Orderno
Dim myPath
Dim myFile
Orderno = Target.Offset(0, 5).Value
myPath = ThisWorkbook.Path & "D:\Berry\Order Confirmations\VBAtest\"
myFile = Dir(myPath & "*" & Orderno & "*.pdf*")
Attachment1 = (myPath & myFile)
MsgBox (Attachment1)
If Attachment1 <> "" Then
On Error Resume Next
Set AttachME = MailDoc.CREATERICHTEXTITEM("attachment1")
Set EmbedObj1 = AttachME.embedobject(1454, "attachment1", (myPath & myFile), "")
On Error Resume Next
End If
Set workspace = CreateObject("Notes.NotesUIWorkspace")
Call workspace.EDITDOCUMENT(True, MailDoc).GOTOFIELD("Body")
Set Maildb = Nothing
Set MailDoc = Nothing
Set AttachME = Nothing
Set Session = Nothing
Set EmbedObj1 = Nothing
.ScreenUpdating = True
.DisplayAlerts = True
End With
errorhandler1:
Set Maildb = Nothing
Set MailDoc = Nothing
Set AttachME = Nothing
Set Session = Nothing
Set EmbedObj1 = Nothing
要在单击 B 列上的任何内容时将宏设为 运行:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Select Case Target.Range.Column
Case 2 'Two being the column number
Call myMacro
Case Else
End Select
End Sub
当您使用 Offset 来获取电子邮件的值时,您将偏移目标以获得正确的值,因此如果您要向您的宏传递一个参数,例如:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Select Case Target.Range.Column
Case 2 'Two being the column number
Call myMacro(Target.Range)
Case Else
End Select
End Sub
然后在您的宏中您可以执行以下操作:
Sub myMacro(Target as Range)
Target.offset(0,1).value 'to get the value to the right of the clicked cell
.....
End sub