Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.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
Vba 发送包含多个RangeToHTML范围的电子邮件_Vba_Excel_Email - Fatal编程技术网

Vba 发送包含多个RangeToHTML范围的电子邮件

Vba 发送包含多个RangeToHTML范围的电子邮件,vba,excel,email,Vba,Excel,Email,我正在使用一些从Ron de Bruin的网站(真棒,顺便说一句)复制的代码,遇到了一个障碍 生成的电子邮件将仅将标题粘贴到completedTasks范围 它将正确地将摘要和未完成任务范围粘贴到电子邮件正文 如果我删除了所有处理未完成任务的代码,那么它将正确地将摘要和已完成任务HTML粘贴到电子邮件正文中 提前感谢您的帮助 Function RangetoHTML(rng As Range) ' Changed by Ron de Bruin 28-Oct-2006 ' Working in

我正在使用一些从Ron de Bruin的网站(真棒,顺便说一句)复制的代码,遇到了一个障碍

生成的电子邮件将仅将标题粘贴到
completedTasks
范围

它将正确地将
摘要
未完成任务
范围粘贴到电子邮件正文

如果我删除了所有处理
未完成任务的代码
,那么它将正确地将
摘要
已完成任务
HTML粘贴到电子邮件正文中

提前感谢您的帮助

Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2016
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook

    TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

    'Copy the range and create a new workbook to past the data in
    rng.Copy
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With

    'Publish the sheet to a htm file
    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         Filename:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         Source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With

    'Read all data from the htm file into RangetoHTML
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    RangetoHTML = ts.readall
    ts.Close
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")

    'Close TempWB
    TempWB.Close savechanges:=False

    'Delete the htm file we used in this function
    Kill TempFile

    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function


Sub Monthly_Close_Daily_Report()
'
'

Dim yearMonth As String
Dim closeDay As String
Dim currTime As String
Dim summaryRange As Range
Dim completedTasks As Range
Dim incompleteTasks As Range
Dim emailRng As Range, cl As Range
Dim sTo As String

Application.ScreenUpdating = False
Sheets("Inputs").Select

'Check to make sure there are no errors, then proceed
If Not IsError(Sheets("Inputs").Range("B12")) Then
    If Sheets("Inputs").Range("B12") = "Yes" Then
        'Store the YY-MM as a variable
        Sheets("Inputs").Select
        yearMonth = Range("B4").Value

        'Store the MM/DD/YYYY as a variable
        Sheets("Inputs").Select
        closeDay = Range("B5").Value

        'Store the current time as a variable
        Sheets("Inputs").Select
        currTime = Format(Now(), "h:mmAM/PM")

        'Unfilter the Task Listing tab
        Sheets("Task Listing").Select
        Activesheet.ShowAllData

        'Refresh the table with new Sharepoint data
        ActiveWorkbook.Connections("SharePoint").Refresh

            'Create a new email with the Email Listing tab in the "To" line
            Set OutApp = CreateObject("Outlook.Application")
            Set OutMail = OutApp.CreateItem(0)

            'Determine the email addresses to send to
            Set emailRng = Worksheets("Email Listing").Range("B2:B50")
            For Each cl In emailRng
                sTo = sTo & ";" & cl.Value
            Next
            sTo = Mid(sTo, 2)

            'Set the Summary range to be copied into the email
            Set summaryRange = Sheets("Summary").Range("A1:G11")
            summaryRange.Copy

            'Filter the Task Listing tab for this month's completed tasks & copy to range
            Sheets("Task Listing").Select
            ActiveSheet.ListObjects("Close_Tasks").Range.AutoFilter Field:=1, Criteria1:=yearMonth, Operator:=xlFilterValues
            ActiveSheet.ListObjects("Close_Tasks").Range.AutoFilter Field:=6, Criteria1 _
                :="Completed"
            Set completedTasks = Application.Intersect(ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible), Range("A:G"))
            'Set completedTasks = Sheets("Task Listing").UsedRange.SpecialCells(xlCellTypeVisible)
            Worksheets("Task Listing").ShowAllData

            'Filter the Task Listing tab for this month's non-completed tasks & copy to range
            ActiveSheet.ListObjects("Close_Tasks").Range.AutoFilter Field:=1, Criteria1:=yearMonth, Operator:=xlFilterValues
            ActiveSheet.ListObjects("Close_Tasks").Range.AutoFilter Field:=6, Criteria1:="<>Completed"
            Set incompleteTasks = Application.Intersect(ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible), Range("A:G"))

            'On Error Resume Next
            With OutMail
                .To = sTo
                .CC = ""
                .BCC = ""
                .Subject = "Month End Close Status for " & yearMonth & " As Of " & currTime & " on " & closeDay
                .HTMLBody = RangetoHTML(summaryRange) & "<br><br><strong>Completed Tasks" & RangetoHTML(completedTasks) & "<br><br><strong>Incomplete Tasks" & RangetoHTML(incompleteTasks)
                .Display 'Can also use .Send which will send the email.  We want to preview before sending, though.
            End With

            Set OutMail = Nothing
            Set OutApp = Nothing

    Else
        'If tasks are missing Due Dates, flag those for the user and exit the macro
        MsgBox ("There are ""Due Dates"" missing for some tasks.  Please correct the issue and run the macro again.")
    End If

