【问题标题】:How to create a series of emails, to attach files in a folder, while limiting the number of files attached to any one email?如何创建一系列电子邮件,在文件夹中附加文件,同时限制附加到任何一封电子邮件的文件数量?
【发布时间】:2021-10-29 13:17:44
【问题描述】:

我需要将选定文件夹中的文件附加到 Outlook 电子邮件。

例如,如果文件数为 15,则应创建 2 封电子邮件。第一封电子邮件将包含前 10 个文件,第二封电子邮件将包含剩余的 5 个。
换句话说,一封电子邮件最多可以包含 10 个文件,如果一个文件夹中的文件超过 10 个,则继续创建新电子邮件并附加文件,直到全部附加。

以下代码创建了正确数量的电子邮件(如果有 12 个文件,它会创建 2 封电子邮件),但会将所有文件附加到每封电子邮件(两封电子邮件都包含 12 个文件)。

Sub attach()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim strbody As String
    
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Show
        .Title = "Select a Folder"
        sFolder = .SelectedItems(1)
    End With
    
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(sFolder)
    Set fls = f.Files
    
    Z = 10
    
    For d = 0 To fls.Count - 1 Step 10
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
        'On Error Resume Next
        With OutMail
            .To = "abc@gmail.com"
            .CC = ""
            .subject = "file"
            
            y = 0
            For Each x In fls
                If y < Z Then
                    .Attachments.Add (sFolder & "\" & x.Name)
                    y = y + 1
                Else
                    Exit For
                End If
            
                Z = Z + 10
            Next
            .Display
        End With
    Next
End Sub

【问题讨论】:

  • 你能修正上一个问题中的Attachements 错字吗?
  • 是的,我修好了。问题依然存在
  • 我建议将文件路径放入一个数组并循环遍历它。您可以轻松地从数组中的元素数中获取文件数。然后在 10 号之后创建另一封电子邮件并附上其余的。

标签: vba file email outlook attachment


【解决方案1】:

您可以尝试使用Mod 函数,该函数将两个数字相除并仅返回余数,请参见以下代码:

Z = 10

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

For d = 0 To fls.Count - 1

  If (d Mod 10) == 0 Then
    Set OutMail = OutApp.CreateItem(0)
  End If

  'On Error Resume Next
   With OutMail
      .To = "abc@gmail.com"
      .CC = ""
      .subject = "file"
        
      y = 0

        'set reference to the x file there

        If y < Z Then
          .Attachments.Add (sFolder & "\" & x.Name)
           y = y + 1
        Else
          Exit For
        End If
        
       If (d Mod 9) == 0 Then
          .Display
       End If   
        
    End With

Next

【讨论】:

    【解决方案2】:

    由于fls 无法通过索引进行迭代,因此将文件放入集合中。一旦你这样做了,逻辑就变得非常简单。下面的代码突出显示了如何为每封电子邮件获取正确数量的附件。您可以修改它以添加特定于 Outlook 的调用来实际创建电子邮件:

    'add the files to a collection so we can iterate by index
    Set cFiles = New Collection
       
    For Each f In fls
       cFiles.Add sFolder & "\" & f.Name
    Next
       
    'build our emails
    For i = 0 To cFiles.Count - 1
       If i Mod 10 = 0 Then
          Debug.Print "New email"
       End If
       
       Debug.Print "Attachment:  " & cFiles(i + 1)
    Next
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-25
      • 1970-01-01
      • 2023-01-03
      相关资源
      最近更新 更多