【问题标题】:How to insert a table after body of e-mail and before signature?如何在电子邮件正文之后和签名之前插入表格?
【发布时间】:2019-06-19 14:48:20
【问题描述】:

我正在使用以下代码将表格从 excel 粘贴到 Outlook 文件。但是,现在表格粘贴在电子邮件的最底部 - 在签名之后。

我想要实现的是在“区域”一词之后插入表格。在“问候”之前 - 所以在签名之前。

Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Sourcewb As Workbook
Dim Destwb As Workbook
Dim sh As Worksheet
Dim DateString As String
Dim FolderName As String
Dim myOutlook As Object
Dim myMailItem As Object
Dim mySubject As String
Dim myPath As String
Dim i As Integer
With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .Calculation = xlCalculationManual
End With

'Prompt for Email Subject

Set outlApp = CreateObject("Outlook.Application")
weeknumber = "Week " & WorksheetFunction.WeekNum(Now, vbMonday)
'mySubject = InputBox("Subject for Email")
For i = 2 To 3
region = Sheets("Sheet1").Cells(i, 5).Value
mySubject = "Overdue Milestones | " & weeknumber & " | " & region

'Copy every sheet from the workbook with this macro
Set Sourcewb = ActiveWorkbook
'Create new folder to save the new files in
DateString = Format(Now, "yyyy-mm-dd hh-mm-ss")
FolderName = "C:\Users\mxr0520\Desktop\Ignite Reports\Milestones\" & weeknumber
If i < 3 Then
MkDir FolderName
Else
End If
'Copy every visible sheet to a new workbook
Set sh = Sheets(region)
    'If the sheet is visible then copy it to a new workbook
    If sh.Visible = -1 Then
        sh.Copy
        'Set Destwb to the new workbook
        Set Destwb = ActiveWorkbook
        'Determine the Excel version and file extension/format
        With Destwb
            If Val(Application.Version) < 12 Then
                'You use Excel 97-2003
                FileExtStr = ".xls": FileFormatNum = -4143
            Else
                'You use Excel 2007-2016
                If Sourcewb.Name = .Name Then
                    MsgBox "Your answer is NO in the security dialog"
                    GoTo GoToNextSheet
                Else
                    FileExtStr = ".xlsx": FileFormatNum = 51
                End If
            End If
        End With
        'Change all cells in the worksheet to values if you want
        If Destwb.Sheets(1).ProtectContents = False Then
            With Destwb.Sheets(1).UsedRange
                .Cells.Copy
                .Cells.PasteSpecial xlPasteValues
                .Cells(1).Select
            End With
            Application.CutCopyMode = False
        End If
        'Save the new workbook, email it, and close it
        'Set otlNewMail = outlApp.CreateItem(myMailItem)

        Set OutLookApp = CreateObject("Outlook.application")
        Set OutlookMailitem = OutLookApp.CreateItem(0)
            With OutlookMailitem
            .display
            End With
            Signature = OutlookMailitem.htmlbody

        With Destwb
            .SaveAs FolderName _
                  & "\" & Destwb.Sheets(1).Name & FileExtStr, _
                    FileFormat:=FileFormatNum
        End With
        myPath = ActiveWorkbook.path & "\" & ActiveWorkbook.Name
        With Destwb
            .Close False
        End With

        With OutlookMailitem
            .Subject = mySubject
            .To = Sheets("Sheet1").Cells(i, 6)
            .CC = Sheets("Sheet1").Cells(i, 7)
            .htmlbody = "Dear All," & "<br>" _
            & "<br>" _
            & "Attached please find the list of milestones that are <b>overdue</b> and <b>due in 14 days</b> for " & region & "." & "<br>" & "<br>" & "Regards," & "<br>" _
            & "Marek" _
            & Signature
            .Attachments.Add myPath

    Worksheets("Summary").Range("A1:E14").Copy
    Set vInspector = OutlookMailitem.GetInspector
    Set weditor = vInspector.WordEditor

    wEditor.Application.Selection.Start = Len(.body)
    wEditor.Application.Selection.End = wEditor.Application.Selection.Start
    wEditor.Application.Selection.Paste

            .display

        End With
        Set OutlookMailitem = Nothing
    End If

