VBA Rename Category

C

Céline Brien

Hi !
Recently I had help from Ken Slovak in creating a macro to delete
Contacts of a category.
If I modify this macro, is it possible to obtain a macro to rename a
category ?
Thanks for your help,
Céline
--------------------------
Option Explicit
Sub RenameCategory()
''
Dim ol_App As New Outlook.Application
Dim ol_Mapi As Outlook.NameSpace
Dim ol_Folder As Outlook.MAPIFolder
Dim ol_Items As Outlook.Items
Dim ol_Itm As Object
Dim LgI As Long
Set ol_Mapi = ol_App.GetNamespace("MAPI")
Set ol_Folder = ol_Mapi.GetDefaultFolder(olFolderContacts)
Set ol_Items = ol_Folder.Items
For LgI = ol_Items.Count To 1 Step -1
Set ol_ContactItm = ol_Items(LgI)
If ol_ContactItm.Class = olContact Then
If ol_ContactItm.Categories = "Excel" Or InStr(1,
ol_ContactItm.Categories, "; Excel") Or InStr(1,
ol_ContactItm.Categories, "Excel;") Then
ol_ContactItm.Rename
????? How do I indicate the new name ??????
End If
End If
Next LgI
Set ol_ContactItm = Nothing
Set ol_Items = Nothing
Set ol_Folder = Nothing
Set ol_Mapi = Nothing
Set ol_App = Nothing
End Sub
 
K

Ken Slovak - [MVP - Outlook]

You just want to replace the existing Categories field with some other
string? Just set your Categories variable to whatever you want:
ol_ContactItm.Categories = "whatever"

Just make sure to save the item so any changes are persisted.
 
C

Céline Brien

Hi everybody,
Hi Ken,
Thank you for your answer.
I replaced ol_ContactItm.Rename by
ol_ContactItm.Categories = "whatever" (see below)
It does'nt work.
When write "make sure to save the item so any changes are persisted" do
you mean I have to had VBA codes to save ?
Thanks

--------------------------------------------
Option Explicit
Sub RenameOutlookContacts()
'
' Macro pour supprimer les contacts Excel avant de les importer du
fichier Excel
' Macro proposée par Grég sur le forum Outlook, avril 2004
' Céline Brien (819) 326-7273
'
Dim ol_App As New Outlook.Application
Dim ol_Mapi As Outlook.NameSpace
Dim ol_Folder As Outlook.MAPIFolder
Dim ol_Items As Outlook.Items
Dim ol_ContactItm As Object
Dim LgI As Long
Set ol_Mapi = ol_App.GetNamespace("MAPI")
Set ol_Folder = ol_Mapi.GetDefaultFolder(olFolderContacts)
Set ol_Items = ol_Folder.Items
For LgI = ol_Items.Count To 1 Step -1
Set ol_ContactItm = ol_Items(LgI)
If ol_ContactItm.Class = olContact Then
If ol_ContactItm.Categories = "Excel" Or InStr(1,
ol_ContactItm.Categories, "; Excel") Or InStr(1,
ol_ContactItm.Categories, "Excel;") Then
ol_ContactItm.Categories = "whatever"
End If
End If
Next LgI
Set ol_ContactItm = Nothing
Set ol_Items = Nothing
Set ol_Folder = Nothing
Set ol_Mapi = Nothing
Set ol_App = Nothing
End Sub
 
K

Ken Slovak - [MVP - Outlook]

Your changes won't persist unless you save the Outlook item after you change
it. So, after you make your change to .Categories you would use a line:
ol_ContactItm.Save

Do that after all the changes you make to the item.
 
C

Céline Brien

Hi everebody,
Hi Ken,
It worked !
Thank you so much for your help.
Being able to rename a category within items is a very very good
improvement to Outlook.
Now, to rename the category of all Outlook elements, not just contacts
do I need to do it one folder at a time or can I do somethins like
Personnal folder and all sub-folders ?
Thank you for your precious help,
Céline
 
