如何下载Excel格式邮件附件?VBA导出Outlook附件乱码求助
Hey there! Let's tackle your two Outlook attachment questions one by one, nice and clear.
You’ve got two reliable ways to get this done:
手动下载(simple & straightforward)
- Open the target email and locate the attachment section at the bottom
- Right-click the Excel attachment (with
.xlsxor.xlssuffix) and select Save As - Choose your local save folder in the pop-up window, then click Save to finish
VBA batch download(great for repeated tasks)
If you need to handle Excel attachments from multiple emails in bulk, use this basic VBA code:
Public Sub BatchSaveExcelAttachments() Dim olApp As Outlook.Application Dim olNS As Outlook.Namespace Dim olFolder As Outlook.MAPIFolder Dim olMail As Outlook.MailItem Dim objAtt As Outlook.Attachment Dim savePath As String ' Set your local save path, remember to add a backslash at the end savePath = "C:\MyExcelAttachments\" Set olApp = New Outlook.Application Set olNS = olApp.GetNamespace("MAPI") ' Select your target folder (e.g., a subfolder under Inbox) Set olFolder = olNS.GetDefaultFolder(olFolderInbox).Folders("Your Target Folder") ' Loop through every email in the folder For Each olMail In olFolder.Items If olMail.Class = olMail Then ' Loop through all attachments in the email For Each objAtt In olMail.Attachments ' Only save Excel format attachments If LCase(Right(objAtt.FileName, 5)) = ".xlsx" Or LCase(Right(objAtt.FileName, 4)) = ".xls" Then objAtt.SaveAsFile savePath & objAtt.FileName End If Next objAtt End If Next olMail ' Release objects to free up memory Set objAtt = Nothing Set olMail = Nothing Set olFolder = Nothing Set olNS = Nothing Set olApp = Nothing MsgBox "Excel attachments downloaded successfully!" End Sub
Based on your description, the core issue is almost certainly that your code isn’t preserving the attachment’s original filename (including the .xlsx suffix). Without the correct extension, Windows treats the file as an unknown "File" type, which causes the garbled text when you open it.
Problem breakdown
From your code snippet, you declared objAtt As Outlook.Attachment, but you’re probably not using objAtt.FileName (this property includes the full original name like Report.xlsx) when saving. Instead, you might be manually specifying a filename without the suffix, or mishandling the filename value.
Fixed code example
Here’s the adjusted complete code to ensure proper xlsx attachment saving:
Public Sub saveAttachtoDisk(itm As Outlook.MailItem) Dim objAtt As Outlook.Attachment Dim saveFolder As String ' Replace with your own local save path, must end with a backslash! saveFolder = "C:\Your\Target\Folder\" ' Loop through all attachments in the current email For Each objAtt In itm.Attachments ' Only process xlsx attachments to avoid interference from other types If LCase(Right(objAtt.FileName, 5)) = ".xlsx" Then ' Key step: Use the attachment's original filename to preserve the .xlsx suffix objAtt.SaveAsFile saveFolder & objAtt.FileName End If Next objAtt ' Release object to avoid memory leaks Set objAtt = Nothing End Sub
Critical reminders
- Always use
objAtt.FileName: This property grabs the full original name of the attachment, including the.xlsxsuffix, so Windows can recognize the file type correctly - Check your save path: Make sure the path ends with a backslash
\—otherwise, the filename will merge with the path incorrectly (e.g.,C:\FolderFileinstead ofC:\Folder\File.xlsx) - Filter attachment types: Adding the
.xlsxcheck prevents accidental saving of other file types that might cause issues
Extra troubleshooting steps
- Confirm your Outlook rule is correctly triggering this macro and isn’t being applied to non-target emails
- Double-check that the attachments in the emails are standard xlsx files (you already said manual saving works, but a quick verification won’t hurt)
内容的提问来源于stack exchange,提问作者EAA




