【问题标题】:How to attach a file to a message?如何将文件附加到消息?
【发布时间】:2020-10-16 11:39:07
【问题描述】:

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

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

这是我的代码的一部分。

Dim filename, file As String
Dim folder, path As String

filename = folderpath & "\sheets\"
file = Dir(filename & x & "*.xlsx")

Dim ObjMsg As Object
 
'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 (filename & file)
    file = Dir
Wend

【问题讨论】:

  • 我也尝试了以下命令,但没有任何效果。
  • 您寻找filename & x & "*.xlsx",但附加filename & filex 是什么?

标签: excel vba outlook


【解决方案1】:
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

【讨论】:

    【解决方案2】:

    这是代码:

    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
    

    【讨论】:

      猜你喜欢
      • 2013-01-01
      • 2017-05-07
      • 2020-11-08
      • 2016-07-29
      • 2019-12-14
      • 1970-01-01
      • 1970-01-01
      • 2013-03-31
      • 1970-01-01
      相关资源
      最近更新 更多