C

Céline Brien

Hi everybody,
Hi Ken,
See codes below to rename the categoy of Outlook elements, whatever the
folder.
I get an error message on : Set ol_Folder = ol_Mapi.Folders
Incompatibilty of type.
Thanks,
Céline

-----------------------------------------------------------------------
Option Explicit
Sub RenameCategory()
'
'
Dim ol_App As New Outlook.Application
Dim ol_Mapi As Outlook.NameSpace
Dim ol_Folder As Outlook.MAPIFolder
Dim ol_Items As Outlook.Items
Dim ol_AllItms As Object
Dim LgI As Long
Set ol_Mapi = ol_App.GetNamespace("MAPI")
Set ol_Folder = ol_Mapi.Folders
Set ol_Items = ol_Folder.Items
For LgI = ol_Items.Count To 1 Step -1
Set ol_AllItms = ol_Items(LgI)
If ol_AllItms.Categories = "Before" Or InStr(1,
ol_AllItms.Categories, "; Before") Or InStr(1, ol_AllItms.Categories,
"Before;") Then
ol_AllItms.Categories = "After"
ol_AllItms.Save
End If
Next LgI
Set ol_AllItms = Nothing
Set ol_Items = Nothing
Set ol_Folder = Nothing
Set ol_Mapi = Nothing
Set ol_App = Nothing
End Sub
 
?

=?iso-8859-1?B?R3LpZw==?=

Céline,

Is not the error on "Set ol_Items = ol_Folder.Items" ? I did not find an
"Items" property or method for a "mapifolder" object but an "Item" method!
 
K

Ken Slovak - [MVP - Outlook]

You cannot set one folder (ol_Folder) to a Folders collection. You need to
use an index property. The code you are using will start from the top level
folder and just get that. You would need to recursively iterate each Folders
collection and for each MAPIFolder check its Folders collection.
'In For loop
Set ol_Folder = ol_Mapi.Folders(i)
'do stuff
Set olFolders1 = ol_Folder.Folders
'call the same Sub again to parse all the subfolders of the current
folder
'this will be a recursive Sub that keeps calling itself until it runs
out of subfolders
'then it will finish the loop and check the next folder.
 
C

Céline Brien

Hi everybody,
Hi Ken,
Thank you for your answer.
I will try your suggestion and come back.
I have been suggested to change :
Dim ol_App As New Outlook.Application
for :
Dim ol_App As Outlook.Application
and add :
Set ol_App = Application
You get a "trusted" code with OL2K3
What do you think of this ?
Thanks again,
Céline
 
C

Céline Brien

Hi everybody,
Hi Grég,
With computer I am always sceptical until I understand ;-)
I just wanted to have a second advice on this.
Your link gave it to me, many thanks,
Céline
 
K

Ken Slovak - [MVP - Outlook]

If you are writing an Outlook COM addin then setting a global
Outlook.Application object reference to the Application object passed in the
On_Connection event handler is the way to go. That way that object and any
objects derived from it are trusted in Outlook 2003, and it's required for
previous versions of Outlook if you want to trust the COM addin using the
Exchange security form and the hash control.

If you are writing Outlook VBA macro code then just use the Application
object, that's inherently trusted in Outlook 2003 VBA code.
 
C

Céline Brien

Hi everybody,
Hi Ken,
Thank you for this precision.
I will now try to insert a loop in my codes has you suggested (see
below).
I'll be back with the result soon.
Thanks again,
Céline
-------------------------------------------------------------
'In For loop
Set ol_Folder = ol_Mapi.Folders(i)
'do stuff
Set olFolders1 = ol_Folder.Folders
'call the same Sub again to parse all the subfolders of the current
folder
'this will be a recursive Sub that keeps calling itself until it
runs
out of subfolders
'then it will finish the loop and check the next folder.
 

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