excel 我试图创建一个VBA宏电子邮件特定的工作人员的日常任务分配给他们

oknwwptz  于 2022-12-01  发布在  其他
关注(0)|答案(1)|浏览(85)

目前,我有一个标准宏,该宏通过电子邮件发送一个基本电子邮件模板,该模板将发送给所有员工,但我希望编辑该宏,使其包含分配给员工的电子邮件中分配的任务。
当前“A1”将包含员工姓名,“B1-B5”将包含任务,然后是一个空行,后面是下一个“员工”成员。(因此A7)将是下一个五线谱名称。(由于可以分配给每个工作人员的任务数量是随机的并且可以在任何一天改变,以上只是一个例子,让你大致了解电子表格是如何工作的)我有一张工作表,上面有员工的名字,旁边还有一个电子邮件地址的列表。
我仍然是一个非常新的编码,而在线寻找,我已经设法复制下面,并得到这个工作在我的电子表格。

Private Sub Workbook_AfterSave(ByVal Success As Boolean)
'Updated by Extendoffice 20181102
    Dim xOutApp As Object
    Dim xMailItem As Object
    Dim xName As String
    On Error Resume Next
    Set xOutApp = CreateObject("Outlook.Application")
    Set xMailItem = xOutApp.CreateItem(0)
    xName = ActiveWorkbook.FullName
    With xMailItem
        .To = "sales@stevesoultltd.co.uk"
        .CC = ""
        .Subject = "TEST"
        .Body = "Hi," & Chr(13) & Chr(13) & "File is now updated."
        .Attachments.Add xName
        .Display
       '.send
    End With
    Set xMailItem = Nothing
    Set xOutApp = Nothing
End Sub
j2qf4p5b

j2qf4p5b1#

你能把员工的电子邮件地址写在表格里吗,比如写在C1里经理名字的对面?(或者甚至代替经理的名字)
如果它在C1中,您的代码应该如下所示。

Private Sub Workbook_AfterSave(ByVal Success As Boolean)
    Dim xOutApp As Object, xMailItem As Object
    Dim xName As String, lManager as Long, lTask as Long, shtTasks as Worksheet
    Set xOutApp = CreateObject("Outlook.Application")
    Set shtTasks = ActiveSheet
    xName = ActiveWorkbook.FullName

   'Loop through Managers
    lManager = 1
    Do Until lManager >= shtTasks.UsedRange.Rows.Count
        If shtTasks.Range("A" & lManager) <> "" Then 'to prevent blank emails being created on lines with no manager
        Set xMailItem = xOutApp.CreateItem(0)
        With xMailItem
            .To = shtTasks.Range("C" & lManager) 'This is the cell with the email address in it
            .Subject = "TEST"
            .Body = "Hi," & chr(13) & chr(13) & "File is now updated." & chr(13) & "Your tasks are: "

            'Loop through Tasks:
            lTask = lManager
            Do Until shtTasks.Range("B" & lTask) = ""
                .Body = .Body & " " & shtTasks.Range("B" & lTask) & ", "
                lTask = lTask + 1
            Loop
            .Body = Left(.Body, Len(.Body)-2) & "." 'replace the last comma with a full stop

            .Attachments.Add xName
            .Display
            '.Send
            Set xMailItem = Nothing
            lManager = lTask + 1 'Move to next manager
        End With
        End If
    Loop
    Set xOutApp = Nothing
End Sub

相关问题