So there I was, minding my own business, when a Project came my way. One of the things the Project manager wanted was a survey to be mailed to all the Business Unit Contacts. The survey was an Excel Spreadsheet with about 10 questions on it. Easy enough, you say. He then wanted the answers collected in a master spreadsheet for his perusal. As he was far too busy to collect these himself, especially when he realized that there was going to be over 300 of them, I was tasked with this part of the project. Bare in mind that all the surveys were comming back as email attachments and they all had the same name. Naturally, I wrote a script that would do the following:
1. Open Outlook and let me select the Folder that I was collecting the surveys in.
(Got to love those Outlook rules!)
2. Save the *.xlsx attachments in my c:temp directory.
3. Open my master spreadsheet and the survey response, copy the answers and paste them into a new column in the Master.
Here is the script I came up with:
# ============================================================================================== # # Microsoft PowerShell Source File -- Created with SAPIEN Technologies PrimalScript 2009 # # NAME: # # AUTHOR: The OldDog # DATE : 8/10/2010 # # COMMENT: # # ============================================================================================== # <-- Script -------------------------> # This part gets the file attachments $o = New-Object -comobject outlook.application $n = $o.GetNamespace("MAPI") $f = $n.PickFolder() $filepath = "c:\temp" $f.Items| foreach { $SendName = $_.SenderName $_.attachments|foreach { Write-Host $_.filename $a = $_.filename If ($a.Contains("xlsx")) { $_.saveasfile((Join-Path $filepath "$SendName.xlsx")) } } } # This part opens Excel and does the copy/paste magic. $xl = New-Object -comobject Excel.Application $xl.Visible = $true $xl.DisplayAlerts = $False $wb = $xl.Workbooks.Open("C:\ProjectMaster.xlsx") dir C:temp*.xlsx | ForEach-Object { $FileName = $_.name $n = $FileName.tostring() $FullName = "c:\temp\$n" $wb1 = $xl.Workbooks.Open($FullName) $ws = $wb1.Worksheets.Item(1) $ws.Activate $ws.Range("C5:C23").Select() $ws.Range("C5:C23").Copy() $wb.Activate() $ws1 = $wb.Worksheets.Item("Survey Says") $mainRng = $ws1.UsedRange.Cells $ColCount = $mainRng.Columns.Count $xCol = $ColCount $xCol = $xCol + 1 Write-Host $xCol $ws1.Cells.Item(1,$xCol) = $n $ws1.Cells.Item(2,$xCol).Select() $ws1.Cells.Item(2,$xCol).PasteSpecial(-4163) $wb1.Close() } #<------ End Script ----------------------------------> # For finding Attachments in Outlook # This works as well $o = New-Object -comobject outlook.application $n = $o.GetNamespace("MAPI") $f = $n.PickFolder() foreach ($item in $f.items) { $sender = $item.SenderName foreach($attach in $item.Attachments) { if($attach.filename.contains("xlsx")) { write-host "$sender sent $($attach.filename)" } } }
Certainly I like your web site, however you need to test the spelling on several of your posts. Many of them are rife with spelling issues and I find it very bothersome to tell you. On the other hand I will certainly come again again!
I was recommended this website through my cousin. I’m not certain whether or not this submit is written via him as nobody else understand such particular about my difficulty. You’re amazing! Thanks!
I got what you mean, regards for posting. Woh I am glad to find this website through google.
I have been absent for a while, but now I remember why I used to love this site. Thank you, I will try and check back more frequently. How frequently you update your web site?
Great blog here! Also your web site loads up fast! What web host are you using? Can I get your affiliate link to your host? I wish my site loaded up as fast as yours lol
WordPress.com
It’s in reality a great and helpful piece of info. I am satisfied that you simply shared this helpful information with us. Please keep us informed like this. Thanks for sharing.
Not good format code, 😥
Suggestions:
Maybe useful donwload embedded images (inline images) too, and attachments, using powershell. From Outlook, and to from MSG files.