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

printing the attachments on email in Outlook from a PowerShell script

749 views
Skip to first unread message

Larry__Weiss

unread,
Apr 18, 2010, 6:42:17 PM4/18/10
to

I need help writing a PowerShell script to automate printing
the attachments to email in Outlook.

- Larry

Marco Shaw [MVP]

unread,
Apr 19, 2010, 8:42:19 AM4/19/10
to
I frequent a few forums... Did you (or someone else) already ask this
question a few months ago?

You may have a problem automating the printing of the attachments, but you
can definitely save them all to a common directory. I'm not sure if you can
select multiple documents of different formats and easily right-click to
print them all.

Are the attachments all the same format/document type?

Marco

"Larry__Weiss" <l...@airmail.net> wrote in message
news:#Cc7mh03...@TK2MSFTNGP02.phx.gbl...

Larry__Weiss

unread,
Apr 19, 2010, 10:05:57 AM4/19/10
to
I'm assisting a friend of mine with the creation of this script,
and I have discovered some previous threads, but nothing that ties
the whole thing together.

The attached file format will be PDF. The email client is Outlook.

I want to use this as a real-world example of the use of PowerShell to
demonstrate at the NTPCUG PowerShell SIG.

- Larry

rkd

unread,
Apr 20, 2010, 7:33:01 AM4/20/10
to
Are you a bit familiar with the outlook object model? using it could bring
the solution. Let me know. I could try to create a code snippet. Do you use
Outlook as an Exchange-Client oder pop/imap-client?

Reinhard

"Larry__Weiss" wrote:

> .
>

Larry__Weiss

unread,
Apr 20, 2010, 9:51:04 AM4/20/10
to
It is the pop based client.

- Larry

rkd

unread,
Apr 21, 2010, 3:56:01 AM4/21/10
to
Hi Larry, please try this once:

This example assumes you have a subfolder 'Pdf' under your inbox folder
where the e-mails with the pdf-attachments reside and you have a folder
C:\pdf in your filesystem.
Outlookversion: 2002 (hope other/higher versions support this code too)

$ol = New-Object -Com Outlook.Application
$ns = $ol.GetNameSpace('MAPI')
$olinbox = 6
$inbox = $ns.GetDefaultFolder( $olInbox )
$olPdfFolder = $inbox.Folders.Item('pdf')
$olPdfFolder.Items | %{
foreach ($attachment in $_.Attachments)
{
$attachment.SaveAsFile( "C:\pdf\$($attachment.FileName)")
}
}

$shell = New -com Shell.Application
$shell.Namespace('C:\pdf').Items() | %{ $_.InvokeVerb('Print') }

hope this meets your needs and you get success.
Reinhard

PS: you can also transfer the code above in just 2 lines. the first one does
the outlook job, the second one does the shell job. pretty cool what we can
do with psh.
"Larry__Weiss" wrote:

> .
>

Larry__Weiss

unread,
Apr 21, 2010, 5:46:40 PM4/21/10
to
Thanks! I'll test it and give you some feedback.
Do you have a reference for the Outlook object model that you can recommend?

- Larry

Marco Shaw [MVP]

unread,
Apr 21, 2010, 7:38:29 PM4/21/10
to
> $shell = New -com Shell.Application
> $shell.Namespace('C:\pdf').Items() | %{ $_.InvokeVerb('Print') }

Very interesting approach! My suggestions would have been to use COM with
Microsoft Word to print the PDFs, but that would likely be more resource
intensive.

Marco

rkd

unread,
Apr 22, 2010, 4:33:02 AM4/22/10
to

I used the online help of my outlook installation, topic 'forms and
programming informations'.
But it can be that the programming part of the online help is part of an
optional installation topic, so a standard installation doesn't provide such
a help topic
Reinhard

"Larry__Weiss" wrote:

> .
>

rkd

unread,
Apr 22, 2010, 4:44:01 AM4/22/10
to
Yes you're very right, Marco. the shell.application Com-object offers a few
very interessting things. I neglected it for a long time. Only in the last
time i took my focus more on it and it seems that it payed out :-)
Reinhard

Larry__Weiss

unread,
May 15, 2010, 6:36:02 PM5/15/10
to
Thanks again for the PowerShell code.

We used it as an example to demonstrate PowerShell usage at
the North Texas PC Users Group PowerShell SIG in Dallas
this morning. The only thing we had to change was
the next to last line where we modified it to be

$shell = New-Object -com Shell.Application

- Larry


On 4/21/2010 2:56 AM, rkd wrote:

> Hi Larry, please try this once:
>
> This example assumes you have a subfolder 'Pdf' under your inbox folder
> where the e-mails with the pdf-attachments reside and you have a folder
> C:\pdf in your filesystem.
> Outlookversion: 2002 (hope other/higher versions support this code too)
>
> $ol = New-Object -Com Outlook.Application
> $ns = $ol.GetNameSpace('MAPI')
> $olinbox = 6
> $inbox = $ns.GetDefaultFolder( $olInbox )
> $olPdfFolder = $inbox.Folders.Item('pdf')
> $olPdfFolder.Items | %{
> foreach ($attachment in $_.Attachments)
> {
> $attachment.SaveAsFile( "C:\pdf\$($attachment.FileName)")
> }
> }
>
> $shell = New -com Shell.Application
> $shell.Namespace('C:\pdf').Items() | %{ $_.InvokeVerb('Print') }
>
> hope this meets your needs and you get success.
> Reinhard
>
> PS: you can also transfer the code above in just 2 lines. the first one does
> the outlook job, the second one does the shell job. pretty cool what we can
> do with psh.
>
> "Larry__Weiss" wrote:
>> It is the pop based client.
>>
>>

Larry__Weiss

unread,
May 25, 2010, 8:32:54 PM5/25/10
to

Now that I have a start on this script, I'd like to add more smarts to
it (like moving the emails to another folder after saving the
attachments so the next time I execute it, it won't re-print any
previous attachments).

Is there a good book out there that explains the Outlook object model?
And if there is a revision of a book that shows PowerShell examples of
using the Outlook object model that would be ideal.

- Larry

0 new messages