0

我需要帮助编写一些循环来为范围内的每个单元格创建一个命令按钮。
我实现了根据需要创建尽可能多的按钮。我的问题是为每个人设置 .OnAction 属性。
每个按钮都会通过 Lotus Notes 向指定地址发送一封电子邮件,邮件的主题和正文存储在相邻的单元格中。该代码已经有效,这是发送电子邮件的过程:

Sub Send(ByVal MailAddress, Subject, Message As String)

Dim Maildb As Object
Dim MailDoc As Object
Dim Body As Object
Dim Session As Object

    Set Session = CreateObject("Lotus.NotesSession")
    Call Session.Initialize
    'Call Session.Initialize("password")
    UserName = Session.UserName
    Set Maildb = Session.GetDatabase("", "C:\Lotus\Notes\Data\names.nsf")
    'Set Maildb = Session.GetDatabase("", MailDbName)
    If Not Maildb.IsOpen = True Then Call Maildb.Open
    Set MailDoc = Maildb.CreateDocument
    Call MailDoc.ReplaceItemValue("Form", "Memo")
    Call MailDoc.ReplaceItemValue("SendTo", MailAddress)
    Call MailDoc.ReplaceItemValue("Subject", Subject)
    Set Body = MailDoc.CreateRichTextItem("Body")
    Call Body.AppendText(Message)
    MailDoc.SaveMessageOnSend = True
    Call MailDoc.ReplaceItemValue("PostedDate", Now())
    Call MailDoc.Send(False)

    Set Maildb = Nothing
    Set MailDoc = Nothing
    Set Body = Nothing
    Set Session = Nothing
End Sub

现在,我想做的是在工作簿打开时创建按钮,遍历工作表的第一个编译列。接下来,我将在向工作表添加/删除行时添加/删除按钮。为此,我目前有以下代码:

Private Sub Workbook_Open()

   Dim L As Integer
   Dim t As Range
   Dim btn As Button
   Dim arg As String

   Application.ScreenUpdating = False
   ActiveSheet.Buttons.Delete
   Sheets(1).Activate
   L = Application.WorksheetFunction.CountA(Range("C:C"))
   For i = 2 To L
        Set t = ActiveSheet.Range(Cells(i, 1), Cells(i, 1))
        Set btn = ActiveSheet.Buttons.Add(t.Left, t.Top + 5, t.Width, 20)
        'arg = "'Invia  Range("J1").Value , Cells(i, t.Column + 2).Value , Cells(i, t.Column+3).Value '"
        With btn
           .OnAction = arg
           .Caption = "Invia"
           .Name = "Btn" & i
        End With
   Next i
End Sub

我的问题是我无法以正确的方式编写要传递给 OnAction 属性的字符串。那应该是使用 3 个参数调用程序发送:1)邮件地址:在单元格 J1(静态)中找到 2)主题:在循环的当前行(i)的 C 列中找到 3)正文:在 D 列中找到循环的当前行 (i)

无法让它工作。我对 VBA 很陌生,我对所有这些引号、单引号和双引号都感到疯狂。
有人可以帮助我吗?
提前 Mhanks,
马可

4

1 回答 1

2
arg = "'Send ""{1}"", ""{2}"", ""{3}"" '" 

arg = Replace(arg,"{1}", Range("J1").Value)
arg = Replace(arg,"{2}", Cells(i, t.Column + 2).Value)
arg = Replace(arg,"{3}", Cells(i, t.Column+3).Value)

.OnAction = arg
于 2012-11-16T21:51:20.450 回答