将多个工作表保存到不同的工作簿,并使用VBA将这些工作表附加到Outlook

时间:2013-03-15 12:17:02

标签: excel excel-vba outlook outlook-vba vba

我需要一个宏来完成以下工作:

我有一张6张工作簿。每个都有不同的名称。表格名称为ENT,GHD,T3等。

我想复制每张表的内容并将其保存到具有不同名称的不同工作簿以及日期n时间戳。

例如,Sheet ENT必须保存为XYZ Report DATE TIME。 Xlsx和这个文件需要附加到outlook。

同样对于GHD,文件名应为ABC Report DATE TIME。 XLSX。 同样适用于T3纸张和其余3张纸。所有这些文件都将邮寄给不同的人。

这些新文件的捕获必须保存到创建它的原始文件的文件夹中。

2 个答案:

答案 0 :(得分:0)

所以你想循环遍历下面的工作表:

for Each xSheet In activeworkbook.worksheets

然后使用工作表名称和工作簿路径

调用saveas方法
xsheet.saveas activeworkbook.path & xSheet.Name & " Report " & vba.forma(now, "dd mmm yyyy hhmmss"), xlnormal

然后你想创建一个Outlook邮件项目,设置收件人,主题和附件,然后发送它......

所以应该让你开始,然后你只需在参考中添加对Outlook应用程序的引用,使你能够使用这些对象......

HTH

菲利普

答案 1 :(得分:-1)

Sub COMEON()
  Dim onePublishObject As PublishObject
  Dim oneSheet As Worksheet
  Dim scriptingObject As Object
  Dim outlookApplication As Object
  Dim outlookMail As Object
  Dim htmlBody As String
  Dim htmlFile As String
  Dim textStream, fil As String
  Dim dummy As Workbook


  Today = Format(Now(), "dd-mm-yyyy")

  Set dummy = ActiveWorkbook
  Set scriptingObject = CreateObject("Scripting.FileSystemObject")
  Set outlookApplication = CreateObject("Outlook.Application")
  For Each oneSheet In ActiveWorkbook.Worksheets




                                                        Dim StrBody As String
                                                        StrBody = " THIS IS A TEST" & " " & UCase(oneSheet.Name) & " " & "XYZ," & vbNewLine & _
                                                        vbNewLine & _
                                                        "Please FIND ATTACHED <B>'XYZ REPORT'<B>"





    Application.DisplayAlerts = False
    Sheets(oneSheet.Name).Copy
    ActiveWorkbook.SaveAs dummy.Path & "\" & oneSheet.Name & ".xlsx"
    ActiveWorkbook.Close
    Application.DisplayAlerts = True
    Set outlookMail = outlookApplication.CreateItem(0)
    With outlookMail
        .htmlBody = StrBody & htmlBody
        .attachments.Add dummy.Path & "\" & oneSheet.Name & ".xlsx"
        .Display
        .Subject = "XYZ Report" & " " & UCase(oneSheet.Name) & " " & "(" & Today & ")"

    End With
  Next oneSheet
End Sub