Vba 从PST文件中移动所有项目
终于到了我必须寻求帮助的地步 由于电子邮件服务器的空间限制,我们公司的常见做法是将邮件/日历等从outlook备份到PST文件 我们现在不再有在电子邮件服务器上阻止此操作的空间限制,因此我们希望将PST文件中的所有信息输入用户邮箱 最终,我们希望运行vbscript或类似程序,搜索用户的本地驱动器,发现任何PST文件,然后将所有数据传输到名为“Imported”的文件夹下的exchange邮箱,然后删除PST 理想情况下,我们只需通过PShell直接进行交换,而无需用户,但由于大多数用户都有“许多”PST文件,其中大多数文件都不是必需的,如果我们全部完成,则会填满我们的交换 我根本不知道Outlook VBA,所以这是我唯一需要帮助的部分。我花了一段时间在搜索结果中努力工作,希望看到我可以让它工作,但无法让它工作 我在这方面做了几次不同的尝试。这是我的当前代码:Vba 从PST文件中移动所有项目,vba,outlook,Vba,Outlook,终于到了我必须寻求帮助的地步 由于电子邮件服务器的空间限制,我们公司的常见做法是将邮件/日历等从outlook备份到PST文件 我们现在不再有在电子邮件服务器上阻止此操作的空间限制,因此我们希望将PST文件中的所有信息输入用户邮箱 最终,我们希望运行vbscript或类似程序,搜索用户的本地驱动器,发现任何PST文件,然后将所有数据传输到名为“Imported”的文件夹下的exchange邮箱,然后删除PST 理想情况下,我们只需通过PShell直接进行交换,而无需用户,但由于大多数用户都有“许
' 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
”获取主收件箱文件夹
Const OLInbox=6'收件箱项目文件夹
设置objOutlook=CreateObject(“Outlook.Application”)
设置objNameSpace=objOutlook.GetNamespace(“MAPI”)
Set-objInbox=objNameSpace.GetDefaultFolder(OLInbox)'将objFolder设置到收件箱中以供参考
'在主收件箱中创建导入的文件夹
出错时继续下一步
设置objDestFolder=objInbox.Folders(“导入”)
如果错误号为0,则
设置objNewFolder=objInbox.Folders.Add(“导入”)
如果结束
错误转到0
'将PST添加到Outlook
objNamespace.AddStore(“d:\backup.pst”)
'选择新商店
设置objPST=objNamespace.Folders.GetLast
'重命名存储以便于使用
objPST.Name=“pstmimport”
'断开并重新连接存储以强制刷新文件夹列表
objNamespace.RemoveStore objPST
objNamespace.AddStore(“d:\backup.pst”)
设置objPSTInbox=objOutlook.Session.Folders(“PSTImport”).Folders(“收件箱”)
'设置objPSTFolder=objNameSpace.Folders(“PSTImport”).Folders(“收件箱”)
设置objPSTItems=objPSTInbox.Items
而TypeName(objPSTItems)“无”
移动objDestFolder
设置objPSTItems=objPSTItems.FindNext
温德
目前,完整的脚本如下所示
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
Set objShell=WScript.CreateObject(“WScript.Shell”)
'获取主收件箱文件夹
Const OLInbox=6'收件箱项目文件夹
设置objOutlook=CreateObject(“Outlook.Application”)
设置objNameSpace=objOutlook.GetNamespace(“MAPI”)
Set-objInbox=objNameSpace.GetDefaultFolder(OLInbox)'将objFolder设置到收件箱中以供参考
'在主收件箱中创建导入的文件夹
出错时继续下一步
设置objDestFolder=objInbox.Folders(“导入”)
如果错误号为0,则
设置objNewFolder=objInbox.Folders.Add(“导入”)
设置objDestFolder=objInbox.Folders(“导入”)
如果结束
错误转到0
'将PST添加到Outlook
objNamespace.AddStore(“d:\backup.pst”)
'选择新商店
设置objPST=objNamespace.Folders.GetLast
'重命名存储以便于使用
objPST.Name=“pstmimport”
'断开并重新连接存储以强制刷新文件夹列表
objNamespace.RemoveStore objPST
objNamespace.AddStore(“d:\backup.pst”)
设置objPSTInbox=objOutlook.Session.Folders(“PSTImport”).Folders(“收件箱”)
设置objPSTInboxItems=objPSTInbox.Items
pstinboxitemscont=objPSTInboxItems.count
对于i=PStinboxitemScont至1步骤-1
objPSTInboxItems(i).移动objDestFolder
下一个
测试完成后,将在收件箱中成功创建导入的文件夹
PST被添加为存储,重命名也可以正常工作
然而,似乎是纸条的循环/下一部分失败了。未将任何项目移到导入的文件夹中
我想我们可能没有选择邮箱中的项目。我们是否需要在其中指定另一个“folders()”部分
理想情况下,我们希望移动PST中的任何office内容。有人知道日历条目是否会被复制过来作为这项工作的一部分吗
我们是否需要指定,例如,获取所有邮件并移动,然后获取所有联系人并移动,获取所有日历条目并移动?“无法使其工作”您尚未描述问题,但这里有一些建议
创建文件夹时,添加一行设置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
用这样的东西替换While Wend
For i = PSTInboxItemsCount To 1 Step -1
objPSTInboxItems(i).Move objDestFolder
Next i
开始工作了
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
Set objShell=WScript.CreateObject(“WScript.Shell”)
'获取主收件箱文件夹
Const OLInbox=6'收件箱项目文件夹
设置objOutlook=CreateObject(“Outlook.Application”)
设置objNameSpace=objOutlook.GetNamespace(“MAPI”)
Set-objInbox=objNameSpace.GetDefaultFolder(OLInbox)'将objFolder设置到收件箱中以供参考
'在主收件箱中创建导入的文件夹
出错时继续下一步
设置objDestFolder=objInbox.Folders(“导入”)
如果错误号为0,则
设置objNewFolder=objInbox.Folders.Add(“导入”)
设置objDestFolder=objInbox.Folders(“导入”)
如果结束
错误转到0
“开潜艇
sbImportPST(“d:\backup.pst”)
子sbImportPST(strPSTLocalPath)
'将PST添加到Outlook
objNamespace.AddStore(strPSTLocalPath)
'选择新商店
设置objPST=objNamespace.Folders.GetLast
'重命名存储以便于使用
objPST.Name=“pstmimport”
'断开并重新连接存储以强制刷新文件夹列表
objNamespace.RemoveStore objPST
objNamespace.AddStore(strPSTLocalPath)
'获取顶级邮件-在大多数情况下,这将不需要,因为邮件将位于此文件夹下的“收件箱”文件夹中
设置objPSTInbox=objOutlook.Session.Folders(“PSTImport”)
设置objPSTInboxItems=objPSTInbox.Items
pstinboxitemscont=objPSTInboxItems.count
'单步浏览刚刚发现的所有项目并移动到导入的文件夹
对于i=PStinboxitemScont至1 Ste
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