Option Explicit
'*** Constants'
Const CB_APPNAME = "CategoryProcessor"
Const CB_VERSION = "1.0"
Const ForReading = 1
'*** Class Variables'
Private bolInitialized As Boolean
Private intCount As Integer
Private objFSO As Object
Private objFile As Object
Private olkCategory As Object
Private strDefaultFilename As String
Private Sub Class_Initialize()
Dim arrVersion As Variant
arrVersion = Split(Outlook.Application.VERSION, ".")
If arrVersion(0) < 12 Then
MsgBox "This object only works with Outlook 2007 and higher.", vbCritical + vbOKOnly, CB_APPNAME
Else
Set objFSO = CreateObject("Scripting.FileSystemObject")
strDefaultFilename = Environ("USERPROFILE") & "\My Documents\Outlook Categories.txt"
bolInitialized = True
End If
End Sub
Private Sub Class_Terminate()
Set objFSO = Nothing
End Sub
Public Sub Export(Optional strFilename As String)
Dim strFileUsed As String
If bolInitialized Then
intCount = 0
strFileUsed = IIf(strFilename = "", strDefaultFilename, strFilename)
Set objFile = objFSO.CreateTextFile(strFileUsed, True)
For Each olkCategory In Outlook.Session.Categories
objFile.WriteLine olkCategory.Name & "," & olkCategory.Color & "," & olkCategory.ShortcutKey
intCount = intCount + 1
Next
objFile.Close
End If
MsgBox "Exported " & intCount & " categories to " & vbCrLf & strFileUsed, vbInformation + vbOKOnly, CB_APPNAME & " - Export"
End Sub
Public Sub Import(Optional strFilename As String)
Dim strBuffer As String, strFileUsed As String, arrValues As Variant, olkCategory As Object, intRead As Integer
On Error Resume Next
If bolInitialized Then
intCount = 0
intRead = 0
strFileUsed = IIf(strFilename = "", strDefaultFilename, strFilename)
If objFSO.FileExists(strFileUsed) Then
Set objFile = objFSO.OpenTextFile(strFileUsed, ForReading)
Do Until objFile.AtEndOfStream
strBuffer = objFile.ReadLine
arrValues = Split(strBuffer, ",")
Set olkCategory = Outlook.Session.Categories.Item(arrValues(0))
If TypeName(olkCategory) = "Nothing" Then
Outlook.Session.Categories.Add arrValues(0), arrValues(1), arrValues(2)
intCount = intCount + 1
End If
Set olkCategory = Nothing
intRead = intRead + 1
Loop
objFile.Close
MsgBox "Imported " & intCount & " of " & intRead & " categories read from " & vbCrLf & strFileUsed, vbInformation + vbOKOnly, CB_APPNAME & " - Import"
Else
MsgBox "The file " & strFileUsed & " does not exist. Import aborted.", vbCritical + vbOKOnly, CB_APPNAME & " - Import"
End If
End If
On Error GoTo 0
End Sub
Sub CategoriesExport()
Dim objCatProcessor As New CategoryProcessor
With objCatProcessor
.Export InputBox("Enter the name of the file, including the path, you want to export to.", "Get Export Filename")
End With
Set objCatProcessor = Nothing
End Sub
Sub CategoriesImport()
Dim objCatProcessor As New CategoryProcessor
With objCatProcessor
.Import InputBox("Enter the name of the file, including the path, you want to import from.", "Get Import Filename")
End With
Set objCatProcessor = Nothing
End Sub
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (32)
Author
Commented:Commented:
Author
Commented:Commented:
Author
Commented:View More