Solved

WSH Open Outlook with .attachment (Zip-File)

Posted on 2014-03-24
9
583 Views
Last Modified: 2014-03-25
Hello Experts,

I've a simple wsh/vbs Script which doesn't work. In "Code-Example 1" Outlook doesn't start so I think there must be a problem with the DFile.

My Script create a Zip Archiv and I want to send this Archiv via Outlook respectively open Outlook with the right attachment.

DFile is the 'guilty' item (i.g. DFile = C:\Verschlüsselung\10000\Archiv-10000.Zip"

' Code-Example 1
...
ScriptPath = Left(WScript.ScriptFullName, InStrRev(WScript.ScriptFullName, "\"))
sStr=Split(ScriptPath,"\")
FolderN=sStr(UBound(sStr)-1)
DFile = ScriptPath & "Archiv-" & FolderN & "-" & ".Zip"
...
Function Send2Outlook (DFile) 
Set objOutl = CreateObject("Outlook.Application")
Set objMailItem = objOutl.CreateItem(olMailItem)
Set attachment = objMailItem.Attachments
attachment.Add DFile, olByValue, 4, "Great"
objMailItem.Display
...
End Function

Open in new window


If I use a simple Filename in attachment.Add it works.

 
' Code-Example 1
...
Function Send2Outlook () 
Set objOutl = CreateObject("Outlook.Application")
Set objMailItem = objOutl.CreateItem(olMailItem)
Set attachment = objMailItem.Attachments
attachment.Add "C:\123456\123456.txt", olByValue, 4, "Great"
objMailItem.Display
End Function
...

Open in new window


Why doesn't DFile work in Attachment.Add Statement? It's an $ which the absolute Filename and Filepath.
I use ByRef and ByVal, delimiter chr(34) but in "Code-Example 1" Outlook doesn't start?

Best Regards.
reredok
0
Comment
Question by:reredok
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 69

Expert Comment

by:Qlemo
ID: 39950048
Maybe it is because you append another dash ("-") to the filename? Your created zip file is searched as C:\Verschlüsselung\10000\Archiv-10000-.Zip, and probably does not exist.

It might be generated the same, but that is something we cannot see here.
0
 
LVL 4

Author Comment

by:reredok
ID: 39950118
The file exists.
The same file can be send via Outlook manually.
I added the pathname/filename hard-coded in script and it works fine.
I presume that Outlook doesn't start because there must be a diffence between "C:\123456\123456.txt" and "C:\Verschlüsselung\10000\Archiv-10000.Zip" regarding the Outlook.Attachments Statement.
Further I used the Filesystemobject in Function call ... with no success.
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39950174
To clarify: You used "C:\Verschlüsselung\10000\Archiv-10000-.Zip" (with the trailing dash) hardcoded, and it worked?
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 4

Author Comment

by:reredok
ID: 39950263
Yes.
C:\Verschlüsselung\10000\Archiv-10000.Zip
not
C:\Verschlüsselung\10000\Archiv-10000-.Zip
sry

the vbs code create a Zip Archiv with winrar:
i.e.
Zip-Archiv: Archiv-10001-2014-03-24_115211.Zip
Syntax: a custom Nummer 10001
Date: 2014-03-24
TimeStamp: 115211
Extension: Zip
Result: C:\Verschlüsselung\Archiv-10001-2014-03-24_115211.Zip

works:
attachment.Add "C:\Verschlüsselung\Archiv-10001-2014-03-24_115211.Zip", olByValue, 4, "Great"

works not:
attachment.Add DFile, olByValue, 4, "Great"
where DFile = "C:\Verschlüsselung\Archiv-10001-2014-03-24_115211.Zip"

works not:
attachment.Add chr(34) & DFile & chr(34), olByValue, 4, "Great"

What's wrong with DFile???
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39950504
It's best to debug that in Outlook VBA, and that is what I will try to do now.

Edit: Works in VBA, as expected. I get the "Der Pfad ist nicht vorhanden" error for non-existing files, and the file attached in a new mail if it exists, with this simple VBA code:
Public Sub atttest()
Dim DFile As String, objMailItem As MailItem
  ' DFile = "C:\Verschlüsselung\Archiv-10001-2014-03-24_115211.Zip"
  DFile = "C:\temp\ee\1.txt"
  Set objMailItem = CreateItem(olMailItem)
  objMailItem.Attachments.Add DFile, olByValue, 4, "Great"
  objMailItem.Display
End Sub

Open in new window

What's your default eMail type? HTML, Text or RTF? (The latter has some special features, and should NOT be used).
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39950568
Same with VBS:
Option Explicit

const olMailItem = 0
const olByValue = 1

Dim DFile, objOutlook, objMailItem

' DFile = "C:\Verschlüsselung\Archiv-10001-2014-03-24_115211.Zip"
DFile = "C:\temp\ee\1.txt"
set objOutlook = CreateObject("Outlook.Application")
Set objMailItem = objOutlook.CreateItem(olMailItem)
objMailItem.Attachments.Add DFile, olByValue, 4, "Great"
objMailItem.Display

Open in new window

So it must be something else, or RTF.
0
 
LVL 4

Author Comment

by:reredok
ID: 39950595
it's wsh vbs code which is controlled by a "file or more file drag 'n' drop" Event. Debug is not so easy and it's impossible for my opinion in Outlook VBA.
I don't understand why
CreateObject("Outlook.Application").CreateItem(olMailItem).Attachments deals so different between a $ "C:\Verschlüsselung\Archiv-10001-2014-03-24_115211.Zip" and a variant DFile
Which kind of Input is needed? In ms documentation it must be a variant
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39950687
"It must be a variant" cannot be true. In almost all cases the real type of the var is what you need. The file parameter can be a string or an Outlook object (hence the var type of variant is allowed here). For other types the Attachment.Add should result in an error message.

First step to disect the culprit is to do exactly what I did for test - use a very limited test case and code. If that works, you can try to integrate that into some more code of you, and so on.

BTW, if you write
DFile = "C:\Verschlüsselung\Archiv-10001-2014-03-24_115211.Zip"

Open in new window

that isn't a variant anymore, but a string. Keep in mind you can't use (force) types for vars in VBS.

On another note, why are you providing the "4" as position? I would expect to have a 1 here, as you did not fill in anything in the mail body yet.
0
 
LVL 4

Author Closing Comment

by:reredok
ID: 39952836
I rebuild my Code and found a Problem with DFile which was only declared in a vbs function and not in called code. So the scope of this var maybe not well defined... maybe outlook had same problems with this fact. So I <Dim DFile> in "Main-Code" and transmit it up to every called function so to say as dummy
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question