PDA

View Full Version : Solved: question about "UserProperties"



separator
04-13-2005, 05:22 AM
Hi!
I have user defined field "Number" in one of my folders.
Data Type for this field - "Integer"
I trying to change it by using UserProperties:

>Dim myItem As MailItem
>
> myItem.UserProperties.Add "Number", olNumber
> myItem.UserProperties("Number") = "111"

But recive error
"A custom field with this name, but different Data Type alredy exists. Enter a different name"

I try to write -
myItem.UserProperties.Add "Number", olNumber
but "Data type is not supported"

So, somebody can help me ?:help :)

Regards!

Killian
04-13-2005, 09:01 AM
Hi again,
This is happening because you're trying to put a number into a text property.
If you want to use the same proerty name ("Number") you'll have to delete it first then add it a a olNumber type.
I don't have outlook available right now but it's something like this:
myItem.UserProperties.Remove("Number")
myItem.UserProperties.Add "Number", olNumber

You might have to use the Find method on the Item first to check its name, "Number", get its Index and Remove referring to the index:
myItem.UserProperties.Remove(index)

Hope that's helped

separator
04-13-2005, 09:12 AM
Hi Killian!

I alredy try to remove it at first but recive error "Type mismatch"
But IMHO problem not in this!

Because for other fields with other types I don't need to do it. It working well.

Field "Number" alredy exists in Outlook & have Type "Integer"
But User Property have only:
OlUserPropertyType
olOutlookInternal = 0
olText = 1
olNumber = 3
olDateTime = 5
olYesNo = 6
olDuration = 7
olKeywords = 11
olPercent = 12
olCurrency = 14
olFormula = 18
olCombination = 19

So, number not acceptible! I need find the way to use Integer in UserProperty.
If it posible ?

MOS MASTER
04-13-2005, 12:08 PM
Hi, :D

Could this be as simple as it looks?

You're using "111" to fill the value property like Killian says you can't asign a string to an Integer property. (Or you should convert it with Val() or CInt())

In you're case you can change this line: myItem.UserProperties("Number").Value = "111"

'with:

myItem.UserProperties("Number").Value = 111

A simple test procedure that adds a new contact with property:
Sub UserProp()
Dim oApp As New Outlook.Application
Dim oItem As Outlook.ContactItem
Set oItem = oApp.CreateItem(olContactItem)
With oItem
.UserProperties.Add "aNumber", olNumber
.UserProperties("aNumber").Value = 111

MsgBox .UserProperties("aNumber").Value
.Display
End With
End Sub

Enjoy! :thumb

separator
04-14-2005, 12:32 AM
Hi!

You don't understand me.:hi: Line that I show before it's just example. But wharever :
>Dim myItem As MailItem
>
> myItem.UserProperties.Add "Number", olNumber
> myItem.UserProperties("Number") = "111"

I recive error when I try to change filed type (Not when define value for it)!
It's the problem!
I have a lot(more then 1000) of fields in Outlook with Type Integer!
But I can't Define for UserProperty Integer Type! Only "Number"

2 Killian
I think the best way is, how you alredy said to remove item, then make new!
But I recive error "Type mismatch" at this line

>myItem.UserProperties.Remove "Number"

What it can be ?

Regards to all.

Killian
04-14-2005, 05:31 AM
Yeah, these UserProperties in Outlook are a little strange...
You must refer to them by index, not by name. Therefore the best way to do this is loop through the collection and test for what you want, then do something with it.
I just tested this routine and it seems to work fine. It will go through each of the selected mails and if it has the "Number" property, remove it and add a new one of type olNumberSub ReplaceAll()

Dim olItem As MailItem
Dim i As Long

For Each olItem In Application.ActiveExplorer.Selection
For i = 1 To olItem.UserProperties.Count
If olItem.UserProperties.Item(i).Name = "Number" Then
olItem.UserProperties.Remove (i)
olItem.UserProperties.Add "Number", olNumber
olItem.UserProperties.Item(i).Value = 111
End If
Next
Next

End Sub

separator
04-14-2005, 06:49 AM
Thanx Killian!

But the same error!
Field with this name, but different data type alredy exists!

>olItem.UserProperties.Remove (i)
Actually not delete the field, the same with:
> olItem.UserProperties.Item(i).Delete

I think the best way to make a new field with diferent name and "Number" Type and ignore old one.

Regards.

Killian
04-14-2005, 08:21 AM
I think the best way to make a new field with diferent name and "Number" Type and ignore old one. Probably true...
Although, I don't really understand why it fail on the remove line - the data type isn't important. :dunno

