Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Printing an Attachment Automatically??

104 views
Skip to first unread message

Yonah Sudwerts

unread,
Nov 29, 2004, 1:52:25 PM11/29/04
to
Ok, Here goes:
I receive emails that have attachments in the form of TIFF files. I would
like to be able to:
1: Print out the said Tiff images. (Each email only has one file attached)
2: Mark the email as Read.
3: Move the email to a different folder.

I am aware that I could accomplish Items 2 & 3 using a simple rule in
Outlook. From what I have discovered I need to use VB to try and get 1 done.

Any ideas on what I can do. From what I can tell there is no Print method
for the Attachment property.
(I am going to just guess something here, that maube I need to open then Tif
file and use other types og Objects to do the prinitng)

I do have some VB scripting experience with Access. But I have never
scripted in Outlook.
I have started a script, just to try and play around, its in the Module that
is with the "This Outlook Session". Another thing I would need help with, is
how do I get it to run automatically when New Emails arrive. I see How I can
run the Macro manually, but not automatically.
Feel Free to speak in "Medium" level language.

Thanks,
Yoni


Ken Slovak - [MVP - Outlook]

unread,
Nov 30, 2004, 9:21:28 AM11/30/04
to
See http://www.slovaktech.com/code_samples.htm#StripAttachments for code to
strip attachments from selected emails and save them to the file system.
That is a required first step. Then you can use many different methods to
print the saved attachment files. One option is to use a Win32 API call to
ShellExecute with the path of the file and the verb "print", assuming you
have the "print" verb defined for TIFF files.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Absolute Beginner's Guide to Microsoft Office Outlook 2003
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm


"Yonah Sudwerts" <massu...@yahoo.com> wrote in message
news:41ab6fa0$0$3075$9a6e...@unlimited.newshosting.com...

Yonah Sudwerts

unread,
Nov 30, 2004, 3:03:12 PM11/30/04
to
A I said, I kind of figured out that I would have to do some non-Outlook
scripting. Can you give me an idea of where to look for help on the second
part of the job? (a differnt newsgroup maybe).

Thank YOu Very Much,
Yoni

"Ken Slovak - [MVP - Outlook]" <kens...@mvps.org> wrote in message
news:uFTeHmu1...@TK2MSFTNGP15.phx.gbl...

Ken Slovak - [MVP - Outlook]

unread,
Dec 1, 2004, 9:33:36 AM12/1/04
to
Well, the MSDN library has information on ShellExecute. It's pretty simple.
First open Windows Explorer and in the File Types tab select TIFF. Click on
the Advanced button. Make sure a "print" verb is defined. Then this code
would do it for you:

Private Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

ShellExecute(0&, "print", strPath, 0&, 0&, 0&)

Where strPath is the path to the file you want to print.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Absolute Beginner's Guide to Microsoft Office Outlook 2003
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm


"Yonah Sudwerts" <massu...@yahoo.com> wrote in message

news:41acd17c$0$27439$9a6e...@unlimited.newshosting.com...

Yonah Sudwerts

unread,
Dec 1, 2004, 3:33:55 PM12/1/04
to
I kind of get it, I need to use the "Save Attachments" and then run this.
My question is, where do I put this to get it to run automatically?

(For that matter, How can I get this entire thing to run automatically on
new email, or so I have to use it manually through the Macro Menu)

It turns out I am a bit of a n00b, I do unserstand the script when looking
at it, but I don't get how to make it run.

Thanks for your help,
Yoni

"Ken Slovak - [MVP - Outlook]" <kens...@mvps.org> wrote in message

news:Oz$aKO71E...@TK2MSFTNGP10.phx.gbl...

Yonah Sudwerts

unread,
Dec 1, 2004, 4:39:06 PM12/1/04
to
Ok, I am lost:
I found this code to save the Attached Files:


Sub SaveAttachment()
Dim myItems, myItem, myAttachments, myAttachment As Object
Dim myOrt As String
Dim myOlApp As New Outlook.Application
Dim myOlExp As Outlook.Explorer
Dim myOlSel As Outlook.Selection
'Set destination folder
myOrt = "C:\"

On Error Resume Next
'work on selected items
Set myOlExp = myOlApp.ActiveExplorer
Set myOlSel = myOlExp.Selection
'for all items do...
For Each myItem In myOlSel
'point on attachments
Set myAttachments = myItem.Attachments
If myAttachments.Count > 0 Then
'for all attachments do...
For i = 1 To myAttachments.Count
'save them to destination
myAttachments(i).SaveAsFile myOrt &
myAttachments(i).DisplayName
Next i
myItem.Save
End If
Next
Set myItems = Nothing
Set myItem = Nothing
Set myAttachments = Nothing
Set myAttachment = Nothing
Set myOlApp = Nothing
Set myOlExp = Nothing
Set myOlSel = Nothing
End Sub

