0

I want to select files within Windows Explorer and then by pressing a shortcut (assigned to a VBS-script) to send these files with Outlook (2010).

I found two working code snippets:

Code snippet1 (Creating Email):

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

Code snippet2 (returning paths of the selected files in Windows Explorer):

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"

How to combine these code snippets to achieve my purpose? I tried to give the SelectedItem.Path a variable to add it to the objMailItem.Attachments.Add but it is not working.

I tried the cdo approach but this issue seems to be more complex. I have an office365-account and the configuration settings seems to differ from VBScript to send email without running Outlook.

Community
  • 1
  • 1
Rinu
  • 9
  • 4
  • Why not use [tag:cdo.message], it's way more flexible and doesn't require an instance of Outlook to operate? – user692942 Dec 17 '19 at 08:58
  • Hi Lankymart! Thank you for helping but my problem is not to start Outlook (OL is running all the time). My problem is: I have a shared folder (where I got my pdf-invoices) on my drive and I have to monitor this folder and in some cases I have to send them to an email adress.. what I could do is to use the windows right-click context menu for sending an email but this is too much awkward (at least 30 times a day).. The code snippets above are working fine separately but I can't combine them for my purpose. – Rinu Dec 17 '19 at 10:23
  • All I was saying is you don't need to rely on Outlook to send an email programmatically, so if you are having problems combining the two, cut out Outlook altogether and incorporate `CDO.Message` into your existing script. – user692942 Dec 17 '19 at 11:18
  • Ok now I understand what you mean.. ok I will give it a shot and then I will present my results here.. – Rinu Dec 17 '19 at 11:22
  • I tried to use cdo-approach but this issue seems to be even more complex.. I have an office365-account and I'm not sure about the configuration settings.. The configuration settings seems to differ from the standard (link above: VBScript to send email without running Outlook).. Lankymart thank you anyway I will try to solve it maybe completely different.. – Rinu Dec 17 '19 at 13:07

1 Answers1

0

Yesss I got it working and it is very cool, I love it :-)

  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
Rinu
  • 9
  • 4