Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 用于添加附件的文件路径的通配符_Vba_Outlook_Wildcard - Fatal编程技术网

Vba 用于添加附件的文件路径的通配符

Vba 用于添加附件的文件路径的通配符,vba,outlook,wildcard,Vba,Outlook,Wildcard,我想添加特定文件夹中的附件。我指定了文件的路径和两个固定的关键字 在“filename2”之后和“pmonth”之前有更多字符需要完成文件路径,这些字符不是固定的,因此我需要使用通配符(*) 代码给出了 “找不到文件” 我已经通过各种线程并尝试解决方案。没有一个能满足我的需要 ctr=2至最后一行的 filename1=单元格(ctr,1).Value filename2=单元格(ctr,3).Value Set OutMail=OutApp.CreateItemFromTemplate(str

我想添加特定文件夹中的附件。我指定了文件的路径和两个固定的关键字

在“filename2”之后和“pmonth”之前有更多字符需要完成文件路径,这些字符不是固定的,因此我需要使用通配符(*)

代码给出了

“找不到文件”

我已经通过各种线程并尝试解决方案。没有一个能满足我的需要

ctr=2至最后一行的

filename1=单元格(ctr,1).Value
filename2=单元格(ctr,3).Value
Set OutMail=OutApp.CreateItemFromTemplate(str\u模板)
path=“C:\Users\nikunj.v.tripathi\Desktop\”&filename1&“\ux2&”-“&”*”&pmonth&“&syear&“.xlsx”
发邮件

.Attachments.Add path“要在本例中有效地使用Dir函数,需要将路径和文件名作为两个单独的变量。假设您添加了另一个名为filename的变量,则可以使用以下代码

...

path = "C:\Users\nikunj.v.tripathi\Desktop\"
filename = filename1 & "_" & filename2 & " -" & "*" & pmonth & " " & syear & ".xlsx"

...

filename = Dir(path & filename) ' Dir returns the filename of the first file matching
                                ' the criteria, or returns an empty string if no match.

Do Until filename = ""
    .Attachments.Add path & filename
    filename = Dir ' Using Dir again returns the next file matching
                   ' the criteria, or returns an empty string if no match.
Loop


当然-
Attachments.Add
添加单个附件并返回
attachment
对象。它如何可能添加多个附件

您可以使用
Scripting.FileSystemObject
循环浏览文件夹中的所有文件,并一次添加一个附件。例如,见

使用
Dir
-命令并循环其结果。一个接一个地附上文件。hi@FunThomas,感谢您的回复我以前从未使用过Dir,所以您建议在指定路径时使用Dir还是在outmail中调用它?你能举个例子吗?嗨,山姆,你的代码工作得很好。谢谢你的帮助。:)