微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

vba中的.attachment问题

如何解决vba中的.attachment问题

   Dim strto As String
   Dim lastrow As Long
   stirfile = Dir(DestPath & "*.pdf*")
   For i = lastrow To 7 Step 1
   lastrow = ActiveSheet.Cells(Rows.Count,2).End(xlUp).Row
   strto = Range("G7") & ";" & Range("K7") & ";" & Range("O7") & ";" & Range("S7")
   With Mail
      .Subject = "Quote reg for VAM project"
      .From = "arunkumarg@gmail.com"
      .To = strto
      .CC = ""
      .BCC = ""
      .TextBody = strbody
              .Attachments.Add (DestPath)
   End With
   Next i
   Mail.Send
End Sub

我只想将目标路径中的所有 pdf 文件通过 gmail 发送,我在“.Attachment”区域失败了 请指导我

解决方法

这演示了如何使用 Dir 遍历文件夹中的文件。

Option Explicit ' Consider this mandatory
' Tools | Options | Editor tab
' Require Variable Declaration
' If desperate declare as Variant

Private Sub attachFilesInFolder()
    
    Dim myMail As MailItem
    Dim olApp As outlook.Application
    
    Dim DestPath As String
    
    Dim stirFilePath As String
    Dim stirFile As String
    
    DestPath = "P:\kin\PROJECTS\TARSON\Newfolder1\"
    stirFilePath = DestPath & "*.pdf"
    
    Debug.Print
    Debug.Print "stirFilePath.......: " & stirFilePath
    
    Set olApp = New outlook.Application
    
    Set myMail = olApp.CreateItem(0)
        
    With myMail
        
        .Subject = "Quote reg for VAM project"
        
        stirFile = dir(stirFilePath)
        Debug.Print "DestPath & stirFile: " & DestPath & stirFile
            
        Do While stirFile <> ""
            Debug.Print "       stirFile....: " & stirFile
            .Attachments.add (DestPath & stirFile)
            stirFile = dir
        Loop
            
    End With
        
    myMail.Display
   
End Sub

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。