I'm trying to develop a macro to ultimately create a new category in Outlook 2007 if the desired category doesn't already exist. I got some sample code from the help in the VBA area of Outlook and from the internet. Here is what I have right now:
Private Sub ListCategoryIDs()
Dim objNameSpace As NameSpace
Dim objCategory As Category
Dim objCategoryForAdd As Category
Dim newCategory As Categories
Dim strOutput As String
Dim mynewcat
' Obtain a NameSpace object reference.
Set objNameSpace = Application.GetNamespace("MAPI")
' Check if the Categories collection for the Namespace
' contains one or more Category objects.
If objNameSpace.Categories.Count > 0 Then
' Enumerate the Categories collection.
For Each objCategory In objNameSpace.Categories
' Add the name and ID of the Category object to
' the output string.
strOutput = strOutput & objCategory.Name & _
": " & objCategory.CategoryID & vbCrLf
End If
' Display the output string.
MsgBox strOutput
If strOutput Like "*HasXComments*" Then
MsgBox ("category exists")
MsgBox ("category does not exist")
newCategory.Add "HasXComments"
End If
' Clean up.
Set objCategory = Nothing
Set objNameSpace = Nothing
End Sub
When I run it it tells me the existing categories, then answers correctly whether or not the desired category exists. If it doesn't exist then it tries to create it using the line
newCategory.Add "HasXComments"
That line always causes a problem. I've tried it with and without parenthesis, using "set", and countless other ways of typing this line. The line is exactly what is in Outlook's help. After typing the period I choose "Add" from the list. After hitting
a space it prompts me to enter the name, color, and then shortcut key. That seems to tell me that I'm using an acceptable Outlook format for the command.
On the internet I found a couple of things saying that a new category can't be created with an Outlook macro because the categories aren't in Outlook, they are in the registry. I've searched the registry, but didn't find any existing category.
Thank you so much for any help you can provide!
Harassment is any behavior intended to disturb or upset a person or group of people. Threats include any threat of suicide, violence, or harm to another.
Any content of an adult theme or inappropriate to a community web site.
Any image, link, or discussion of nudity.
Any behavior that is insulting, rude, vulgar, desecrating, or showing disrespect.
Any behavior that appears to violate End user license agreements, including providing product keys or links to pirated software.
Unsolicited bulk mail or bulk advertising.
Any link to or advocacy of virus, spyware, malware, or phishing sites.
Any other inappropriate content or behavior as defined by the Terms of Use or Code of Conduct.
Any image, link, or discussion related to child pornography, child nudity, or other child abuse or exploitation.
The error tells you that the variable isn't set to anything. Set it to Application.Session.Categories
Outlook Add-Ins & VBA Macros
www.vboffice.net
Harassment is any behavior intended to disturb or upset a person or group of people. Threats include any threat of suicide, violence, or harm to another.
Any content of an adult theme or inappropriate to a community web site.
Any image, link, or discussion of nudity.
Any behavior that is insulting, rude, vulgar, desecrating, or showing disrespect.
Any behavior that appears to violate End user license agreements, including providing product keys or links to pirated software.
Unsolicited bulk mail or bulk advertising.
Any link to or advocacy of virus, spyware, malware, or phishing sites.
Any other inappropriate content or behavior as defined by the Terms of Use or Code of Conduct.
Any image, link, or discussion related to child pornography, child nudity, or other child abuse or exploitation.