【问题标题】:Getting attachment from Outlook and placing in the body of a new Email after Formatting从 Outlook 获取附件并在格式化后放置在新电子邮件的正文中
【发布时间】:2015-09-09 04:07:57
【问题描述】:

这基本上就是我想要做的......

  1. 按主题名称搜索特定电子邮件

  2. 获取该电子邮件的附件(附件是原始数据的 excel 表)

  3. 从 Excel 附件上的另一个模块运行格式化子例程

  4. 将新格式的附件放在新电子邮件的正文中

  5. 将新邮件发送给客户

我需要关于步骤 3 和 4 的帮助。

Option Explicit
Sub sendEmail()

Dim olApp As Outlook.Application
Dim olEmail As Outlook.MailItem
Dim olNs As Namespace
Dim Fldr As MAPIFolder
Dim olMi As MailItem
Dim olAtt As Attachment
Dim MyPath As String
Dim i As Long

Set olApp = New Outlook.Application
Set olEmail = olApp.CreateItem(olMailItem)
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
MyPath = "C:\Users\(Me)\Desktop\"

For i = Fldr.Items.count To 1 Step -1
    Set olMi = Fldr.Items(i)
    If InStr(1, olMi.Subject, "[The email I'm looking for by subject]") > 0 Then
         For Each olAtt In olMi.Attachments
                olAtt.Module2.Format   '<--- this is where i try to do step 3
                olAtt.SaveAsFile MyPath & "NewSheet" & ".xls"             
        With olEmail
            .BodyFormat = olFormatHTML
            .Body = olAtt.Range   '<----this is where i try to do step 4
            .To = "someone@something.com"
            .Subject = "Tester"
            .send
        End With

        Next olAtt
        olMi.Save
    End If
Next i

Set olAtt = Nothing
Set olMi = Nothing
Set Fldr = Nothing
Set MoveToFldr = Nothing
Set olNs = Nothing
Set olApp = Nothing

End Sub

【问题讨论】:

  • “运行格式化子程序”究竟是什么?这里的附件是什么?
  • 子程序只是简单地格式化 excel 文档以使其看起来像样。附件是一个 Excel 表,上面有原始数据,所以我使用了一个子程序,按照我想要的方式对其进行格式化。
  • 您可能需要将每个附件保存到磁盘以便“重新格式化”它,但如果没有更多细节(例如Module2.Format 的代码),就不可能提出更详细的建议。
  • Module2.Format 子例程已经工作。我每天都使用 ctrl+F 热键来自动格式化我打开的文件。现在我只是想让它调用子程序,而不必自己打开文件并按下为其设置的热键。抱歉没有更具体。

标签: vba outlook attachment


【解决方案1】:

Getting Started with VBA in Outlook 2010

我注意到以下代码:

For i = Fldr.Items.count To 1 Step -1
 Set olMi = Fldr.Items(i)
  If InStr(1, olMi.Subject, "[The email I'm looking for by subject]") > 0 Then

不要遍历文件夹中的所有项目。相反,您需要使用 Items 类的 Find/FindNextRestrict 方法来获取与您的条件相对应的项目。您可以在 MSDN 中的以下文章中阅读有关它们的更多信息。此外,您可能会发现以下文章很有帮助:

.Body = olAtt.Range '

Outlook 对象模型提供了三种处理项目主体的主要方式:

  1. Body.
  2. HTMLBody
  3. Word 编辑器。 Inspector 类的WordEditor 属性返回代表消息正文的Word 文档实例。因此,您可以使用 Word 对象模型对消息正文进行任何您需要的操作

更多信息请参见Chapter 17: Working with Item Bodies

【讨论】:

  • 感谢您的意见。我会调查的。
猜你喜欢
  • 1970-01-01
  • 2020-06-10
  • 2011-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-20
  • 2011-03-15
相关资源
最近更新 更多