programactically removing a contact from a category

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I asked this question earlier, but perhaps I wasn't clear... Is there a
simple way using VBA in a form I'm publishing to add code (assigned to a
toolbar button) to remove the contact from the category "Answered Mail"?
Thanks,
Charlie
 
Hi Charlie,

there´s no way at all using VBA in a CustomForm. Code behind that Form
is VBScript.

Removing a contact from a category in VBA, sample:

dim oContact as OutlookContactItem
dim lPos as Long
Set oContact=Application.ActiveExplorer.Selection(1)
With oContact
lPos= Instr(.Categories,"Answered Mail")
Select Case lPos
Case 0
Case 1
.Categories=mid(.Categories, lPos+len(Answered Mail))
.Save
Case Else
.Categories=.Categories & mid(.Categories, lPos+len(Answered
Mail))
.Save
End select
End with
 
Hi, I tried this code, but it seemed to have a bug in it. I put Mail Sent in
" " and that made it run, but it doesn't seem to do anything. Maybe I don't
need the quotes, but I haven't got it to work. Can you take a look at this
code and see if there just a simple problem I made. For simplicity, I
changed the category to "Mail"
Many thanks,
ck
Dim oContact As Outlook.ContactItem
Dim lPos As Long
Set oContact = Application.ActiveExplorer.Selection(1)
With oContact
lPos = InStr(.Categories, "Mail")
Select Case lPos
Case 0
Case 1
.Categories = Mid(.Categories, lPos + Len("Mail"))
.Save
Case Else
.Categories = .Categories & Mid(.Categories, lPos + Len("Mail"))
.Save
End Select
End With
 
Hi Charlie,

the sample is ok. It shows you how to modify the Categories property and
expects that you´ve selected at least one ContactItem in a folder. How
do you get the sample starting?
 
Hi, I just figured out that this code does work when the only category it is
in is "Mail". (if it's in other categories also, the code doesn't do
anything that I can tell). However, there will never be a time when that's
the only category it's in... It's in others, some listed before "Mail", some
after... listed alphabetically. I need it to just remove the one
category-"Mail" is this not possible? Someone said it might be by using the
split() and join() functions, but I wouldn't know how to do it by myself...
Hope someone knows the answer to this. Wish I could repay you for it. Many
thanks, it's a big help.
Charlie
 
Sorry, you´re right. Here it comes again. The sample assumes that there
are always two characters between two categories - I never have seen
anything else.

You can call the function e.g.:

With Application.ActiveExplorer.Selection(1)
.Categories=RemoveCategory(.Categories, "Mail")
.Save
End with


Private Function RemoveCategory(sCategories As String, sRemove As
String) As String
Dim lPos As Long
Dim lLenTotal As Long
Dim lLenRemove As Long

lLenTotal = Len(sCategories)
If lLenTotal Then
lLenRemove = Len(sRemove)
lPos = InStr(1, sCategories, sRemove, vbTextCompare)
Select Case lPos
Case 1
Select Case lLenTotal
Case Is > lLenRemove
sCategories = Trim$(Mid$(sCategories, lPos + Len(sRemove) + 1))
Case Else
sCategories = vbNullString
End Select
Case Is > 1
Select Case lLenTotal
Case Is > lPos + lLenRemove - 1
sCategories = left$(sCategories, lPos - 3) & _
Mid(sCategories, lPos + Len(sRemove))
Case Else
sCategories = left$(sCategories, lPos - 3)
End Select
End Select
End If
RemoveCategory = sCategories
End Function
 
Back
Top