Replace one category with another

L

lynxwoman

I have the book Microsoft Outlook Programming by Sue Mosher. I have
written the macro to replace one category with another.

I'm having trouble getting it to work. I use a custom for that requires
at least 2 categories to be selected (also from Sue Mosher).

Here's what my code looks like:
Sub ReplaceCat()
Dim objApp As Outlook.Application
Dim objFolder As Outlook.MAPIFolder
Dim objItem As Object
Dim arr() As String
Dim strOldCat As String
Dim strNewCat As String
Dim I As Integer
strOldCat = " "
strNewCat = "RTP Office"
Set objApp = CreateObject("Outlook.Application")
Set objFolder = objApp.ActiveExporer.CurrentFolder
For Each objItem In objFolder.Items
arr() = Split(objItem.Categories, ",")
For I = 0 To UBound(arr)
If arr(I) = strOldCat Then
arr(I) = strNewCat
Exit For
End If
Next
objItem.Categories = Join(arr, ",")
'objItem.Save
Next
Set objApp = Nothing
Set objFolder = Nothing
Set objItem = Nothing
End Sub

Any words to the unwise are appreciated

Thank you so much,
Sandy
 
L

lynxwoman

I want the macro to run only on a specific contact folder,
how do I ensure the change will only happen in a specific
folder?

sandy
 
S

Sue Mosher [MVP-Outlook]

Look at how the code determines what folder it's going to work on:

Set objFolder = objApp.ActiveExporer.CurrentFolder

This is the folder that the user currently has displayed. If you want to
code in which folder and it's not a default folder, then you need to walk
the folder hierarchy using the Folders collections or use a function that
does that for you. See http://www.outlookcode.com/d/code/getfolder.htm

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
L

lynxwoman

Thanks Sue. When I run the VBScript to change the category it doesn't
change anything.
Example: strOldCat = "_1A Interiors"
strNewCat = "1A Interior Architecture"

When I view the categories, none of the _1A Interiors have been changed.
Any suggestions?
thanks

Subject: Re: ALSO
From: "Sue Mosher [MVP-Outlook]" <[email protected]> Sent:
5/4/2004 12:57:55 PM


Look at how the code determines what folder it's going to work on:

Set objFolder = objApp.ActiveExplorer.CurrentFolder

This is the folder that the user currently has displayed. If you want to
code in which folder and it's not a default folder, then you need to
walk
the folder hierarchy using the Folders collections or use a function
that
does that for you. See http://www.outlookcode.com/d/code/getfolder.htm

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



lynxwoman said:
I want the macro to run only on a specific contact folder,
how do I ensure the change will only happen in a specific
folder?

sandy


..



Thank you so much,
Sandy
 
S

Sandy

do I need to modify the language of the macro to
accomodate the required categories script? I ask because
in order to Save the form has to have 2 required
categories, is there a way to work that into the replace
one category with another script?

Outlook 2002

thanks
sandy
 
S

Sue Mosher [MVP-Outlook]

You might want to set a breakpoint in the For I = 0 To UBound(arr) loop or
add a Debug.Print arr(I) statement to see just what categories are being
detected for each item.

If this is a one-time operation, you can also make the change manually by
selecting all items, right-clicking and choosing Categories.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



lynxwoman said:
Thanks Sue. When I run the VBScript to change the category it doesn't
change anything.
Example: strOldCat = "_1A Interiors"
strNewCat = "1A Interior Architecture"

When I view the categories, none of the _1A Interiors have been changed.
Any suggestions?
thanks

Subject: Re: ALSO
From: "Sue Mosher [MVP-Outlook]" <[email protected]> Sent:
5/4/2004 12:57:55 PM


Look at how the code determines what folder it's going to work on:

Set objFolder = objApp.ActiveExplorer.CurrentFolder

This is the folder that the user currently has displayed. If you want to
code in which folder and it's not a default folder, then you need to
walk
the folder hierarchy using the Folders collections or use a function
that
does that for you. See http://www.outlookcode.com/d/code/getfolder.htm
 
S

Sue Mosher [MVP-Outlook]

All the script you cited does is replace one category with another. If you
also need validation that additional categories have been selected on each
item that meet certain criteria, then you'll have to add that logic. If the
items are using a custom form that requires certain categories, the code in
that form will run and try to enforce that requirement.

Another possible approach is to use CDO instead of Outlook to update the
catgegories, the difference being that accessing items with CDO will not
invoke the code on the form for those items.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top