Vba 从Outlook下载附件并在Excel中打开
我正在尝试使用Excel中的VBA在Outlook电子邮件中下载并打开Excel电子表格附件。 我怎样才能:Vba 从Outlook下载附件并在Excel中打开,vba,excel,outlook,email-attachments,Vba,Excel,Outlook,Email Attachments,我正在尝试使用Excel中的VBA在Outlook电子邮件中下载并打开Excel电子表格附件。 我怎样才能: 下载我的Outlook收件箱中第一封电子邮件(最新电子邮件)中唯一的附件 将附件保存在具有指定路径(例如:“C:…”)的文件中 将附件名称重命名为:当前日期+以前的文件名 将电子邮件保存到具有类似“C:…”路径的其他文件夹中 将Outlook中的电子邮件标记为“已读” 在excel中打开excel附件 我还希望能够将以下内容保存为分配给各个变量的单个字符串: 发件人电子邮件地址 收到日
- 发件人电子邮件地址
- 收到日期
- 发送日期
- 主题
- 电子邮件的信息
Sub SaveAttachments()
Dim olFolder As Outlook.MAPIFolder
Dim att As Outlook.Attachment
Dim strFilePath As String
Dim fsSaveFolder As String
fsSaveFolder = "C:\test\"
strFilePath = "C:\temp\"
Set olFolder = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
For Each msg In olFolder.Items
While msg.Attachments.Count > 0
bflag = False
If Right$(msg.Attachments(1).Filename, 3) = "msg" Then
bflag = True
msg.Attachments(1).SaveAsFile strFilePath & strTmpMsg
Set msg2 = Application.CreateItemFromTemplate(strFilePath & strTmpMsg)
End If
sSavePathFS = fsSaveFolder & msg2.Attachments(1).Filename
End If
End Sub
我可以一次性提供完整的代码,但这无助于您从中学习;)所以,让我们把你的请求分解,然后逐一处理。这将是一个很长的帖子,所以请耐心:) 共有5个部分将涵盖所有7点(是7点,不是6点),因此您不必为第7点创建新问题
第一部分
发件人电子邮件地址
,收到日期
,发送日期
,主题
,电子邮件的消息
Const olFolderInbox As Integer = 6
Sub ExtractFirstUnreadEmailDetails()
Dim oOlAp As Object, oOlns As Object, oOlInb As Object
Dim oOlItm As Object
'~~> Outlook Variables for email
Dim eSender As String, dtRecvd As String, dtSent As String
Dim sSubj As String, sMsg As String
'~~> Get Outlook instance
Set oOlAp = GetObject(, "Outlook.application")
Set oOlns = oOlAp.GetNamespace("MAPI")
Set oOlInb = oOlns.GetDefaultFolder(olFolderInbox)
'~~> Check if there are any actual unread emails
If oOlInb.Items.Restrict("[UnRead] = True").Count = 0 Then
MsgBox "NO Unread Email In Inbox"
Exit Sub
End If
'~~> Store the relevant info in the variables
For Each oOlItm In oOlInb.Items.Restrict("[UnRead] = True")
eSender = oOlItm.SenderEmailAddress
dtRecvd = oOlItm.ReceivedTime
dtSent = oOlItm.CreationTime
sSubj = oOlItm.Subject
sMsg = oOlItm.Body
Exit For
Next
Debug.Print eSender
Debug.Print dtRecvd
Debug.Print dtSent
Debug.Print sSubj
Debug.Print sMsg
End Sub
因此,请注意您的请求,该请求涉及在变量中存储细节
第2部分 现在转到下一个请求
Const olFolderInbox As Integer = 6
'~~> Path for the attachment
Const AttachmentPath As String = "C:\"
Sub DownloadAttachmentFirstUnreadEmail()
Dim oOlAp As Object, oOlns As Object, oOlInb As Object
Dim oOlItm As Object, oOlAtch As Object
'~~> New File Name for the attachment
Dim NewFileName As String
NewFileName = AttachmentPath & Format(Date, "DD-MM-YYYY") & "-"
'~~> Get Outlook instance
Set oOlAp = GetObject(, "Outlook.application")
Set oOlns = oOlAp.GetNamespace("MAPI")
Set oOlInb = oOlns.GetDefaultFolder(olFolderInbox)
'~~> Check if there are any actual unread emails
If oOlInb.Items.Restrict("[UnRead] = True").Count = 0 Then
MsgBox "NO Unread Email In Inbox"
Exit Sub
End If
'~~> Extract the attachment from the 1st unread email
For Each oOlItm In oOlInb.Items.Restrict("[UnRead] = True")
'~~> Check if the email actually has an attachment
If oOlItm.Attachments.Count <> 0 Then
For Each oOlAtch In oOlItm.Attachments
'~~> Download the attachment
oOlAtch.SaveAsFile NewFileName & oOlAtch.Filename
Exit For
Next
Else
MsgBox "The First item doesn't have an attachment"
End If
Exit For
Next
End Sub
第4部分 继续下一个请求
read
Const olFolderInbox As Integer = 6
Sub MarkAsUnread()
Dim oOlAp As Object, oOlns As Object, oOlInb As Object
Dim oOlItm As Object, oOlAtch As Object
'~~> Get Outlook instance
Set oOlAp = GetObject(, "Outlook.application")
Set oOlns = oOlAp.GetNamespace("MAPI")
Set oOlInb = oOlns.GetDefaultFolder(olFolderInbox)
'~~> Check if there are any actual unread emails
If oOlInb.Items.Restrict("[UnRead] = True").Count = 0 Then
MsgBox "NO Unread Email In Inbox"
Exit Sub
End If
'~~> Mark 1st unread email as read
For Each oOlItm In oOlInb.Items.Restrict("[UnRead] = True")
oOlItm.UnRead = False
DoEvents
oOlItm.Save
Exit For
Next
End Sub
第5部分 继续下一个请求
Sub OpenExcelFile()
Dim wb As Workbook
'~~> FilePath is the file that we earlier downloaded
Set wb = Workbooks.Open(FilePath)
End Sub
我把这篇文章转换成了几篇博客文章(有更多的解释),可以通过中的第15、16和17点访问
感谢Sid:)为您的代码(偷了您的代码)。。我今天遇到了这种情况。这是我的代码。下面的代码保存附件,邮件和邮件信息。所有信用都归Sid所有
已测试
次mytry()
作为对象的Dim-olapp
作为对象的Dim olmapi
将邮件作为对象
作为对象
Dim lrow作为整数
作为对象的Dim-olattach
作为字符串的Dim str
常量num作为整数=6
常量路径为String=“C:\HP\”
Const emailpath为String=“C:\Dell\”
常量olFolderInbox为整数=6
设置olp=CreateObject(“outlook.application”)
设置olmapi=olp.getnamespace(“MAPI”)
设置olmail=olmapi.getdefaultfolder(num)
如果olmail.items.restrict(“[UNREAD]=True”).Count=0,则
MsgBox(“无未读邮件”)
其他的
对于olmail.items.restrict中的每个olitem(“[UNREAD]=True”)
lrow=ActiveSheet.Range(“A”&Rows.Count)。End(xlUp)。Row+1
范围(“A”&lrow).Value=m.Subject
范围(“B”&lrow).Value=olitem.senderemailaddress
范围(“C”&lrow)。值=m.to
范围(“D”和lrow)。值=m.cc
范围(“E”和lrow)。值=m.body
如果m.attachments.Count为0,则
对于OLITAM附件中的每个olattach
olattach.saveas文件路径和格式(日期,“MM dd yyyy”)&olattach.Filename
下一个奥拉塔
如果结束
str=受试者
str=替换(str,“/”,“-”)
str=替换(str,“|”和“124;”)
调试。打印str
olitem.SaveAs(emailpath&str&“.msg”)
m.unread=False
多芬特
嗯,救命
下一代
如果结束
ActiveSheet.Rows.WrapText=False
端接头
你睡过吗?:)。我甚至没有时间读这篇文章,更不用说写了,假设我知道怎么写的话。感谢您在SO上所做的出色工作。+1天哪!你真的拥有世界上所有的时间:但是我必须说,我真的很喜欢阅读你的帖子。你肯定会花时间尽可能地让你的帖子内容丰富。继续努力!这篇文章太棒了!感谢您解释每一步并花时间写出每一步。我希望我能不止一次地投票支持这个答案。继续完成令人惊叹的工作,所以。:)希德,很高兴看到你再次发帖!每次你回答问题,我都能学到一些有价值的东西@Siddharth Rout如果我想从给定主题的特定文件中读取附件,该怎么办。
Sub OpenExcelFile()
Dim wb As Workbook
'~~> FilePath is the file that we earlier downloaded
Set wb = Workbooks.Open(FilePath)
End Sub
(Excel vba)
Tested
Sub mytry()
Dim olapp As Object
Dim olmapi As Object
Dim olmail As Object
Dim olitem As Object
Dim lrow As Integer
Dim olattach As Object
Dim str As String
Const num As Integer = 6
Const path As String = "C:\HP\"
Const emailpath As String = "C:\Dell\"
Const olFolderInbox As Integer = 6
Set olp = CreateObject("outlook.application")
Set olmapi = olp.getnamespace("MAPI")
Set olmail = olmapi.getdefaultfolder(num)
If olmail.items.restrict("[UNREAD]=True").Count = 0 Then
MsgBox ("No Unread mails")
Else
For Each olitem In olmail.items.restrict("[UNREAD]=True")
lrow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row + 1
Range("A" & lrow).Value = olitem.Subject
Range("B" & lrow).Value = olitem.senderemailaddress
Range("C" & lrow).Value = olitem.to
Range("D" & lrow).Value = olitem.cc
Range("E" & lrow).Value = olitem.body
If olitem.attachments.Count <> 0 Then
For Each olattach In olitem.attachments
olattach.SaveAsFile path & Format(Date, "MM-dd-yyyy") & olattach.Filename
Next olattach
End If
str = olitem.Subject
str = Replace(str, "/", "-")
str = Replace(str, "|", "_")
Debug.Print str
olitem.SaveAs (emailpath & str & ".msg")
olitem.unread = False
DoEvents
olitem.Save
Next olitem
End If
ActiveSheet.Rows.WrapText = False
End Sub