【问题标题】:Move all Items from a PST file从 PST 文件中移动所有项目
【发布时间】: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 中的任何办公内容。有谁知道日历条目是否会作为其中的一部分被复制。

我们是否需要指定例如,获取所有邮件并移动,然后获取所有联系人并移动,获取所有日历条目并移动?

【问题讨论】:

    标签: vba outlook


    【解决方案1】:

    “无法正常工作”您没有描述问题,但这里有一些建议。

    创建文件夹时添加一行设置objDestFolder。

    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
    

    或者总是尝试在主收件箱中创建 Imported 文件夹

    ' Bypass the error if the folder exists
    On Error Resume Next
    Set objDestFolder = objInbox.Folders.add("Imported")
    On Error GoTo 0
    Set objDestFolder = objInbox.Folders("Imported")
    

    用类似的东西替换 While Wend。

    For i = PSTInboxItemsCount To 1 Step -1
        objPSTInboxItems(i).Move objDestFolder
    Next i
    

    【讨论】:

    • 感谢 niton 的快速响应
    【解决方案2】:

    搞定了

    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
    
    
    ' Run the sub
    sbImportPST ("d:\backup.pst")
    
    
    Sub sbImportPST (strPSTLocalPath)
        ' Add the PST to Outlook
        objNamespace.AddStore (strPSTLocalPath)
    
        ' 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 (strPSTLocalPath)
    
        ' Get the mail items in the top level - in most cases this will not be needed as mails will be in the "inbox" folder under this folder
        Set objPSTInbox = objOutlook.Session.Folders("PSTImport")
        Set objPSTInboxItems = objPSTInbox.Items
        PSTInboxItemsCount = objPSTInboxItems.count
        ' Step through all items just discovered and move to Imported Folder
        For i = PSTInboxItemsCount To 1 Step -1
            objPSTInboxItems(i).Move objDestFolder
        Next 
    
        ' Step through all subfolders of the PST (this wilkl include the folder "calendar" and "contacts" and "Inbox") and move the folder.
        Set oFolders = objPSTInbox.Folders 
        For i = oFolders.Count To 1 Step -1 
            oFolders.Item(i).MoveTo  objDestFolder
        Next 
    
        ' Remove the PST file from Outlook
        objNamespace.RemoveStore objPST
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-29
      • 1970-01-01
      • 2017-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-13
      • 1970-01-01
      相关资源
      最近更新 更多