Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 有谁能帮我绑定一个自动打开按钮的代码和一个发送大量电子邮件的代码_Excel_Vba_Outlook - Fatal编程技术网

Excel 有谁能帮我绑定一个自动打开按钮的代码和一个发送大量电子邮件的代码

Excel 有谁能帮我绑定一个自动打开按钮的代码和一个发送大量电子邮件的代码,excel,vba,outlook,Excel,Vba,Outlook,这是打开按钮的代码: Sub a() Dim btn As Button Application.ScreenUpdating = False ActiveSheet.Buttons.Delete Dim t As Range For i = 1 To 3 Set t = ActiveSheet.Range(Cells(i, 3), Cells(i, 3)) Set btn = ActiveSheet.Butt

这是打开按钮的代码:

Sub a()
    Dim btn As Button 
    Application.ScreenUpdating = False
    ActiveSheet.Buttons.Delete
    Dim t As Range


        For i = 1 To 3
        Set t = ActiveSheet.Range(Cells(i, 3), Cells(i, 3))
        Set btn = ActiveSheet.Buttons.Add(t.Left, t.Top, t.Width, t.Height)
        With btn
         .OnAction = "btnS"
         .Caption = "Btn " & i
         .Name = "Btn" & i
   End With
Next i

Application.ScreenUpdating = True

End Sub

Sub btnS()
    MsgBox Application.Caller
End Sub
这是发送群发电子邮件的代码:

Sub SendEmail(address_mail As String, subject_mail As String, mail_body As String)


Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")

    Dim olMail As Outlook.MailItem
    Set olMail = olApp.CreateItem(olMailItem)

    olMail.To = address_mail
    olMail.Subject = subject_mail
    olMail.Body = mail_body

   On Error GoTo Cancel

    olMail.Send

Cancel:

End Sub
Sub SendMassEmail()

Dim addressString As String
addressString = ""
row_number = 2



Do
DoEvents

    row_number = row_number + 1
    addressString = addressString & Application.ActiveSheet.Range("C" & row_number) & ";"


    Loop Until row_number = 999
Call SendEmail(addressString, Application.ActiveSheet.Range("F9"), Application.ActiveSheet.Range("F10"))

End Sub

我真的不明白你为什么要把两个代码都绑定到一个里面。如果您能澄清一下,我们将不胜感激

因为只要两个代码在同一个模块中,您就可以创建一个新的子模块,并在需要时运行它,该子模块将一个接一个地运行两个代码:

Sub Bind()
Call a ' runs the first code
Call SendEmail ' runs the sendemail code
Call SendMassEmail ' runs the sendmassemail code
End Sub
上述代码将依次运行所有三个代码。你可以根据需要洗牌

编辑:

好的,根据我从你的评论中了解到的情况,你想要一个sub,它将创建一个按钮,当点击该按钮时,它将发送电子邮件

假设您的第二个代码一切正常,您可以这样做:

Sub a()
    Dim btn As Button
    Application.ScreenUpdating = False
    ActiveSheet.Buttons.Delete
    Dim t As Range

r = 1 ' select the row number where you want to create the button
c = 3 ' select the column number where you want to create the button

        Set t = ActiveSheet.Range(Cells(r, c), Cells(r, c))
        Set btn = ActiveSheet.Buttons.Add(t.Left, t.Top, t.Width, t.Height)
        With btn
         .OnAction = ActiveSheet.Name & ".SendEmail" ' select the name of your second code here.
         .Caption = "Btn"
         .Name = "Btn"
   End With

Application.ScreenUpdating = True

End Sub
通过这种方式,在活动工作表上创建一个按钮,当单击该按钮时,将运行第二个代码,如果第二个代码也在同一工作表中,该代码将执行所需的操作

我相信这就是你一直在寻找的

编辑2:

从你的评论来看,你想要的似乎是一个简单的邮件合并。如果是这样的话,那么请阅读更多关于邮件合并的信息,因为它会提供更多的功能

您所要求的内容可以在excel中完成,但要复杂得多,这就是方法:

创建2张工作表:工作表1姓名和电子邮件工作表2主题和正文 在工作表上添加ActiveX按钮:主题和正文 使其看起来如下所示:

最后,将以下代码添加到按钮:

Private Sub CommandButton1_Click()


LastRow = Sheet1.Cells(Sheet1.Rows.Count, "B").End(xlUp).Row 'find the last filled row with emails

For Each Cell In Worksheets("Names & Emails").Range("B2:B" & LastRow) 'sets the range of cells with emails
    If Cell.Value = "" Then GoTo Line1 'loop until a blank cell is reached i.e. lastrow
    x = x & Cell.Value & "; " 'appends all the email in one line while adding "; " in between
Next

Emails = Left([x], Len([x]) - 2) 'removes the last 2 characters ("; ") from the appended string as the seperator is not needed after the last email

Line1:

    Dim OutApp As Object
    Dim OutMail As Object
    Dim strbody As String

    Set OutApp = CreateObject("Outlook.Application")
    Set OlObjects = OutApp.GetNamespace("MAPI")
    Set OutMail = OutApp.CreateItem(olMailItem)

On Error Resume Next
    With OutMail
        .To = Emails 'inserts the appended line in the To field
        .Subject = Worksheets("Subject & Body").Range("B2").Value 'uses the value of B2 as the Subject of the Email
        .Body = Worksheets("Subject & Body").Range("B5").Value 'uses the value of B5 as the Body of the Email
        '.Display 'you can enable this line if you need to view the email before it's sent
        .Send 'sends the email
    End With

x = ""

End Sub

现在,无论何时单击该按钮,它都会向列表中的所有地址发送一封电子邮件,其中包含您将选择的主题和正文。

如果您准确地解释您正在尝试执行的操作,这会有所帮助-此处的预期事件顺序是什么?你到底遇到了什么问题?你好,基本上我不太理解第一个代码,但它基本上会弹出一个按钮,当点击它时,它会发送大量电子邮件。因此,我想将我的第一个代码与第二个代码进行协作,第二个代码在运行时可以自动打开按钮,因此调用按钮的第一个代码需要进行一些修改,以满足代码第二部分的需要。因此,当我运行代码时,它应该在任何工作表中自动打开按钮。如果有人可以试着运行第二部分,看看它做了什么,然后帮助我在第二部分中安装第一个代码,那么当单击按钮时,该按钮应该做什么?因为在你的第一个代码中,看起来你正在创建3个新按钮。嘿,第一个代码不是我的,所以我不太理解。我只想创建一个按钮,当点击该按钮时,它会向第二个代码中指定的特定列中的人发送大量电子邮件感谢您@CaptainABC的帮助。我有个小问题要问你。因此,在第二段代码中,我想写一个代码,其中邮件的主体和主题应该从另一张工作表中提取,该工作表将链接到包含电子邮件的工作表。@user2971393 Mmmm。。。你必须更具体一些。邮件的主题行和正文是否与所有收件人相同?如果是,主题行和主体存储在哪张表和哪些单元格中?