Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.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工作表附加到Outlook电子邮件?_Excel_Vba_Outlook - Fatal编程技术网

如何将Excel工作表附加到Outlook电子邮件?

如何将Excel工作表附加到Outlook电子邮件?,excel,vba,outlook,Excel,Vba,Outlook,我正在尝试修复一个问题,即附加一个文件 我有一个包含人员及其姓名列表和条件(Y/N)列的表 附页1的代码(不起作用) file\u name\u import=Format(现在是“yyyy-mm-dd-hh-mm-ss”) file\u name\u import=file\u name\u import&“-file 1.xlsx” 工作表(“第1页”)。副本 ChDir“H:\Folder 1\Folder 2\Folder 3\Folder 4\” ActiveWorkbook.Save

我正在尝试修复一个问题,即附加一个文件

我有一个包含人员及其姓名列表和条件(Y/N)列的表

附页1的代码(不起作用)

file\u name\u import=Format(现在是“yyyy-mm-dd-hh-mm-ss”)
file\u name\u import=file\u name\u import&“-file 1.xlsx”
工作表(“第1页”)。副本
ChDir“H:\Folder 1\Folder 2\Folder 3\Folder 4\”
ActiveWorkbook.SaveAs文件名:=_
“H:\Folder 1\Folder 2\Folder 3\Folder 4\File 1”&文件名\u导入_
FileFormat:=xlOpenXMLWorkbook,CreateBackup:=False
.Attachments.Add“H:\Folder 1\Folder 2\Folder 3\Folder 4\File 1\”&文件名\u导入

我想添加代码,这样我的电子邮件就会弹出(所有需要的人都在“收件人”和附件中)。

这里的想法是将工作表复制到一个新文件中,并将其保存在临时文件夹中。然后将其附加到您的电子邮件中

Option Explicit

Public Sub SendEmail()
    ' For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
    ' Working in Office 2000-2016
    ' Attachment code based on: http://www.vbaexpress.com/kb/getarticle.php?kb_id=326
    ' Adapted by Ricardo Diaz ricardodiaz.co
    Dim OutApp As Object
    Dim OutMail As Object
    Dim sourceTable As ListObject
    Dim evalRow As ListRow

    Dim newBook As Workbook
    Dim newBookName As String

    Dim counter As Long
    Dim toArray() As Variant

    Application.ScreenUpdating = False

    Set OutApp = CreateObject("Outlook.Application")

    Set sourceTable = Range("Table1").ListObject

    On Error GoTo Cleanup

    ' Save current file to temp folder (delete first if exists)
    ThisWorkbook.Worksheets("Sheet1").Copy
    Set newBook = ActiveWorkbook
    newBookName = "AttachedSheet.xlsx"
    On Error Resume Next
    Kill Environ("temp") & newBookName
    On Error GoTo 0
    Application.DisplayAlerts = False
    newBook.SaveAs Environ("temp") & newBookName
    Application.DisplayAlerts = True

    ' Loop through each table's rows
    For Each evalRow In sourceTable.ListRows

        If evalRow.Range.Cells(, 2).Value Like "?*@?*.?*" And LCase(evalRow.Range.Cells(, 3).Value) = "yes" Then
            ReDim Preserve toArray(counter)
            toArray(counter) = evalRow.Range.Cells(, 2).Value
            counter = counter + 1
        End If

    Next evalRow

    ' Setup the email
    Set OutMail = OutApp.CreateItem(0)

    With OutMail
        ' Add gathered recipients
        For counter = 0 To UBound(toArray)
            .Recipients.Add (toArray(counter))
        Next counter

        .Subject = "Reminder"

        .Body = "Dear All" _
                & vbNewLine & vbNewLine & _
                "Please contact us to discuss bringing " & _
                "your account up to date"

        'You can add files also like this
        .Attachments.Add newBook.FullName ' -> Adjust this path

        .Display ' -> Or use Display
    End With

    Set OutMail = Nothing

Cleanup:
    Set OutApp = Nothing
    Application.ScreenUpdating = True
End Sub

让我知道如果它工作

