首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >通过VBA附加多个文件

通过VBA附加多个文件
EN

Stack Overflow用户
提问于 2018-02-08 13:57:33
回答 1查看 3.5K关注 0票数 0

谁能帮我编辑下面的脚本,以添加多个文件列在电子表格的第3列(列C)?

我当前的宏一次查找一个文件,并发送单独的电子邮件。我需要它来查找C列(第三列)中列出的多个文件名(在列出的文件夹路径中),并且它会这样做,直到它到达空单元格。

我当前的脚本在下面,你会看到它一次查找一个文件。

代码语言:javascript
运行
复制
Sub AttachandSendEmail()
Dim obMail As Outlook.MailItem
Dim irow As Integer
Dim dpath As String
Dim pfile As String

'file path
dpath = "C:\Users\filelocation"

'looping through all the files and sending an mail

irow = 1

Do While Cells(irow, 3) <> Empty


'pikcing up file name from column C
 pfile = Dir(dpath & "\*" & Cells(irow, 3) & "*")


'checking for file exist in a folder and if its a pdf file

If pfile <> "" And Right(pfile, 3) = "pdf" Then

    Set obMail = Outlook.CreateItem(olMailItem)

    With obMail
        .To = "email@comapny.com"
        .Subject = "O/S Blanace"
        .BodyFormat = olFormatPlain
        .Body = "Please see attached files"
        .Attachments.Add (dpath & "\" & pfile)
        .Send
    End With

End If

'go to next file listed on the C column
 irow = irow + 1

Loop


End Sub
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-08 14:04:44

试一试,它会发送一条附加了所有文件的消息。

代码语言:javascript
运行
复制
Set obMail = Outlook.CreateItem(olMailItem)
With obMail
    .To = "email@comapny.com"
    .Subject = "O/S Blanace"
    .BodyFormat = olFormatPlain
    .Body = "Please see attached files"

    Do While Cells(irow, 3) <> Empty
        'pikcing up file name from column C
        pfile = Dir(dpath & "\*" & Cells(irow, 3) & "*")
        'checking for file exist in a folder and if its a pdf file

        If pfile <> "" And Right(pfile, 3) = "pdf" Then
            .Attachments.Add (dpath & "\" & pfile)
        End If

        'go to next file listed on the C column
        irow = irow + 1
    Loop

    .Send
End With
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48678566

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档