使用 VBScript 通过电子邮件发送选定的文件

Send selected files via Email using VBScript

我想在 Windows 资源管理器中 select 文件,然后按快捷方式(分配给 VBS 脚本)使用 Outlook (2010) 发送这些文件。

我找到了两个有效的代码片段:

代码片段 1(创建电子邮件):

Dim objOutl
Set objOutl = CreateObject("Outlook.Application")
Set objMailItem = objOutl.CreateItem(olMailItem)

'comment the next line if you do not want to see the outlook window
objMailItem.Display
strEmailAddr  = "test@test.com"
objMailItem.Recipients.Add strEmailAddr
objMailItem.Body = "Hi, this is the body.."
objMailItem.Attachments.Add "C:\test.txt"

'objMailItem.Send

Set objMailItem = nothing
Set objOutl = nothing

代码片段 2(在 Windows 资源管理器中返回 selected 文件的路径):

Function GetSelectedFiles() 'Returns paths as array of strings

    Dim FileList, Window, SelectedItem
    'avoid duplicates by storing paths in dictionary keys
    Set FileList = CreateObject("Scripting.Dictionary")

    With CreateObject("Shell.Application")
        For Each Window In .Windows
            'skip IE Windows
            If InStr(1, Window.FullName, "iexplore.exe", vbTextCompare) = 0 Then
                For Each SelectedItem In Window.Document.SelectedItems
                    FileList(SelectedItem.Path) = Null
            'MsgBox SelectedItem.Path
                Next
            End If
        Next
    End With

    GetSelectedFiles = FileList.Keys 'array of paths
End Function



MsgBox  "Click OK after selecting the items", vbOKOnly Or vbInformation, "Select a few items"

Dim SelectedFiles
SelectedFiles =  GetSelectedFiles

MsgBox  "You selected: " & vbNewLine  & vbNewLine & Join(SelectedFiles, vbNewLine), vbOKOnly Or vbInformation, "Selected Items"

如何组合这些代码片段来达到我的目的?我试图给 SelectedItem.Path 一个变量以将其添加到 objMailItem.Attachments.Add 但它不起作用。

我尝试了 cdo 方法,但这个问题似乎更复杂。我有一个 office365 帐户,配置设置似乎与 VBScript to send email without running Outlook.

不同

是的,我让它工作了,它非常酷,我喜欢它:-)

  Dim x ,objOutl ,objMailItem ,strEmailAddr  

  Set objOutl = CreateObject("Outlook.Application")
  Set objMailItem = objOutl.CreateItem(olMailItem)

  'comment the next line if you do not want to see the outlook window
  objMailItem.Display
  strEmailAddr  = "test@test.com"
  objMailItem.Recipients.Add strEmailAddr
  objMailItem.Subject = "Test"
  objMailItem.Body = "Hi, this is the body.."
  'in the next line it will jump in to function "GetSelectedFiles"
  x=GetSelectedFiles   

'comment out the next three lines for sending directly..
'objMailItem.Send
'Set objMailItem = nothing
'Set objOutl = nothing


Function GetSelectedFiles() 'Returns paths as array of strings
Dim FileList, Window, SelectedItem
'avoid duplicates by storing paths in dictionary keys
Set FileList = CreateObject("Scripting.Dictionary")

 With CreateObject("Shell.Application")
    For Each Window In .Windows
        'skip IE Windows
        If InStr(1, Window.FullName, "iexplore.exe", vbTextCompare) = 0 Then
            For Each SelectedItem In Window.Document.SelectedItems
                FileList(SelectedItem.Path) = Null
                x = SelectedItem.Path

               'next line is just for debugging..
               'msgBox x
               'The next line was the solution
               objMailItem.Attachments.Add x
        Next
       End If
    Next
 End With

GetSelectedFiles = x 'array of paths
End Function