vba - 附加文件夹中的所有 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
声明的前括号中。
解决方案
您可以轻松实现您的结果,而无需使用旧的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
希望这可以帮助。
推荐阅读
- google-sheets - 谷歌电子表格动态超链接
- c++ - 完美转发无法与显式模板实例化指令(EIDir;又名手动实例化)链接
- node.js - 仅显示用于调试 node.js 的旧版和预览选项
- javascript - Firebase 和 Angular:*ngFor 无需重新加载图像
- ruby-on-rails - redmine收不到邮件
- javascript - JavaScript 一次替换文本中的多个字符
- linux - curl 是否认为方案协议是允许的?
- api - 为什么亚马逊的供应商中心 API 没有在特定日期更改 PO?
- python - 如何将列中的值更改为假值
- c# - 调试 IIS Express 身份验证问题