MOS MASTER
04-14-2005, 12:58 PM
Hi!

You don't understand me.:hi: Line that I show before it's just example. But wharever :
Hi,

Well I think I did understand you're questione but I cannot reproduce you're errors on my machine...


>Dim myItem As MailItem
>
> myItem.UserProperties.Add "Number", olNumber
> myItem.UserProperties("Number") = "111"

I recive error when I try to change filed type (Not when define value for it)!
It's the problem!
I have a lot(more then 1000) of fields in Outlook with Type Integer!
But I can't Define for UserProperty Integer Type! Only "Number"
To my knowledge olNumber is an Integer constant!

And on my machine it does accept Integers..(For that matter it even accepts Strings...so seams to be off variant type??)

Tested it against an integer like by converting string to integer:
.UserProperties("aNumber").Value = CInt("111")

Like Killian I have some trouble understanding what's causing this problem in you're enviroment.

The ReplaceAll sub runs fine over here ... don't understand this! :banghead:

No errors what so ever if I try to add the property twice in one sub:
Sub UserProp2()
Dim oApp As New Outlook.Application
Dim oItem As Outlook.MailItem
Set oItem = oApp.CreateItem(olMailItem)
With oItem
.UserProperties.Add "Number", olNumber
.UserProperties("Number").Value = 111

.UserProperties.Add "Number", olNumber
.UserProperties("Number").Value = 111

MsgBox .UserProperties("Number").Value
.Display
End With
End Sub

Perhaps you can attach you're whole procedure perhaps there is something else in there that's causing this problem?

Enjoy! :thumb

separator
04-15-2005, 02:20 AM
Yes I can. Here it is:

Sub SelectedChange(statStatus, statOwn, statCustomer, statTechnology, statKeyWords, statTargetDate)

Public StatNumber As Integer
Public Change_Number As Boolean
Public Change_Status As Boolean
Public Change_Own As Boolean

Dim myOlApp As New Outlook.Application
Dim myOlExp As Outlook.Explorer
Dim myOlSel As Outlook.Selection
Dim MsgTxt As String
Dim myItem As MailItem

Set myOlExp = myOlApp.ActiveExplorer
Set myOlSel = myOlExp.Selection
For Each myItem In myOlSel
If Change_RTN = True Then
myItem.UserProperties.Add("Number", olText).Value = statNumber
myItem.Save
End If
If Change_Status = True Then
myItem.UserProperties.Add("Status", olText).Value = statStatus
myItem.Save
End If
If Change_Own = True Then
myItem.UserProperties.Add("Own", olText).Value = statOwn
myItem.Save
End If
Next

End Sub

MOS MASTER
04-15-2005, 10:10 AM
Hi, :D

Little time today but I'll review tommorow..

First you're code has errors in them. Put Option Explicit on top of you're program code en run you're code...It will not be executed!

Main reason is because you use "Public" variables inside you're Subprocedure. This is not allowed..Put them on top right under the Option Explicit.

Also the sub has 6 parameters and you're only using 2 off them in you're code..why is that? (Seems redundant to me)

Further more the parameters aren't dimensioned e.g.: statStatus As String, statOwn As string, etc...

So there all Variants right now and that makes for slow code execution.

Take a look at it and tommorow I'll test you're sub..

Enjoy! :thumb

MOS MASTER
04-16-2005, 11:49 AM
Hi, :D

I've been getting the same error executing you're code on the emails found in the sellection.

Still I'm not able to fully understand why this error occurs.

What have I done:
* Run a sub that prints all the userProperties on the selected emails in the Inbox. (This to make shure that there wasn't any before I run you're sub on it)

* So at that point I knew there was no Number propertie in it. (That I could see that is)

* Running you're code on it I received the Ghost error. The strange thing is that this only happens to allready existing emails. If you create a new one and give it the Number property then there's no problem what so ever.

* The other thing is that it only happens with the property called: "Number" if you call in "aNumber" in you're procedure..all runs well!!!

That fact only makes me believe that "Number" is some kind off "Reserved" Word in Outlook that Outlook needs for it's one. (Every application has a bunch of reserved words that you're not supposed to use)

So If you just use a different name for you're property other then "Number" everything runs well. (at least it does over here)

Enjoy! :thumb

separator
04-19-2005, 05:04 AM
Thanx to all :)
Now just using different name

MOS MASTER
04-19-2005, 11:40 AM
Hi, :D

Glad you've got it working now...you're welcome! :thumb
ps..could you mark you're thread solved?