目前,我的代码列表将从收到的电子邮件中复制正文信息,并打开指定的Excel表格并将内容复制到Excel表格中并关闭它。我还想将附件从传入的电子邮件保存到指定的路径:C:\ Users \ ltorres \ Desktop \ Projects将附件从邮件自动下载并保存到Excel
我试过这个,但是这个代码不会与outlook结合。我会用Excel运行
Public Sub saveAttachtoDisk (itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim dateFormat As String
saveFolder = "C:\Users\ltorres\Desktop\Projects"
dateFormat = Format(Now, "yyyy-mm-dd H-mm")
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName
Set objAtt = Nothing
Next
End Sub
Const xlUp As Long = -4162
Sub ExportToExcel(MyMail As MailItem)
Dim strID As String, olNS As Outlook.NameSpace
Dim olMail As Outlook.MailItem
Dim strFileName As String
'~~> Excel Variables
Dim oXLApp As Object, oXLwb As Object, oXLws As Object
Dim lRow As Long
strID = MyMail.EntryID
Set olNS = Application.GetNamespace("MAPI")
Set olMail = olNS.GetItemFromID(strID)
'~~> Establish an EXCEL application object
On Error Resume Next
Set oXLApp = GetObject(, "Excel.Application")
'~~> If not found then create new instance
If Err.Number <> 0 Then
Set oXLApp = CreateObject("Excel.Application")
End If
Err.Clear
On Error GoTo 0
'~~> Show Excel
oXLApp.Visible = True
'~~> Open the relevant file
Set oXLwb = oXLApp.Workbooks.Open("C:\Users\ltorres\Desktop\Projects\Project 2\TemplateFinal\lighting.xlsm")
'~~> Set the relevant output sheet. Change as applicable
Set oXLws = oXLwb.Sheets("Multiplier")
lRow = oXLws.Range("A" & oXLws.Rows.Count).End(xlUp).Row + 1
'~~> Write to outlook
With oXLws
lRow = .Range("A" & .Rows.Count).End(xlUp).Row + 1
Dim MyAr() As String
MyAr = Split(olMail.Body, vbCrLf)
For i = LBound(MyAr) To UBound(MyAr)
.Range("A" & lRow).Value = MyAr(i)
lRow = lRow + 1
Next i
'
End With
'~~> Close and Clean up Excel
oXLwb.Close (True)
oXLApp.Quit
Set oXLws = Nothing
Set oXLwb = Nothing
Set oXLApp = Nothing
Set olMail = Nothing
Set olNS = Nothing
End Sub
请不要[以上结果](https://www.google.com/search?q=vba+save+outlook+attachment&oq=VBA+save+outlook+&aqs=chrome.0.0j69i57j0l4.2880j0j1&sourceid=chrome&ie= UTF-8)的帮助?你有什么尝试? – BruceWayne
@BruceWayne请参阅reedited文章。如上所述,该代码必须在excel中运行。我希望Outlook能够自动检测带有附件的新传入电子邮件,并将它们保存到路径 – Luis
“它必须在Excel中运行...我希望Outlook能够自动检测...”,那么Outlook不需要一些代码呢?你为什么认为这应该从Excel运行? (我没有使用Outlook/VBA,所以很好奇) – BruceWayne