Excel 如何将文件附加到邮件?

Excel 如何将文件附加到邮件?,excel,vba,outlook,Excel,Vba,Outlook,我需要附加目录中的文件。我尝试了多个命令 outlook消息保存在文件夹中,但没有附件,我需要附加它 这是我代码的一部分 Dim文件名,文件为字符串 Dim文件夹,路径为字符串 filename=folderpath&“\sheets\” file=Dir(文件名&x&“*.xlsx”) 作为对象的Dim ObjMsg '创建电子邮件 出错时继续下一步 设置olapp=GetObject(,“Outlook.Application”) 如果错误号=429,则 设置olapp=CreateObje

我需要附加目录中的文件。我尝试了多个命令

outlook消息保存在文件夹中,但没有附件,我需要附加它

这是我代码的一部分

Dim文件名,文件为字符串
Dim文件夹,路径为字符串
filename=folderpath&“\sheets\”
file=Dir(文件名&x&“*.xlsx”)
作为对象的Dim ObjMsg
'创建电子邮件
出错时继续下一步
设置olapp=GetObject(,“Outlook.Application”)
如果错误号=429,则
设置olapp=CreateObject(“Outlook.application”)
多芬特
如果结束
错误转到0
设置ObjMsg=olapp.CreateItem(olMailItem)
多芬特
ObjMsg.To=“”
ObjMsg.CC=“”
ObjMsg.Subject=“”
ObjMsg.BodyFormat=3'设置richtext格式
Set editor=ObjMsg.GetInspector.WordEditor
editor.Content.Paste
wd.Quit SaveChanges:=False
而Len(文件)>0
ObjMsg.Attachments.Add(文件名和文件名)
file=Dir
温德
这是代码:

Dim objFolder, objShell
On Error Resume Next
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder(0, "select the folder where the files are located " ,0)
If IsObject(objFolder) Then SelectFolder = objFolder.Self.Path
Set objFolder = Nothing
Set objShell = Nothing
On Error GoTo 0

我也尝试了下面的命令,但没有任何效果。您可以查找
filename&x&“*.xlsx”
但附加
filename&file
。什么是
x
Dim fso, objFiles As Object
Dim folder As folder
Dim fil As file
Dim ObjMsg As Object
Dim file As String

strPath = getFunction

Set fso = CreateObject("scripting.filesystemobject")
Set folder = fso.GetFolder(strPath)
Set objFiles = fso.GetFolder(strPath).Files

For Each fil In folder.Files
    file = strPath & "\" & ".xlsx"
    'Create email
    On Error Resume Next
    Set olapp = GetObject(, "Outlook.Application")
    If Err.Number = 429 Then
    Set olapp = CreateObject("Outlook.application")
    DoEvents
    End If
    On Error GoTo 0
    
    Set ObjMsg = olapp.CreateItem(olMailItem)
    DoEvents
     
      ObjMsg.To = ""
      ObjMsg.CC = ""
      ObjMsg.Subject = ""
      
      ObjMsg.BodyFormat = 3 'Set richtext format
      Set editor = ObjMsg.GetInspector.WordEditor
      editor.Content.Paste
      wd.Quit SaveChanges:=False
      While Len(file) > 0
        ObjMsg.Attachments.Add (file)
        file = Dir
      Wend
Next