I understand most of what is going on. Problem 1 is that it only seems to
work on a selected email, not on an entire Folder.
Second problem is how would I implement the Entire Second stage, I am a bit
off on that part. Sorry.

(Last but not last, How would I automate it to run when new email comes in)
I would also add in to the end to send all processed (that fit the
description) emails to another folder.

Am I asking too much?

Thanks,
Yoni


Yonah Sudwerts

unread,
Dec 2, 2004, 6:44:15 AM12/2/04
to
Since my previous posts, I have gotten somewhere.
I have a working Procedure that Scans the Folder that I want, and Saves all
Attachments to my folder.
Now, from what I understand from what you have shown me here. I need to
declare the function, and then once its declared, I can just add the "
ShellExecute(0&, "print", strPath, 0&, 0&, 0&)" line right into my procedure
right after the "objAttachments.Item(i).SaveAsFile strFile" line.
The problem that I have right now is that the Function does not seem to be
declaring right. Did you make a mistake at all? I am getting the "Compile
Error: Only Comments may appear after End Sub, End Function or End Property"
msgbox.

I will copy what I have so far:

>>>Sub Save_Attachments_TESTER_Folder()
Dim objMsg As Object
Dim objAttachments As Outlook.Attachments
Dim i As Long
Dim lngCount As Long
Dim strFile As String
Dim strFolder As String
Dim MyFolder
On Error Resume Next
'Set myOlExp = Application.ActiveExplorer.CurrentFolder
'Set objSelection = Application.ActiveExplorer.Selection
Set MyFolder =
Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Folders("TESTER")
strFolder = "c:\"
For Each objMsg In MyFolder.Items
'For Each objMsg In objSelection
Set objAttachments = objMsg.Attachments
lngCount = objAttachments.Count
If lngCount > 0 Then
For i = lngCount To 1 Step -1
strFile = strFolder & objAttachments.Item(i).FileName
objAttachments.Item(i).SaveAsFile strFile
Next i
End If
objMsg.Save
Next
ExitSub:
Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
End Sub>>>>>

I used a few different sources to help me get to this.
I have a few other, Outlook specific Questions, which I will put in a new
Post, since others mioght be able to help me as well.

Thanks,

Yoni


"Ken Slovak - [MVP - Outlook]" <kens...@mvps.org> wrote in message

news:Oz$aKO71E...@TK2MSFTNGP10.phx.gbl...

saul kaye

unread,
Dec 2, 2004, 10:50:01 AM12/2/04
to
Thanks Ken, Can you please try and look at the post before this one, as it
was my most recent one. I am sorry for driving you crazy.
Yoni

"Yonah Sudwerts" <massu...@yahoo.com> wrote in message

news:41aeff93$0$15785$9a6e...@unlimited.newshosting.com...

Ken Slovak - [MVP - Outlook]

unread,
Dec 2, 2004, 9:31:46 AM12/2/04
to
What I would do is handle the ItemAdd event for the Inbox folder's Items
collection. That will fire for every new email that comes into the Inbox.
You can see how to hook up the ItemAdd event at
http://www.outlookcode.com/d/code/zaphtml.htm#cw.

The declaration for the ShellExecute function should be at module level,
before any Subs or Functions are declared. In the ItemAdd handler put the
code to save the attachments. Use Application instead of declaring an
Outlook.Application object.

ItemAdd passes you an Item argument, check for Item.Class = olMail to make
sure it's an email. Then check for Item.Attachments.Count > 0. Then run the
attachment stripping code if True in a loop to get each attachment. After
saving each attachment you can check to see if it has a .TIFF extension. If
so then use the file path to call ShellExecute.

The other code you cited looks at Explorer.Selection, so of course it would
handle selected items in the current Explorer (folder view).

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Absolute Beginner's Guide to Microsoft Office Outlook 2003
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm


"Yonah Sudwerts" <massu...@yahoo.com> wrote in message

news:41ae29ea$0$11989$9a6e...@unlimited.newshosting.com...

Ken Slovak - [MVP - Outlook]

unread,
Dec 3, 2004, 11:04:57 AM12/3/04
to
The code you posted with your attachment extraction loop looks OK. I would
just add in the loop this:

For i = lngCount To 1 Step -1
strFile = strFolder & objAttachments.Item(i).FileName
objAttachments.Item(i).SaveAsFile strFile

ShellExecute 0&, "print", strFile, 0&, 0&, 0&
Next i

The declaration for ShellExecute should be at the module level, above and
subs or functions in that code or class module as follows:

Private Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

'Then this:
Sub Save_Attachments_TESTER_Folder()
'and so on

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Absolute Beginner's Guide to Microsoft Office Outlook 2003
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm


"saul kaye" <pha...@netvision.net.il> wrote in message
news:41af3931$0$15741$9a6e...@unlimited.newshosting.com...

0 new messages