代码>选项显式“考虑这个强制” “工具|选项|编辑器”选项卡 '需要变量声明 公共子附件FileToEmail() Dim OutApp作为对象 将邮件变暗为对象 将sourceTable设置为ListObject Dim evalRow作为列表行 昏暗的柜台一样长 Dim toArray()作为变量 作为字符串的Dim strDir Dim文件\u名称\u导入为字符串 作为字符串的Dim fName Application.ScreenUpdating=False Set-OutApp=CreateObject(“Outlook.Application”) '未重新创建Excel详细信息,此问题不需要 文件名导入=格式(现在为“yyyy-mm-dd-hh-mm-ss”) file\u name\u import=file\u name\u import&“-file 1.xlsx” '由于错误处理不当,将绕过下标超出范围错误 '工作表(“第1页”)。副本 工作表(“表1”)。副本 '由于错误处理不善,将绕过尾部反斜杠错误 'ChDir“H:\Folder 1\Folder 2\Folder 3\Folder 4\” strDir=“C:\Folder 1\Folder 2\Folder 3\Folder 4\” 调试。打印strDir '反斜杠已在strDir的末尾 fName=strDir&“文件1”&文件名\导入 调试。打印fName ActiveWorkbook.SaveAs文件名:=fName,文件格式:=xlOpenXMLWorkbook,CreateBackup:=False '设置电子邮件 Set-OutMail=OutApp.CreateItem(0) '如果没有绕过错误的特定原因,请不要使用错误恢复下一步 '而是在您可以看到错误后立即修复错误 发邮件 '未重新创建Excel详细信息,此问题不需要 .Subject=“提醒” .Body=“亲爱的所有人”_ &vbNewLine&vbNewLine&_ “请遵守随附文件中的转让。”_ “查找您的店铺并尽快处理。” .Attachments.Add fName .展示 以 发送邮件=无 清理: 设置应用程序=无 Application.ScreenUpdating=True 端接头
您好,我正在尝试解决一个问题,即附加文件。我还添加了我尝试过的代码。我只是想解释一下我打算做的事情。看到错误可以调试代码。删除错误时的
转到清除
和错误时的
继续下一步
。将“附加工作表1的代码”放入工作代码中,并修复您现在看到的错误。编辑您的帖子,描述您无法修复的错误。你好,李嘉图,非常感谢!此操作用于生成带有附加文件的outlook弹出窗口。我看到它只会在第3栏中添加“是”的人。但“表1”中的D列列出了未遵守各项任务的人员名单。现在使用此列,我想将第3列中的条件更改为“是”,以便在“表1”的D列中找到所有唯一的名称。请注意,每次运行宏1时都会生成“工作表1”。我正在考虑做一个if错误(vlookup…)。你认为这样行吗?这是最好的办法吗?你有什么建议吗?嗨,罗伊,如果你用一个新的问题来描述情况,张贴你已经有的代码,那就更好了。如果您愿意,请在此处复制注释中的链接,我将对此进行查看。Hi Ricardo Its给我的错误是对象_Global的“Method‘Range’”失败:Set sourceTable=Range(“表6”)ListObjectHi Ricardo我发布了一个新问题。这里是链接:谢谢你的帮助!
Option Explicit

Public Sub SendEmail()
    ' For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
    ' Working in Office 2000-2016
    ' Attachment code based on: http://www.vbaexpress.com/kb/getarticle.php?kb_id=326
    ' Adapted by Ricardo Diaz ricardodiaz.co
    Dim OutApp As Object
    Dim OutMail As Object
    Dim sourceTable As ListObject
    Dim evalRow As ListRow

    Dim newBook As Workbook
    Dim newBookName As String

    Dim counter As Long
    Dim toArray() As Variant

    Application.ScreenUpdating = False

    Set OutApp = CreateObject("Outlook.Application")

    Set sourceTable = Range("Table1").ListObject

    On Error GoTo Cleanup

    ' Save current file to temp folder (delete first if exists)
    ThisWorkbook.Worksheets("Sheet1").Copy
    Set newBook = ActiveWorkbook
    newBookName = "AttachedSheet.xlsx"
    On Error Resume Next
    Kill Environ("temp") & newBookName
    On Error GoTo 0
    Application.DisplayAlerts = False
    newBook.SaveAs Environ("temp") & newBookName
    Application.DisplayAlerts = True

    ' Loop through each table's rows
    For Each evalRow In sourceTable.ListRows

        If evalRow.Range.Cells(, 2).Value Like "?*@?*.?*" And LCase(evalRow.Range.Cells(, 3).Value) = "yes" Then
            ReDim Preserve toArray(counter)
            toArray(counter) = evalRow.Range.Cells(, 2).Value
            counter = counter + 1
        End If

    Next evalRow

    ' Setup the email
    Set OutMail = OutApp.CreateItem(0)

    With OutMail
        ' Add gathered recipients
        For counter = 0 To UBound(toArray)
            .Recipients.Add (toArray(counter))
        Next counter

        .Subject = "Reminder"

        .Body = "Dear All" _
                & vbNewLine & vbNewLine & _
                "Please contact us to discuss bringing " & _
                "your account up to date"

        'You can add files also like this
        .Attachments.Add newBook.FullName ' -> Adjust this path

        .Display ' -> Or use Display
    End With

    Set OutMail = Nothing

Cleanup:
    Set OutApp = Nothing
    Application.ScreenUpdating = True
End Sub