【发布时间】:2016-09-19 13:08:42
【问题描述】:
它最终到了我不得不寻求帮助的地步。
由于电子邮件服务器的空间限制,我们公司通常将邮件/日历等从 Outlook 备份到 PST 文件。
我们现在在电子邮件服务器上不再有空间限制来防止这种情况发生,因此我们希望将 PST 文件中的所有信息都放入用户邮箱。
最终我们希望运行一个 vbscript 或类似的程序来搜索用户的本地驱动器,发现任何 PST 文件,然后将所有数据传输到名为“Imported”的文件夹下的交换邮箱,然后删除 PST。
理想情况下,我们只需通过 PShell 直接在 Exchange 中执行此操作,而无需用户,但由于大多数用户有“许多”PST 文件,其中大多数不是必需的,如果我们全部完成它们会填满我们的交换。
我根本不知道 Outlook VBA,所以这是我需要帮助的唯一部分。我花了一段时间在搜索结果中寻找自己的方式,希望看到我可以让它工作,但不能让它工作。
我对此进行了几次不同的尝试。这是我当前的代码:
' Get the main Inbox folder
Const OLInbox = 6 'Inbox Items folder
Set objOutlook = CreateObject( "Outlook.Application" )
Set objNameSpace = objOutlook.GetNamespace( "MAPI" )
Set objInbox = objNameSpace.GetDefaultFolder( OLInbox ) 'sets objFolder to the Inbox for it's reference
' Create the Imported folder in the main inbox
On Error Resume Next
Set objDestFolder = objInbox.Folders( "Imported" )
If Err.Number <> 0 Then
Set objNewFolder = objInbox.Folders.Add("Imported")
End If
On Error Goto 0
' Add the PST to Outlook
objNamespace.AddStore ("d:\backup.pst")
' Select the new store
Set objPST = objNamespace.Folders.GetLast
' Rename the Store To be easier To use
objPST.Name = "PSTImport"
' disconnect and reconnect the store to force a refresh of the folder list
objNamespace.RemoveStore objPST
objNamespace.AddStore ("d:\backup.pst")
Set objPSTInbox = objOutlook.Session.Folders("PSTImport").Folders("Inbox")
'Set objPSTFolder = objNameSpace.Folders("PSTImport").Folders("Inbox")
Set objPSTItems = objPSTInbox.Items
While TypeName(objPSTItems) <> "Nothing"
objPSTItems.Move objDestFolder
Set objPSTItems = objPSTItems.FindNext
Wend
目前完整的脚本如下所示
Set objShell = WScript.CreateObject ("WScript.Shell")
' Get the main Inbox folder
Const OLInbox = 6 'Inbox Items folder
Set objOutlook = CreateObject( "Outlook.Application" )
Set objNameSpace = objOutlook.GetNamespace( "MAPI" )
Set objInbox = objNameSpace.GetDefaultFolder( OLInbox ) 'sets objFolder to the Inbox for it's reference
' Create the Imported folder in the main inbox
On Error Resume Next
Set objDestFolder = objInbox.Folders("Imported")
If Err.Number <> 0 Then
Set objNewFolder = objInbox.Folders.Add("Imported")
Set objDestFolder = objInbox.Folders("Imported")
End If
On Error Goto 0
' Add the PST to Outlook
objNamespace.AddStore ("d:\backup.pst")
' Select the new store
Set objPST = objNamespace.Folders.GetLast
' Rename the Store To be easier To use
objPST.Name = "PSTImport"
' disconnect and reconnect the store to force a refresh of the folder list
objNamespace.RemoveStore objPST
objNamespace.AddStore ("d:\backup.pst")
Set objPSTInbox = objOutlook.Session.Folders("PSTImport").Folders("Inbox")
Set objPSTInboxItems = objPSTInbox.Items
PSTInboxItemsCount = objPSTInboxItems.count
For i = PSTInboxItemsCount To 1 Step -1
objPSTInboxItems(i).Move objDestFolder
Next
经测试,在收件箱中成功创建了Imported文件夹。
PST 被添加为商店,重命名也可以正常工作。
但是,脚本的循环/下一部分似乎失败了。没有项目移至 Imported 文件夹。
我想我们可能没有选择邮箱中的项目。我们是否需要在其中指定另一个“folders()”部分?
理想情况下,我们希望移动 PST 中的任何办公内容。有谁知道日历条目是否会作为其中的一部分被复制。
我们是否需要指定例如,获取所有邮件并移动,然后获取所有联系人并移动,获取所有日历条目并移动?
【问题讨论】: