Excel 使用Outlook宏通过电子邮件发送文件

Excel 使用Outlook宏通过电子邮件发送文件,excel,vba,outlook,Excel,Vba,Outlook,使用宏代码将文件夹的所有文件附加到Microsoft outlook电子邮件 Dim fldName As String Sub SendFilesbuEmail() ' From slipstick.me/njpnx Dim sFName As String i = 0 fldName = "C:\Users\" sFName = Dir(fldName) Do While Len(sFName) > 0 Call

使用宏代码将文件夹的所有文件附加到Microsoft outlook电子邮件

Dim fldName As String
Sub SendFilesbuEmail()
    ' From slipstick.me/njpnx
    Dim sFName As String

    i = 0

    fldName = "C:\Users\"

    sFName = Dir(fldName)

    Do While Len(sFName) > 0
        Call SendasAttachment(sFName)
        sFName = Dir
        i = i + 1
        Debug.Print fName
    Loop

    MsgBox i & " files were sent"
End Sub

Function SendasAttachment(fName As String)
    Dim olApp As Outlook.Application
    Dim olMsg As Outlook.MailItem
    Dim olAtt As Outlook.Attachments

    Set olApp = Outlook.Application
    Set olMsg = olApp.CreateItem(0) ' email
    Set olAtt = olMsg.Attachments

    ' attach file
    olAtt.Add (fldName & fName)

    ' send message
    With olMsg
        .Subject = "Here's that file you wanted"
        .To = "abcde@gmail.com"
        .HTMLBody = "Hi " & olMsg.To & ", <br /><br /> I have attached " & fName & " as you requested."
        .Send
    End With
End Function
Dim fldName作为字符串
子SendFilesbuEmail()
'来自slipstick.me/njpnx
将sFName设置为字符串
i=0
fldName=“C:\Users\”
sFName=Dir(fldName)
当Len(sFName)>0时执行
调用SendasAttachment(sFName)
sFName=Dir
i=i+1
调试。打印fName
环
MsgBox i&“已发送文件”
端接头
函数sendaAttachment(fName作为字符串)
Dim olApp作为Outlook.Application
将olMsg设置为Outlook.MailItem
将olAtt设置为Outlook.Attachments
设置olApp=Outlook.Application
设置olMsg=olApp.CreateItem(0)的电子邮件
设置olAtt=olMsg.Attachments
'附加文件
olAtt.Add(fldName和fName)
"发信息",
与奥尔姆格
.Subject=“这是您想要的文件”
.To=”abcde@gmail.com"
.HTMLBody=“Hi”&olMsg.To&“,

我已根据您的要求附上“&fName&” .发送 以 端函数

我收到0个已发送的文件,但该文档未在电子邮件中传输到Microsoft outlook

若要将所有文件附加到一封电子邮件中,请尝试修改您的代码

例如

选项显式
将文件路径设置为字符串
子SendFilesbuEmail()
将文件设置为字符串
我想我会坚持多久
filepath=Environ(“USERPROFILE”)和“\Desktop\”
'filepath=“C:\Users\Om3r\Desktop\FolderName\”
File=Dir(filepath)
调用sendaAttachment(文件)
端接头
函数sendaAttachment(文件为字符串)
Dim olApp作为对象的Outlook.Application
Dim olMsg作为对象“Outlook.MailItem”
将ATMT调整为对象的Outlook.Attachments
我想我会坚持多久
设置olApp=CreateObject(“Outlook.Application”)
设置olMsg=olApp.CreateItem(0)的电子邮件
设置Atmts=olMsg.Attachments
i=0
"发信息",
与奥尔姆格
当Len(文件)>0时执行
添加(文件路径和文件)
File=Dir
i=i+1
环
.展示
.Subject=“这是您想要的文件”
.To=”alias@domain.com"
.HTMLBody=“Hi”&olMsg.To&“,

我有附件文件” 以 MsgBox i&“已发送文件” 设置olMsg=Nothing 设置Atmts=无 端函数
确保将
filepath=Environ(“USERPROFILE”)&“\Desktop\FolderName\”
FolderName更新为正确的文件夹名称


您也可以使用
filepath=“C:\Users\Om3r\Desktop\FolderName\”
并确保更新Om3r和FolderName

请在访问此网站后编辑您的问题。向我们显示您的代码,并准确地告诉我们哪个部分不起作用。下一个代码是我收到0个文件,并且该文档不会在电子邮件中传输到microsoft outlook,编辑您的答案,并将您的代码以正确的格式粘贴到那里,以便我们可以在您的问题帖子中看到您的代码。您是否更新了
fldName=“C:\Users\”
?正在尝试将所有文件附加到一封电子邮件中?我在“Dim olApp As Outlook.Application”行中遇到错误,错误为未定义用户定义类型。您从何处运行代码?Excel或Outlook?感谢您的更新,但文件未附加
Option Explicit
Dim FilesPath As String
Sub SendFilesbuEmail()
    Dim File As String
    Dim i As Long

    FilesPath = Environ("USERPROFILE") & "\Desktop\"
    'FilesPath = "C:\Users\Om3r\Desktop\FolderName\"
    File = Dir(FilesPath)

    Call SendasAttachment(File)

End Sub

Function SendasAttachment(File As String)
    Dim olApp As Object ' Outlook.Application
    Dim olMsg As Object ' Outlook.MailItem
    Dim Atmts As Object ' Outlook.Attachments

    Dim i As Long

    Set olApp = CreateObject("Outlook.Application")
    Set olMsg = olApp.CreateItem(0) ' email
    Set Atmts = olMsg.Attachments
    i = 0

    ' send message
    With olMsg

        Do While Len(File) > 0
            Atmts.Add (FilesPath & File)
            File = Dir
            i = i + 1
        Loop
        .Display
        .Subject = "Here's that file you wanted"
        .To = "alias@domain.com"
        .HTMLBody = "Hi " & olMsg.To & ", <br /><br /> I hav attch Files"
    End With

    MsgBox i & " Files were sent"

    Set olMsg = Nothing
    Set Atmts = Nothing


End Function