End If

    'Filter the "Task Listing" tab for the current month
    Sheets("Task Listing").Select
    Range("A2").Select
    Selection.AutoFilter
    ActiveSheet.ListObjects("Close_Tasks").Range.AutoFilter Field:=1, Criteria1:=yearMonth, Operator:=xlFilterValues

'

End Sub
函数范围HTML(rng作为范围)
“由Ron de Bruin于2006年10月28日更改
“2000-2016年在办公室工作
作为对象的Dim fso
将T作为对象
将文件设置为字符串
将TempWB设置为工作簿
TempFile=Environ$(“temp”)和“\”格式(现在是“dd-mm-yy h-mm-ss”)和“.htm”
'复制范围并创建一个新工作簿,以超过中的数据
收到
Set TempWB=工作簿。添加(1)
带临时工作表(1)
.单元格(1).粘贴特殊粘贴:=8
.单元格(1).粘贴特殊值,False,False
.单元格(1).粘贴特殊xlPasteFormats,False,False
.单元格(1)。选择
Application.CutCopyMode=False
出错时继续下一步
.DrawingObjects.Visible=True
.DrawingObjects.Delete
错误转到0
以
'将工作表发布到htm文件
使用TempWB.PublishObjects.Add(_
SourceType:=xlSourceRange_
文件名:=临时文件_
工作表:=临时工作表(1).名称_
来源:=TempWB.Sheets(1).UsedRange.Address_
HtmlType:=xlHtmlStatic)
.发布(真实)
以
'将htm文件中的所有数据读入RangetoHTML
设置fso=CreateObject(“Scripting.FileSystemObject”)
设置ts=fso.GetFile(TempFile).OpenAsTextStream(1,-2)
RangetoHTML=ts.readall
关闭
RangetoHTML=Replace(RangetoHTML,“align=center x:publishsource=”_
“align=left x:publishsource=”)
“关闭TempWB
TempWB.Close savechanges:=False
'删除此函数中使用的htm文件
杀死临时文件
设置ts=无
设置fso=无
设置TempWB=Nothing
端函数
次月结日报表()
'
'
以字符串形式显示月份
朦胧如弦
把时间当作字符串
Dim SUMMARY Range As Range
将已完成的任务作为范围进行调整
将不完全任务设置为范围
变光范围为零,cl范围为零
将sTo设置为字符串
Application.ScreenUpdating=False
表格(“输入”)。选择
'检查以确保没有错误,然后继续
如果不是IsError(表格(“输入”)。范围(“B12”)),则
如果图纸(“输入”)。范围(“B12”)=是,则
'将YY-MM存储为变量
表格(“输入”)。选择
yearMonth=范围(“B4”).值
'将MM/DD/YYYY存储为变量
表格(“输入”)。选择
closeDay=范围(“B5”).值
'将当前时间存储为变量
表格(“输入”)。选择
currTime=格式(现在(),“h:mmAM/PM”)
'取消筛选任务列表选项卡
工作表(“任务列表”)。选择
Activesheet.ShowAllData
'使用新的Sharepoint数据刷新表
ActiveWorkbook.Connections(“SharePoint”)。刷新
'使用“收件人”行中的“电子邮件列表”选项卡创建新电子邮件
Set-OutApp=CreateObject(“Outlook.Application”)
Set-OutMail=OutApp.CreateItem(0)
'确定要发送到的电子邮件地址
设置emailRng=工作表(“电子邮件列表”)。范围(“B2:B50”)
对于emailRng中的每个cl
sTo=sTo&“;”和cl.值
下一个
sTo=Mid(sTo,2)
'设置要复制到电子邮件中的摘要范围
集合汇总范围=表格(“汇总”)。范围(“A1:G11”)
summaryRange,收到
'筛选本月已完成任务的任务列表选项卡并复制到范围
工作表(“任务列表”)。选择
ActiveSheet.ListObjects(“关闭_任务”).Range.AutoFilter字段:=1,Criteria1:=yearMonth,运算符:=xlFilterValues
ActiveSheet.ListObjects(“关闭任务”).Range.AutoFilter字段:=6,标准1_
:=“已完成”
Set completedTasks=Application.Intersect(ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible),范围(“A:G”))
'Set completedTasks=Sheets(“任务列表”).UsedRange.SpecialCells(xlCellTypeVisible)
工作表(“任务列表”).ShowAllData
'筛选本月未完成任务的任务列表选项卡并复制到范围
ActiveSheet.ListObjects(“关闭_任务”).Range.AutoFilter字段:=1,Criteria1:=yearMonth,运算符:=xlFilterValues
ActiveSheet.ListObjects(“关闭任务”).Range.AutoFilter字段:=6,Criteria1:=“已完成”
Set incompleteTasks=Application.Intersect(ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible),范围(“A:G”))
'出现错误时,请继续下一步
发邮件
.To=sTo
.CC=“”
.BCC=“”
.Subject=“截止日期和当前时间和关闭日期的月底关闭状态”
.HTMLBody=RangetoHTML(汇总范围)和RangetoHTML(已完成任务)和RangetoHTML(已完成任务)和RangetoHTML(未完成任务)
.Display'也可以使用.Send发送电子邮件。不过,我们希望在发送前预览。
以
发送邮件=无
设置应用程序=无
其他的
'如果任务缺少到期日期,请标记用户的到期日期并退出宏
MsgBox(“某些任务缺少“截止日期”。请更正此问题,然后再次运行宏。”)
如果结束
如果结束
'筛选当月的“任务列表”选项卡
工作表(“任务列表”)。选择
范围(“A2”)。选择
自动筛选
ListObjects(“Clo
Sub Monthly_Close_Daily_Report()
'
'
Dim yearMonth As String
Dim closeDay As String
Dim currTime As String
Dim summaryRange As Range
Dim completedTasks As Range
Dim incompleteTasks As Range
Dim placeholderRange As Range
Dim emailRng As Range, cl As Range
Dim lastRow As Long, x As Long
Dim sTo As String

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Sheets("Inputs").Select

'Check to make sure there are no errors, then proceed
If Not IsError(Sheets("Inputs").Range("B12")) Then
    If Sheets("Inputs").Range("B12") = "Yes" Then
        'Store the YY-MM as a variable
        Sheets("Inputs").Select
        yearMonth = Range("B4").Value

        'Store the MM/DD/YYYY as a variable
        Sheets("Inputs").Select
        closeDay = Range("B5").Value

        'Store the current time as a variable
        Sheets("Inputs").Select
        currTime = Format(Now(), "h:mmAM/PM")

        'Unfilter the Task Listing tab
        Sheets("Task Listing").Select
        Range("A1").Select
        Selection.AutoFilter

        'Refresh the table with new Sharepoint data
        ActiveWorkbook.Connections("SharePoint").Refresh

            'Create a new email with the Email Listing tab in the "To" line, and Alan and Tim cc'd
            Set OutApp = CreateObject("Outlook.Application")
            Set OutMail = OutApp.CreateItem(0)

            'Determine the email addresses to send to
            Set emailRng = Worksheets("Email Listing").Range("B2:B50")
            For Each cl In emailRng
                sTo = sTo & ";" & cl.Value
            Next
            sTo = Mid(sTo, 2)

            'Set the Summary range to be copied into the email
            Set summaryRange = Sheets("Summary").Range("A1:G11")
            summaryRange.Copy

            'Filter the table for "Completed" and then add it to the placeholder tab to be converted to HTML
            Sheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Placeholder"
            Range("A1").Select
            ActiveCell.FormulaR1C1 = "Completed Tasks"
            With Selection.Font
                .Name = "Arial"
                .Size = 18
                .ThemeColor = xlThemeColorLight1
            End With
            Selection.Font.Bold = True
            Sheets("Task Listing").Select
            ActiveSheet.ListObjects("Close_Tasks").Range.AutoFilter Field:=1, Criteria1:=yearMonth, Operator:=xlFilterValues
            ActiveSheet.ListObjects("Close_Tasks").Range.AutoFilter Field:=6, Criteria1 _
                :="Completed"
            ActiveSheet.UsedRange.Select
            Selection.SpecialCells(xlCellTypeVisible).Select
            Selection.Copy
            Sheets("Placeholder").Select
            Range("A3").Select
            ActiveSheet.Paste

            'Find the last row of the "Placeholder" sheet
            lastRow = Cells(Rows.Count, 1).End(xlUp).Row

            'Copy the format to the "Incomplete" section header
            Range("A1").Select
            Selection.Copy
            Range("A" & lastRow + 3).Select
            ActiveSheet.Paste
            ActiveCell.FormulaR1C1 = "Incomplete Tasks"

            'Filter the table for "Incomplete" and then add it to the placeholder tab to be converted to HTML
            Sheets("Task Listing").Select
            ActiveSheet.ListObjects("Close_Tasks").Range.AutoFilter Field:=6, Criteria1 _
                :="=In Progress", Operator:=xlOr, Criteria2:="=Not Started"
            ActiveSheet.UsedRange.Select
            Selection.SpecialCells(xlCellTypeVisible).Select
            Selection.Copy
            Sheets("Placeholder").Select

            'Find the new last row of the "Placeholder" tab
            lastRow = Cells(Rows.Count, 1).End(xlUp).Row

            'Paste the incomplete tasks to the "Placeholder" tab
            Range("A" & lastRow + 1).Select
            ActiveSheet.Paste

            'Format the "Placeholder" tab
            Cells.Select
            With Selection
                .WrapText = False
                .Orientation = 0
                .AddIndent = False
                .IndentLevel = 0
                .ShrinkToFit = False
                .ReadingOrder = xlContext
                .MergeCells = False
            End With
            Cells.EntireColumn.AutoFit

            'Find the new last row of the "Placeholder" tab
            lastRow = Cells(Rows.Count, 1).End(xlUp).Row

            'Make the entire "Placeholder" sheet the placeholderRange
            Set placeholderRange = Range("A1:G" & lastRow)

            'On Error Resume Next
            With OutMail
                .To = sTo
                .CC = ""
                .BCC = ""
                .Subject = "Month End Close Status for " & yearMonth & " As Of " & currTime & " on " & closeDay
                '.HTMLBody = RangetoHTML(summaryRange) & "<br><br><strong>Completed Tasks" & RangetoHTML(completedTasks) & "<br><br><strong>Incomplete Tasks" & RangetoHTML(incompleteTasks)
                .HTMLBody = RangetoHTML(summaryRange) & "<br><br>" & RangetoHTML(placeholderRange)
                .Display 'Can also use .Send which will send the email.  We want to preview before sending, though.
            End With

            Set OutMail = Nothing
            Set OutApp = Nothing

    Else
        'If tasks are missing Due Dates, flag those for the user and exit the macro
        MsgBox ("There are ""Due Dates"" missing for some tasks.  Please correct the issue and run the macro again.")
    End If

End If

    'Delete the Placeholder tab
    Sheets("Placeholder").Delete

    'Filter the "Task Listing" tab for the current month
    Sheets("Task Listing").Select
    Range("A2").Select
    Selection.AutoFilter
    ActiveSheet.ListObjects("Close_Tasks").Range.AutoFilter Field:=1, Criteria1:=yearMonth, Operator:=xlFilterValues

Application.ScreenUpdating = True
Application.DisplayAlerts = True

'
End Sub