脚本会扫描outlook中所有账户下的所有邮件,将发件人添加至联系人,添加的信息只有发件人名称和email,email有重复的不会添加
用vba宏一样能实现,只是不喜欢vba语法和office的宏编辑器,还是用powershell实现吧
开发环境win10+office2010 64位,win7没有试过
效率还是有点慢的,好像是com组件就这速度,觉得有用的可能以后还会更新吧
第一次发代码,请大家拍砖
1 <# 2 # 解析outlook中所有的邮件,将发件人添加至联系人 3 #> 4 $ol = New-Object -ComObject outlook.application 5 $app = $ol.GetNamespace("MAPI") 6 $arrContact = New-Object System.Collections.ArrayList 7 $arrContactFolder = New-Object System.Collections.ArrayList 8 $folder = "" 9 10 <# 11 # 载入已存在于outlook中的联系人 12 #> 13 function loadContacts(){ 14 $ct = $app.GetDefaultFolder(10) 15 foreach($c in $ct.items){ 16 $data = @{email=$c.Email1Address} 17 $arrContactFolder.Add($data) > $null 18 } 19 } 20 21 <# 22 # 将联系人数据添加至待保存至outlook的数组 23 #> 24 function appendUnqEmail($sender){ 25 if(-not $sender.SenderEmailAddress){ 26 return 27 } 28 29 #是否存在于待添加的联系人数组 30 foreach($c in $arrContact){ 31 if($c.email.equals($sender.SenderEmailAddress)){ 32 return 33 } 34 } 35 36 #是否存在于outlook中原有联系人 37 foreach($c in $arrContactFolder){ 38 if($c.email.equals($sender.SenderEmailAddress)){ 39 return 40 } 41 } 42 43 $data = @{name=$sender.SenderName;email=$sender.SenderEmailAddress} 44 $arrContact.add($data) > $null 45 } 46 47 <# 48 # 扫描并添加所有文件夹中的联系人数据 49 #> 50 function addSenderData($parent) { 51 Write-Host("正在解析:" + $parent.FullFolderPath) 52 if($parent.Folders.count -gt 0){ 53 foreach($b in $parent.Folders){ 54 addSenderData($b) 55 } 56 } 57 58 $idx = 1 59 foreach($c in $parent.items){ 60 if($c.Class -eq 43){ 61 appendUnqEmail($c) 62 } 63 $_CurrentOperation = "正在解析" + ($idx++) + "/" + $parent.items.count 64 $_status = "文件夹:" + $parent.FullFolderPath 65 Write-Progress -Activity "解析邮件中"` 66 -CurrentOperation $_CurrentOperation ` 67 -status $_status 68 } 69 } 70 71 72 ######main###### 73 loadContacts 74 75 for($i=0; $i -lt $app.Folders.Count; $i++){ 76 addSenderData($app.Folders[$i+1]) 77 } 78 79 $folder = $app.GetDefaultFolder(10) 80 for($i = 0; $i -lt $arrContact.Count; $i++){ 81 $ct = $folder.items.Add("IPM.Contact.Project") 82 $ct.fullname = $arrContact[$i].name 83 $ct.Email1Address = $arrContact[$i].email 84 $ct.Save() 85 } 86 87 Write-Host("共添加了" + $i + "个联系人") 88 Read-Host
原文:https://www.cnblogs.com/liquidsnake9/p/outlook_addcontact.html