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

Saving Outlook Attachments

139 views
Skip to first unread message

Swingline_Stapler

unread,
Mar 12, 2009, 10:16:06 AM3/12/09
to
Sorry guys, I got another question!

I'm trying to save attachments from outlook and have bumped around in the
dark to create the following code in PSv1:

$outlook = new-object -com Outlook.Application
$outlook.Session.GetDefaultFolder(6)
$attachments = $inbox.items |% {$_.attachments}
$attachments |% {$_.saveasfile("C:\TEMP")}

In the console I get the error: "Exception calling "SaveAsFile" with "1"
argument(s): "Cannot save the attachment. You don't have appropriate
permission
to perform this operation."

PowerGUI Script Editor error: "You cannot call a method on a null-valued
xpression.
At :line:4 char:30
+ $attachments |% {$_.saveasfile <<<< ("C:\TEMP\")}"

I'm confused as clearly the functions are listed in the memberclasses for
each... and I actually see the attachments from my emails in $attachments...
yet it isn't panning out. I can't find anyone else out there with examples
for this which I think many would find useful.

Have any of you done this or have any input for a solution?

Kiron

unread,
Mar 12, 2009, 2:48:11 PM3/12/09
to
You need to pass a file name, not just a destination directory to SaveAsFile.
The attachment has a FileName property.

This code groups attachments with same name and saves the first attachment in each group, if the attachment is corrupt or if it can't save it the Trap handles the exception; in this sample it just writes a message.
You could also iterate through each group's attachments and append a different number to the name in order to save all.

$outlook = new-object -com Outlook.Application
$inbox = $outlook.Session.GetDefaultFolder(6)
foreach ($group in $inbox.items |% {$_.attachments} | group filename) {
trap {
Write-Host There was a problem saving $fName
continue
}
$fName = "C:\TEMP\$($group.Name)"
$group.Group[0].saveasfile($fName)
if ($?) {Write-Host $fName was saved succesfuly.}
}

--
Kiron

Swingline_Stapler

unread,
Mar 12, 2009, 4:08:01 PM3/12/09
to
Thank you, Kiron! Works great!

Swingline_Stapler

unread,
Mar 12, 2009, 4:28:01 PM3/12/09
to
I have another question if you don't mind...

Do you know if it's possible to open outlook up through powershell using
different profiles? they don't have to run at the same time, just kind of
like how outlook prompts you when you select which profile you want to use.
Was just curious.

You're awesome, Kiron! :)

OldDog

unread,
Mar 12, 2009, 4:55:49 PM3/12/09
to
On Mar 12, 3:28 pm, Swingline_Stapler
> > Kiron- Hide quoted text -
>
> - Show quoted text -

And while you are at it. Can you tell me why this works if I open
powershell as User, but not as Administrator?

Kiron

unread,
Mar 12, 2009, 7:56:47 PM3/12/09
to
@Swingline_Stapler
Glad to help! You can by calling the Session's/Namespace's Logon method whose signature is:

Logon(Profile, Password, ShowDialog, NewSession)
http://msdn.microsoft.com/en-us/library/bb219914.aspx

# only one session can be opened at a time
$ol = new-object -c outlook.application
# if you want the prompt pass these values to Session.Logon()
$name, $password, $showDialog, $newSession =
'','',$true,$false
# for a specific profile pass these
# $name, $password, $showDialog, $newSession =
# '<profile name>','',$false,$false
$ol.session.Logon($name, $password, $showDialog, $newSession)
$ol.session.GetDefaultFolder(6).Display()
sleep 3
$ol.session.Logoff()
$ol.Quit()
# next line may not be necessary in your system
[Runtime.InteropServices.Marshal]::releaseComObject($ol)

@OldDog
Don't know. I'm on Vista SP1, Outlook v12.0.0.6316, running PS v2 CTP3 as Administrator and works fine. except that Quit() is not enough and got to use Runtime.InteropServices.Marshal's ReleaseComObject Static Method.

--
Kiron

Josh Einstein

unread,
Mar 12, 2009, 8:37:46 PM3/12/09
to
Run As is not the same as UAC in Vista. When you Run As in XP, the
application literally believes it is running under another user's profile.
So that means all the MAPI profiles you've got set up under your user
account are not present under the administrators account. In Vista, when you
elevate a process, it still runs as you but with more permissions.

"OldDog" <mike...@comcast.net> wrote in message
news:95d770e2-40d9-421a...@41g2000yqf.googlegroups.com...

0 new messages