?
Solved

VBS script to add 2 PSt files into Outlook - 99% complete ?

Posted on 2011-04-28
4
Medium Priority
?
1,866 Views
Last Modified: 2012-05-11
I have a text file called MYPst.txt stored on a network drive which shows the file paths for the PST files, there are 2 PST files.  One is on a H drive & the other is the default archive pst found in C:\Documents and Settings\some user\Local Settings\Application Data\Microsoft\Outlook\archive.pst.

The idea is that the script will open the text file & read the file locations in then Pop out locations in a message box, which it does ok.

It should then add the PST files to Outlook, it adds the first PST file ok but then errors on adding the second (Line 24; Char1) (Error Outlook failed to add the personal store to this session) Code 80004005

Can anyone help please ?


Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFile = objFSO.OpenTextFile("H:\MyPst.txt", ForReading) 
 
Const ForReading = 1 
 
Dim arrFileLines() 
i = 0 
Do Until objFile.AtEndOfStream 
Redim Preserve arrFileLines(i) 
arrFileLines(i) = objFile.ReadLine 
i = i + 1 
Loop 
objFile.Close 
 
'Then you can iterate it like this 
 
For Each strLine in arrFileLines 
WScript.Echo strLine 

Set objOutlook = CreateObject("Outlook.Application")
Set objNameSpace = objOutlook.GetNamespace("MAPI")
'Set oShell = CreateObject("WScript.Shell")
'oShell.Run "outlook.exe", 1, False
objNameSpace.AddStore strLine


Next

Open in new window

0
Comment
Question by:coch
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
rlandquist earned 750 total points
ID: 35483621
You are looping for the 2 lines and trying to open outlook twice, etc.  Try this:
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFile = objFSO.OpenTextFile("H:\MyPst.txt", ForReading) 
 
Const ForReading = 1 
 
Dim arrFileLines() 
i = 0 
Do Until objFile.AtEndOfStream 
Redim Preserve arrFileLines(i) 
arrFileLines(i) = objFile.ReadLine 
i = i + 1 
Loop 
objFile.Close 
 
'Then you can iterate it like this 
 Set objOutlook = CreateObject("Outlook.Application")
Set objNameSpace = objOutlook.GetNamespace("MAPI")
'Set oShell = CreateObject("WScript.Shell")
'oShell.Run "outlook.exe", 1, False

For Each strLine in arrFileLines 
WScript.Echo strLine 

objNameSpace.AddStore strLine


Next

Open in new window

0
 
LVL 6

Assisted Solution

by:Russell Lucas
Russell Lucas earned 750 total points
ID: 35483628
You can try the following:-

 
For Each strLine in arrFileLines 
WScript.Echo strLine 

Set objOutlook = CreateObject("Outlook.Application")
Set objNameSpace = objOutlook.GetNamespace("MAPI")
objNameSpace.AddStore strLine
Set objOutlook = Nothing
Set objNameSpace = Nothing

Next

Open in new window


It maybe because the objects are not being unloaded before the next iteration.
0
 

Author Comment

by:coch
ID: 35483814
My bad! Working on Vmworkstation and the snapshot that it ws running on did not have the achive.pst created hence it errored as it didnt exist!

Apologies
0
 

Author Closing Comment

by:coch
ID: 35695981
Points awarded for assistance although I have not tested the solutions
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By default Outlook 2016 displays only one time zone in the Calendar. The following article explains how to display two time zones in one calendar view.
Today as you open your Outlook, you witness an error message: “Outlook is using an old copy of your Outlook Data File…”. Probably, Outlook is accessing an old OST file.
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question