提前感谢您的帮助!

【问题讨论】:

  • Len(.body) 返回什么?在黑暗中拍摄,也许尝试制作 Len(.body) - 1Len(.body) - 2
  • 您有机会尝试以下解决方案吗?
  • 非常感谢您的建议。我试过了,但是在声明变量时它在一开始就被阻塞了:“编译错误:未定义用户定义的类型”。对于第一行(“Dim objOutlook as outlook.Application”)。
  • 好的,我必须调整设置。一切都很好 - 效果很棒。谢谢大卫!

标签: excel vba outlook


【解决方案1】:

通过创建带有邮件正文和“区域”占位符和表格的 .oft(Outlook 电子邮件模板)可能最容易做到这一点。创建没有签名的模板,稍后将根据您的 Outlook 用户设置自动添加。我创建一个这样的模板,并保存为.oft:

然后只需使用Set OutlookMailitem = OutlookApp.CreateItemFromTemplate({path to your template.oft}) 创建新的邮件项,替换“区域”占位符,然后将表格复制/粘贴到表格占位符的位置。

Option Explicit

Sub foo()

Dim objOutlook As Outlook.Application
Dim objMsg As Outlook.MailItem
Dim wdDoc As Word.Document
Dim tblRange As Word.Range
Dim region As String
' define your Region, probably this is done in a loop...
region = "Region 1"
Set objOutlook = CreateObject("Outlook.Application")
' Create email from the template file // UPDATE WITH YOUR TEMPLATE PATH
Set objMsg = objOutlook.CreateItemFromTemplate("C:\path\to\your\template.oft")
objMsg.Display
Set wdDoc = objOutlook.ActiveInspector.WordEditor
' replace placeholder with region:
wdDoc.Range.Find.Execute "{{REGION PLACEHOLDER}}", ReplaceWith:=region
' in my template, paragraph 5 is the table placeholder, modify as needed:
Set tblRange = wdDoc.Range.Paragraphs(5).Range
tblRange.Text = ""  ' remove the placeholder text
' copy the Excel table // modify to refer to your correct table/range
Sheet1.ListObjects(1).Range.Copy
' paste the table into the email
tblRange.PasteExcelTable False, False, False

End Sub

如您所见,最终电子邮件包含我的默认签名(它不是 template.oft 文件的一部分)。

【讨论】:

    【解决方案2】:

    您可以使用以下属性来自定义消息正文:

    1. Body - 代表 Outlook 项目的明文正文的字符串。

    2. HTMLBody - 表示指定项的 HTML 正文的字符串。

    3. 文字编辑器。 Inspector 类的 WordEditor 属性返回代表消息正文的 Word 文档实例。您可以在Chapter 17: Working with Item Bodies in MSDN 中找到所有这些方法。

    Outlook 对象模型不提供任何用于检测签名的属性或方法。您解析消息正文并尝试找到这些地方。

    但是,当您在 Outlook 中创建签名时,会在以下文件夹中创建三个文件(HTM、TXT 和 RTF):

    Vista 和 Windows 7/8/10

     C:\Users\<UserName>\AppData\Roaming\Microsoft\Signatures
    

    Windows XP

    C:\Documents and Settings\<UserName>\Application Data\Microsoft\Signatures
    

    Application DataAppData 是隐藏文件夹,如果您想查看文件,请更改 Windows 资源管理器中的视图,以便显示隐藏的文件和文件夹。

    所以,您阅读了这些文件的内容,并尝试在邮件正文中找到相应的内容。请注意,用户可以在电子邮件末尾键入自定义签名。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-11
      • 2014-10-16
      相关资源
      最近更新 更多