Note that there are some explanatory texts on larger screens.

plurals
  1. POMove PST files to server via VB
    primarykey
    data
    text
    <p>At work we've picked up a new exchange server, so my boss was going to have me go around to all our computers and manually move all the open PST files people had to their folder on the new server. I, for obvious reasons, decided that it would be simpler to script this. After a bit of research I came across one such script that only needed a bit of tweaking (found here <a href="http://halfloaded.com/blog/logon-script-move-local-pst-files-to-network-share/" rel="nofollow">http://halfloaded.com/blog/logon-script-move-local-pst-files-to-network-share/</a>) but had a lot of other things I wouldn't really need (checks for if it was running on a laptop, only affecting local folders, etc.), so I cannibalized the main logic out of it into my own version without most of these sanity checks. The problem I'm running into is that I have 2 seemingly identical loops iterating a different number of times, and it causes problems. Here's what I have</p> <pre><code>Option Explicit Const OverwriteExisting = True ' get username, will use later Dim WshNetwork: Set WshNetwork = wscript.CreateObject("WScript.Network") Dim user: user = LCase(WshNetwork.UserName) Set WshNetwork = Nothing ' network path to write pst files to Dim strNetworkPath : strNetworkPath = "\\server\folder\" 'Fix network path if forgot to include trailing slash... If Not Right(strNetworkPath,1) = "\" Then strNetworkPath = strNetworkPath &amp; "\" End If ' initiate variables and instantiate objects Dim objOutlook, objNS, objFolder, objFSO, objFName, objTextFile, pstFiles, pstName, strPath Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.CreateTextFile("c:\My\Desktop\pst_script_log.txt " , True) Set objOutlook = CreateObject("Outlook.Application") Set objNS = objOutlook.GetNamespace("MAPI") Dim count : count = -1 ' Enumerate PST filesand build arrays objTextFile.Write("Enumerating PST files" &amp; vbCrLf) For Each objFolder in objNS.Folders If GetPSTPath(objFolder.StoreID) &lt;&gt; "" Then pstFiles = GetPSTPath(objFolder.StoreID) pstName = objFolder.Name count = count + 1 objTextFile.Write(count &amp; " " &amp; pstFiles &amp; vbCrLf) ReDim Preserve arrNames(count) arrNames(count) = pstName ReDim Preserve arrPaths(count) arrPaths(count) = pstFiles objOutlook.Session.RemoveStore objFolder End IF Next ' closes the outlook session objOutlook.Session.Logoff objOutlook.Quit Set objOutlook = Nothing Set objNS = Nothing ' quits if no pst files were found If count &lt; 0 Then wscript.echo "No PST Files Found." wscript.Quit End If objTextFile.Write("moving them" &amp; vbCrLf) ' moves the found pst files to the new location Dim pstPath For Each pstPath In arrPaths On Error Resume Next objTextFile.Write(pstPath &amp; vbCrLf) objFSO.MoveFile pstPath, strNetworkPath If Err.Number &lt;&gt; 0 Then wscript.sleep 5000 objFSO.MoveFile pstPath, strNetworkPath End If Err.Clear On Error GoTo 0 Next Set objFSO = Nothing ' sleep shouldn't be necessary, but was having issues believed to be related to latency wscript.sleep 5000 'Re-open outlook Set objOutlook = CreateObject("Outlook.Application") Set objNS = objOutlook.GetNamespace("MAPI") 'Re-map Outlook folders For Each pstPath In arrPaths objTextFile.Write("Remapping " &amp; pstPath &amp; " to " &amp; strNetworkPath &amp; Mid(pstPath, InStrRev(pstPath, "\") + 1) &amp; vbCrLf) objNS.AddStore strNetworkPath &amp; Mid(pstPath, InStrRev(pstPath, "\") + 1) Next count = -1 For Each objFolder In objNS.Folders If GetPSTPath(objFolder.StoreID) &lt;&gt; "" Then count = count + 1 objTextFile.Write("Renaming " &amp; GetPSTPath(objFolder.StoreID) &amp; " to " &amp; arrNames(count) &amp; vbCrLf) objFolder.Name = arrNames(count) End If Next objOutlook.Session.Logoff objOutlook.Quit objTextFile.Write("Closing Outlook instance and unmapping obj references...") Set objFolder = Nothing Set objTextFile = Nothing Set objOutlook = Nothing Set objNS = Nothing wscript.echo "Done." wscript.Quit Private Function GetPSTPath(byVal input) 'Will return the path of all PST files ' Took Function from: http://www.vistax64.com/vb-script/ Dim i, strSubString, strPath For i = 1 To Len(input) Step 2 strSubString = Mid(input,i,2) If Not strSubString = "00" Then strPath = strPath &amp; ChrW("&amp;H" &amp; strSubString) End If Next Select Case True Case InStr(strPath,":\") &gt; 0 GetPSTPath = Mid(strPath,InStr(strPath,":\")-1) Case InStr(strPath,"\\") &gt; 0 GetPSTPath = Mid(strPath,InStr(strPath,"\\")) End Select End Function </code></pre> <p>The offending loop is at lines 24 and 81. The specific error is that count gets incremented more in the second loop than the first, however that's because the first loop is coming up short on its iterations and missing the last PST file. People with similar issues on the site where I found most of this code said that adding in wscript.sleep functions in certain spots helped them, but I've had no such luck in their recommended spots, and I get the impression that their issues are not the same as mine.</p> <p>I'd greatly appreciate help with what's going wrong in my code, and I'm open to suggestions for ways to correct other issues I don't see, and think there's a better way to do something like this.</p> <p>EDI:After doing some more research on my issue, it seems that by performing RemoveStore inside the loop at line 24 I'm changing the value of objNS.Folders (which makes sense), and to avoid this I should store the objFolder items I need to remove and do so in another loop. Problem now is that I don't know how to do that, I've tried</p> <pre><code> [line 35] ReDim Preserve arrFolders(count) arrFolders(count) = objFolder End If Next For Each objFolder in arrFolders objOutlook.Session.RemoveStore objFolder Next </code></pre> <p>However I get Type Mismatch errors regarding RemoveStore, so I think it isn't storing the object how it needs to. Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload