【问题标题】:Send selected files via Email using VBScript [duplicate]使用 VBScript 通过电子邮件发送选定的文件 [重复]
【发布时间】:2019-12-16 17:23:45
【问题描述】:

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

我找到了两个工作代码sn-ps:

代码 sn-p1(创建电子邮件):

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

代码 sn-p2(在 Windows 资源管理器中返回所选文件的路径):

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"

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

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

【问题讨论】:

  • 为什么不使用cdo.message,它更灵活并且不需要Outlook实例来操作?
  • 嗨 Lankymart!感谢您的帮助,但我的问题不是启动 Outlook(OL 一直在运行)。我的问题是:我的驱动器上有一个共享文件夹(我的 pdf 发票),我必须监视这个文件夹,在某些情况下我必须将它们发送到电子邮件地址。我能做的就是使用用于发送电子邮件的 Windows 右键单击​​上下文菜单,但这太尴尬了(每天至少 30 次).. 上面的代码 sn-ps 单独工作正常,但我无法将它们组合起来用于我的目的。跨度>
  • 我的意思是您不需要依靠 Outlook 以编程方式发送电子邮件,因此如果您在将两者结合使用时遇到问题,请完全删除 Outlook 并将 CDO.Message 合并到您现有的脚本。
  • 好的,现在我明白你的意思了.. 好的,我会试一试,然后我会在这里展示我的结果..
  • 我尝试使用 cdo-approach 但这个问题似乎更复杂.. 我有一个 office365 帐户,我不确定配置设置.. 配置设置似乎不同从标准(上面的链接:VBScript 在不运行 Outlook 的情况下发送电子邮件).. Lankymart 谢谢你无论如何我会尝试解决它可能完全不同..

标签: windows shell file vbscript outlook


【解决方案1】:

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

  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

【讨论】:

    猜你喜欢
    • 2015-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-06
    • 1970-01-01
    • 1970-01-01
    • 2015-11-07
    • 1970-01-01
    相关资源
    最近更新 更多