Rebuilding the NickName Cache file via VB Macro.
Posted on 2008-11-19
We all have those users who believe that when they type out a name into their little "To:" bar they are in fact adding contacts to their outlook, not realizing that this feature merely indulges their laziness and makes it hell on us I.T. folk anytime someone corrupts their ost/pst, or a system hard drive crash, or new computer is deployed.
So I've gotten used to copying the nickname cache file, and moving it from profile to profile to keep my users happy.
A hard drive crash, on an old notebook that had never been backed up to the nearest server. Fortunately all of their mail was on the server (ost)
But a nickname cache with names in the thousands (surprised it hasn't crashed already?) was lost, and I have an angery harpy of a VP snarling down my neck about her 'contacts'. Please help.
I found a vbs macro which claims to pull the email addresses out of the sent items folder, then create a new email with all of those emails in the "To: " field as addressees. This would repopulate a new cache file, and solve my problem.
Unfortunately I am a coding idiot, well that, and the script in question doesn't appear to be functional.
Can anyone look over this and find the flaws?
Public Sub GetSentItemsAddresses()
Dim olApp As New Outlook.Application
Dim myNamespace As NameSpace
Dim oSentItems As Outlook.MAPIFolder
Dim oEmail As Outlook.MailItem
Dim oNewEmail As Outlook.MailItem
Dim oToString As String
Dim oRecipientName As Outlook.Recipient
Dim oEmailSubject As String
oEmailSubject = "Build Nickname Emails"
If MsgBox("Cancel now if you are not working offline (otherwise you will send a blank email to everyone you have ever sent en email to).", vbOKCancel, "Confirm you are Offline") = vbCancel Then Exit Sub
Set myNamespace = olApp.GetNamespace("MAPI")
Set oSentItems = myNamespace.GetDefaultFolder(olFolderSentMail)
For Each oEmail In oSentItems.Items
For Each oRecipientName In oEmail.Recipients
If InStr(1, oRecipientName.Address, "@") <> 0 And InStr(1, oToString, oRecipientName.Address) = 0 Then
oToString = oToString + oRecipientName.Address + ";"
' send a new email (which will save the addresses in the nickname file)
Set oNewEmail = olApp.CreateItem(olMailItem)
oNewEmail.To = oToString
oNewEmail.Subject = oEmailSubject
MsgBox "Please delete the email named " + oEmailSubject + " from the Outbox"