通过Excel将一个单元格值添加到电子邮件正文中

时间:2019-03-07 19:12:30

标签: excel vba

我有一个宏,该宏会自动创建电子邮件,将工作簿中的两张工作表作为附件附加,然后将这些工作表通过电子邮件发送给批准的人员。我想弄清楚如何从“说明”表中获取“ C3”中的单元格值到它创建的电子邮件正文中。我已经尝试了几种不同的程序,但还没有找到方法。

Sub Labor_Material_16009()
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Sourcewb As Workbook
Dim Destwb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim OutApp As Object
Dim OutMail As Object

With Application
    .ScreenUpdating = False
    .EnableEvents = False
End With

Set Sourcewb = ActiveWorkbook

With Sourcewb
    Set theactivewindow = ActiveWindow
    Set tempwindow = .NewWindow
    .Sheets(Array("16009 Labor", "16009 Material")).Copy
End With

tempwindow.Close

Set Destwb = ActiveWorkbook

With Destwb
    If Val(Application.Version) < 12 Then
        'You use Excel 97-2003
        FileExtStr = ".xls": FileFormatNum = -4143
    Else
        'You use Excel 2007-2016
        Select Case Sourcewb.FileFormat
        Case 51: FileExtStr = ".xlsm": FileFormatNum = 52
        Case 52:
            If .HasVBProject Then
                FileExtStr = ".xlsm": FileFormatNum = 52
            Else
                FileExtStr = ".xlsm": FileFormatNum = 52
            End If
        Case 56: FileExtStr = ".xlsm": FileFormatNum = 52
        Case Else: FileExtStr = ".xlsm": FileFormatNum = 52
        End Select
    End If
End With

With Destwb.Sheets(1).UsedRange
.Cells.Copy
.Cells.PasteSpecial xlPasteValues
.Cells(1).Select
End With
Application.CutCopyMode = False

TempFilePath = Environ$("temp") & "\"
TempFileName = "16009 - " & Sourcewb.Name & " " & Format(Now, "dd-mmm-yy")

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

With Destwb
    .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
    On Error Resume Next
    With OutMail
        .To = ""
        .CC = ""
        .BCC = ""
        .Subject = "16009 Labor and Material Report"
        .Body = "Please see the attached"
        .Attachments.Add Destwb.FullName
        'You can add other files also like this
        '.Attachments.Add ("C:\test.txt")
        .Send   'or use .Display
    End With
    On Error GoTo 0
    .Close savechanges:=False
End With

Kill TempFilePath & TempFileName & FileExtStr

Set OutMail = Nothing
Set OutApp = Nothing

With Application
    .ScreenUpdating = True
    .EnableEvents = True
End With
End Sub

2 个答案:

答案 0 :(得分:1)

如果您想保留标准签名:

With OutMail
    .To = ""
    .CC = ""
    .BCC = ""
    .Subject = "16009 Labor and Material Report"
    '.Body = NOT HERE
    .Attachments.Add Destwb.FullName

    Dim wdDoc As Word.Document
    Set wdDoc = .GetInspector.WordEditor
    If Not wdDoc Is Nothing Then
        With wdDoc.Range
            .Collapse wdCollapseStart
            .InsertBefore "Please see the attached" & vbCrLf
            .Collapse wdCollapseEnd
            .InsertAfter "Best wishes," & vbCrLf
            .Collapse wdCollapseStart
            Sourcewb.Worksheets("Instructions").Range("C3").Copy
            .Paste
            '.PasteAndFormat wdChartPicture
            '.PasteAndFormat wdFormatPlainText
        End With
    End If
    .Send   'or use .Display
End With

答案 1 :(得分:0)

我将避免嵌套WITH语句。您可以在关闭字符串变量之前将任意范围的值放入字符串变量,然后在正文中使用它。我也将避免像电子邮件代码一样隐藏错误。处理错误或使用错误处理程序,但不要只是跳过它们。

请尝试:

Option Explicit

Sub Labor_Material_16009()
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim Sourcewb As Workbook
    Dim Destwb As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim OutApp As Object
    Dim OutMail As Object

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    Set Sourcewb = ActiveWorkbook

    Dim theactivewindow As Window
    Dim tempwindow As Window
    Dim InstructionText As String
    With Sourcewb
        InstructionText = .Worksheets("Instructions").Range("C3").Value

        Set theactivewindow = ActiveWindow
        Set tempwindow = .NewWindow
        .Sheets(Array("16009 Labor", "16009 Material")).Copy
    End With

    tempwindow.Close

    Set Destwb = ActiveWorkbook

    With Destwb
        If Val(Application.Version) < 12 Then
            'You use Excel 97-2003
            FileExtStr = ".xls": FileFormatNum = -4143
        Else
            'You use Excel 2007-2016
            Select Case Sourcewb.FileFormat
            Case 51: FileExtStr = ".xlsm": FileFormatNum = 52
            Case 52:
                If .HasVBProject Then
                    FileExtStr = ".xlsm": FileFormatNum = 52
                Else
                    FileExtStr = ".xlsm": FileFormatNum = 52
                End If
            Case 56: FileExtStr = ".xlsm": FileFormatNum = 52
            Case Else: FileExtStr = ".xlsm": FileFormatNum = 52
            End Select
        End If
    End With

    With Destwb.Sheets(1).UsedRange
        .Value = .Value
    End With

    TempFilePath = Environ$("temp") & "\"
    TempFileName = "16009 - " & Sourcewb.Name & " " & Format(Now, "dd-mmm-yy")
    Dim tempFullPath As String
    tempFullPath = TempFilePath & TempFileName & FileExtStr

    With Destwb
        .SaveAs tempFullPath, FileFormat:=FileFormatNum
        .Close savechanges:=False
    End With

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    With OutMail
        .To = ""
        .CC = ""
        .BCC = ""
        .Subject = "16009 Labor and Material Report"
        .Body = "Please see the attached" & vbNewLine & InstructionText
        .Attachments.Add tempFullPath
        'You can add other files also like this
        '.Attachments.Add ("C:\test.txt")
        .Send                                    'or use .Display
    End With

    Kill tempFullPath

    Set OutMail = Nothing
    Set OutApp = Nothing

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub