选择存储在数组中的文件名并通过电子邮件 VBA 附加这些文件

问题描述 投票:0回答:1

我正在尝试选择名称存储在以逗号分隔的单元格中的文件,并通过电子邮件附加这些文件。下面是我正在处理的代码,但我不断收到运行时错误 438。

我正在努力找出问题所在,您的专业知识将帮助我解决问题

Private Sub test3()
    Dim mess_body As String, StrFile As String, StrPath As String
    Dim appOutLook As Object
    Dim MailOutLook As Object
    Dim i As Long
    Dim Elem As Variant
    Dim myArr As Variant

    
    
    Set appOutLook = CreateObject("Outlook.Application")
    Set MailOutLook = appOutLook.CreateItem(olMailItem)

    StrPath = Sheets("Input").Range("E3").Value
    
    With MailOutLook
        .To = "[email protected]"
        .Subject = "test"
        .HTMLBody = "test"
        
        
        With Worksheets("DL")
    For i = 2 To .Cells(.Rows.Count, "B").End(xlUp).Row
        myArr = Split(.Range("B" & i).Value, ",")
     

       
        For Each Elem In myArr
               
           StrFile = Dir(StrPath & "\" & Elem & ".xlsx")
'
            Do While Len(StrFile) > 0
            

            .attachments.Add StrPath & "\" & StrFile
            StrFile = Dir
        Loop
        
        Next Elem

    Next i
End With

'
        .Display
    End With
    
    
End Sub
excel vba email outlook
1个回答
0
投票

代码中有两个嵌套的

With
运算符:

  With MailOutLook
        .To = "[email protected]"
        .Subject = "test"
        .HTMLBody = "test"
        
        
        With Worksheets("DL")

Worksheet
没有
Attachments
属性。您需要在嵌套
With
运算符中显式使用源对象。

MailOutLook.attachments.Add StrPath & "\" & StrFile

最后,确保将有效的文件路径传递给

Add
类的
Attachments
方法。附件的来源可以是文件(由带有文件名的完整文件系统路径表示)或构成附件的 Outlook 项目。

© www.soinside.com 2019 - 2024. All rights reserved.