我想添加特定文件夹中的附件。我指定了文件的路径和两个固定的关键字。
在"filename2"之后和"pmmonth"之前还有更多的字符需要完成文件路径,这些字符是不固定的,因此我需要使用通配符(*(。
代码给出
"找不到文件">
我经历了各种各样的线程并尝试了解决方案。没有一个能满足我的要求。
For ctr = 2 To lastrow
filename1 = Cells(ctr, 1).Value
filename2 = Cells(ctr, 3).Value
Set OutMail = OutApp.CreateItemFromTemplate(str_template)
path = "C:Usersnikunj.v.tripathiDesktop" & filename1 & "_" & filename2 & " -" & "*" & pmonth & " " & syear & ".xlsx"
With OutMail
.Attachments.Add path ' <----- this line gives error
.To = Cells(ctr, 10).Value
.cc = Cells(ctr, 11).Value
.htmlbody = Replace(.htmlbody, "#Month#", smonth)
.htmlbody = Replace(.htmlbody, "#CLIENT NAME#", Cells(ctr, 1).Value
.Save
End With
Next ctr
...
path = "C:Usersnikunj.v.tripathiDesktop"
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
循环浏览文件夹中的所有文件,并一次添加一个附件。例如,请参见https://devblogs.microsoft.com/scripting/how-can-i-get-a-list-of-all-the-files-in-a-folder-and-its-subfolders/