Skip to main content

如何快速打開文件對話框以在Outlook中插入附件?

Author: Siluvia Last Modified: 2025-05-13

當我們需要在撰寫郵件時插入附件,通常需要點擊「插入」>「附加檔案」>「瀏覽這台電腦」來打開「插入檔案」對話框,然後找到並插入所需的檔案。在本教程中,我們提供了兩個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