首页 > 解决方案 > 附加文件夹中的所有 CSV 文件

问题描述

我正在运行一个脚本来自动发送带有附件的电子邮件。所有附件都有一个.csv扩展名。

我事先不知道文件的名称。我正在使用该Dir语句。

我尝试将Dir语句分成不同的字符串,但这也不起作用。

Dim cAttachment As String
Dim Folder As String
Dim fileCriteria As String

Folder = "C:\Users\____\Desktop\Test Folder"
fileCriteria = ".csv"
cAttachment = Dir(Folder & "\*" & fileCriteria)

我也试过:

Dim cAttachment As String 
cAttachment = Dir("C:\Users\___\Desktop\Test Folder\*.csv")

我明白了

预期的语句结束

在我Dir声明的前括号中。

标签: vbaoutlook

解决方案


您可以轻松实现您的结果,而无需使用旧的Dir() Function. 为此,您需要使用"Scripting.FileSystemObject".

这是发现特定文件夹中所有扩展名文件的代码:.csv

Dim oFile       As Object
Dim oFSO        As Object
Dim oFolder     As Object
Dim oFiles      As Object  

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder("C:\my\Folder\") 'Set this accordingly
Set oFiles = oFolder.Files

'For all files in the folder
For Each oFile In oFiles
    If (oFile Like "*.csv") Then
        'Add this file to attachments
        objMessage.AddAttachment oFile.Path
    End If
Next

希望这可以帮助。


推荐阅读