【问题标题】:How to attach all files in folder to an email - vba code如何将文件夹中的所有文件附加到电子邮件 - vba 代码
【发布时间】:2019-01-16 14:03:03
【问题描述】:

我在 vba 中有以下代码,一切正常,但我需要更改以附加所选文件夹中的所有文件(现在我必须写下所述附件的名称)。 不幸的是,我在 vba 编程方面是个菜鸟。

Sub Send_Files()

Dim OutApp As Object
Dim OutMail As Object
Dim sh As Worksheet
Dim cell As Range
Dim FileCell As Range
Dim rng As Range

With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With

Set sh = Sheets("Sheet1")

Set OutApp = CreateObject("Outlook.Application")

For Each cell In sh.Columns("A").Cells.SpecialCells(xlCellTypeConstants)

    Set rng = sh.Cells(cell.Row, 1).Range("D1:Z1")

    If cell.Value Like "?*@?*.?*" And _
    Application.WorksheetFunction.CountA(rng) > 0 Then
        Set OutMail = OutApp.CreateItem(0)

        With OutMail
            .To = sh.Cells(cell.Row, 1).Value
            .CC = sh.Cells(cell.Row, 2).Value
            .Subject = "Decont UTA"
            .Body = sh.Cells(cell.Row, 3).Value

            For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
                If Trim(FileCell.Value) <> "" Then
                    If Dir(FileCell.Value) <> "" Then
                        .Attachments.Add FileCell.Value
                    End If
                End If
            Next FileCell

            .Send 'Or use .Display/Send
        End With

        Set OutMail = Nothing
    End If
Next cell

Set OutApp = Nothing

With Application
    .EnableEvents = True
    .ScreenUpdating = True
End With

End Sub

【问题讨论】:

  • 请分享您到目前为止所做的事情!如果您展示您的尝试以及您的问题是什么,您将获得更好的答案。
  • @Nathan 我知道这是可以做到的,我发现自己有几个例子,但我无法适应我的情况(我已经拥有的代码)。 @Sam我现在的问题是我想更改它将附加指定目录中所有文件的代码(例如,我有300封电子邮件每月发送2次,每次都有附件,附件每次都有不同的名称)
  • @yujy 那么为什么不使用示例循环文件并相应地调整您的.Attachments.Add
  • @Nathan_Sav,我不知道该怎么做。过去 2 天我一直在尝试这样做,但它不起作用,就像我说我是 vba 的新手。

标签: excel vba outlook


【解决方案1】:

在文件夹和子文件夹中查找特定文件的一般方法。

'******************************************************************
'* Find files in current folder and optionally in subfolders
'*
Option Explicit

Const ROOTFOLDER = "C:\Test"  'Change as desired
Const EXTENSION = "txt"       'Change as desired

Const FILES = "*." & EXTENSION

Dim g_FolderCount As Integer
Dim g_FileCount As Integer
'**********************************
'* Test code only
'*
Sub Test()
    Dim Path As String

    g_FileCount = 0
    g_FolderCount = 0
    Path = ROOTFOLDER
    GetSubFolders Path, True
    Debug.Print "Number of folders: " & g_FolderCount & ". Number of files: " & g_FileCount
End Sub
'****************************************************************
'* Recursive sub to find path and files
'*
Sub GetSubFolders(Path As String, subFolders As Boolean)
    Dim FSO As Object           'Late binding: Scripting.FileSystemObject
    Dim myFolder As Object      'Late binding: Folder
    Dim mySubFolder As Object

    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set myFolder = FSO.GetFolder(Path)
    If subFolders Then
        If myFolder.subFolders.Count <> 0 Then
            ProcessFiles Path                             'First branch (root)
            For Each mySubFolder In myFolder.subFolders
                g_FolderCount = g_FolderCount + 1
                GetSubFolders mySubFolder.Path, subFolders
            Next
        Else  'No more subfolders in Path, process files in current path
            ProcessFiles Path
        End If
    Else 'No subdirectories, process current only
       ProcessFiles Path
    End If
End Sub
'*********************************************
'* Callback from GetSubFolders
'* Process files in the found folder
'*
Sub ProcessFiles(ByVal Path As String)
    Dim theFilePattern As String
    Dim theFile As String

    Path = Path & "\"
    theFilePattern = Path & FILES
    theFile = Dir(theFilePattern)
    While theFile <> ""    'Attach file with your own code from here
        g_FileCount = g_FileCount + 1
        Debug.Print Path & theFile
        theFile = Dir()    ' Next file if any
    Wend
End Sub

【讨论】:

  • 我看不出这对我有什么帮助,只需将指定目录中的所有文件添加到电子邮件中(300 封不同的邮件,每封邮件有 300 个不同的目录,每个目录有许多不同的文件 - 主要是pdf),我什至无法开始添加年份。代码到我现有的代码。
  • 您是否阅读了我的评论在此处使用您自己的代码附加文件?在那里你打电话给一个将找到的文件附加到你的电子邮件的子:.Attachments.Add Path & theFile
  • 我试过了,但我不能让它工作。我是否必须制作 4 个不同的宏?!,我找到了一种从 excel 中检索所有命名附件的方法(因此我不需要编辑我的原始代码)。抱歉浪费了一年。时间,vba超出了我的能力。
猜你喜欢
  • 2020-01-14
  • 1970-01-01
  • 2021-10-28
  • 1970-01-01
  • 1970-01-01
  • 2014-06-25
  • 2016-09-10
  • 2015-01-10
  • 1970-01-01
相关资源
最近更新 更多