跳至主要内容

如何快速打开文件对话框以在Outlook中插入附件?

Author: Siluvia Last Modified: 2025-07-31

当我们需要在正在编辑的邮件中插入附件时,通常我们需要点击“插入” > “附加文件” > “浏览这台电脑”,以打开“插入文件”对话框,然后找到并插入所需的文件。本教程提供了两个VBA代码,帮助您通过单击轻松打开“插入文件”对话框。


使用VBA快速打开文件对话框以插入附件

以下VBA代码可以实现:

VBA代码1:打开计算机中的默认文档文件夹 VBA代码2:打开计算机中的指定文件夹

请按照以下步骤操作完成任务。

1. 启动您的Outlook,按 Alt + F11 键打开 Microsoft Visual Basic for Applications 窗口。

2. 在 Microsoft Visual Basic for Applications 窗口中,点击“工具” > “引用”,然后在“引用 – Project1”对话框中勾选“Microsoft Scripting Runtime”。

steps of opening the file dialog box to insert an attachment in outlook

3. 点击“插入” > “模块”。然后将以下VBA代码之一复制到“模块”窗口中。

steps of opening the file dialog box to insert an attachment in outlook

VBA代码1:打开计算机中的默认文档文件夹

Sub OpenFileDialog()
'Updated by Extendoffice 20220713
Dim xApp As Object
Dim xFileDlg As FileDialog
Dim xSelItem As Variant
Dim xMail As MailItem
On Error Resume Next
Set xApp = CreateObject("Excel.Application")
xApp.Visible = False
Set xFileDlg = xApp.Application.FileDialog(msoFileDialogFilePicker)
xFileDlg.AllowMultiSelect = True
If xFileDlg.Show = 0 Then Exit Sub
Set xMail = Application.ActiveInspector.currentItem
For Each xSelItem In xFileDlg.SelectedItems
    xMail.Attachments.Add xSelItem
Next
xApp.Quit
Set xFileDlg = Nothing
Set xApp = Nothing
End Sub

VBA代码2:打开计算机中的指定文件夹

Sub OpenCertianFolderDialog()
'Updated by Extendoffice 20220713
Dim xApp As Object
Dim xFileDlg As FileDialog
Dim xSelItem As Variant
Dim xMail As MailItem
On Error Resume Next
Set xApp = CreateObject("Excel.Application")
xApp.Visible = False
Set xFileDlg = xApp.Application.FileDialog(msoFileDialogFilePicker)
xFileDlg.InitialFileName = "C:\Users\Win10x64Test\Desktop\save attachments\"  'Specify the path to the folder you want to open
xFileDlg.AllowMultiSelect = True
If xFileDlg.Show = 0 Then GoTo L1
Set xMail = Application.ActiveInspector.CurrentItem
For Each xSelItem In xFileDlg.SelectedItems
    xMail.Attachments.Add xSelItem
Next
L1:
    xApp.Quit
    Set xFileDlg = Nothing
    Set xApp = Nothing
End Sub

注意事项:

1) VBA代码1有助于打开计算机中的默认文档文件夹。
2) 在VBA代码2中,请将以下行中的文件夹路径更改为所需的文件夹路径。xFileDlg.InitialFileName = "C:\Users\Win10x64Test\Desktop\save attachments\" 这样每次运行此代码时都会打开指定的文件夹。

4. 按 Alt + Q 键关闭 Microsoft Visual Basic for Applications 窗口。

现在,您需要一个按钮来运行宏。

5. 点击“主页” > “新建邮件”创建新邮件。在邮件窗口中,点击“自定义快速访问工具栏” > “更多命令”。

steps of opening the file dialog box to insert an attachment in outlook

6. 在“Outlook选项”对话框中,您需要进行如下配置。

6.1) 在“从以下位置选择命令”下拉列表中,选择“宏”;
6.2) 选择上一步添加的宏;
6.3) 点击“添加”按钮,将该宏添加到“自定义快速访问工具栏”框中。
steps of opening the file dialog box to insert an attachment in outlook

7. 在右侧框中保持脚本选中状态,然后点击“修改”按钮。在“修改按钮”对话框中,为脚本分配一个新按钮并点击“确定”。

steps of opening the file dialog box to insert an attachment in outlook

8. 在“Outlook选项”对话框中点击“确定”保存更改。

9. 然后,您在第7步中指定的按钮将被添加到“快速访问工具栏”。在撰写邮件时,如果要插入附件,只需点击此按钮即可打开“浏览”文件夹并选择需要插入的文件。

steps of opening the file dialog box to insert an attachment in outlook

最佳 Office 办公效率工具

重磅消息:Kutools for Outlook 推出免费版本!

体验全新 Kutools for Outlook 免费版,70 多个强大功能,永久免费使用!点击立即下载!

🤖 Kutools AI 利用先进的AI技术轻松处理邮件,包括答复、总结、优化、扩展、翻译和撰写邮件。

📧 邮件自动化自动答复(支持POP和IMAP) /计划发送邮件 /发送邮件时根据规则自动抄送密送 / 自动转发(高级规则) / 自动添加问候语 / 自动将群发邮件拆分为单独邮件 ...

📨 邮件管理撤回邮件 / 按主题等方式阻止诈骗邮件 / 删除重复邮件 / 高级搜索 / 整合文件夹 ...

📁 附件专家批量保存 / 批量拆离 / 批量压缩 / 自动保存 / 自动拆离 / 自动压缩 ...

🌟 界面魔法😊更多精美个性表情 /重要邮件来临时提醒您 / 最小化而非关闭 Outlook ...

👍 一键高效操作带附件全部答复 /反钓鱼邮件 / 🕘显示发件人时区 ...

👩🏼‍🤝‍👩🏻 联系人与日历从选中的邮件批量添加联系人 / 将联系人组拆分为多个独立组 / 移除生日提醒 ...

使用 Kutools,支持英语、西班牙语、德语、法语、中文及40 多种其他语言,满足您的语言偏好!

一键解锁 Kutools for Outlook。无需等待,立即下载,提升办公效率!

kutools for outlook features1 kutools for outlook features2