【发布时间】:2015-09-09 04:07:57
【问题描述】:
这基本上就是我想要做的......
按主题名称搜索特定电子邮件
获取该电子邮件的附件(附件是原始数据的 excel 表)
从 Excel 附件上的另一个模块运行格式化子例程
将新格式的附件放在新电子邮件的正文中
将新邮件发送给客户
我需要关于步骤 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