none
multiline text swaps on save RRS feed

  • Question

  • I am using the unused children field of contacts as a multiline address field on my custom contact form.  Lately I notice something strange happens but not all the time.

    When I enter:

    Jackson Smith & Jones LLP
    222 Madison Avenue
    Suite 333
    New York, New York 10017

    This seems to work as desired.  But if I enter:

    Tracy Smith & Jones LLP
    222 Madison Avenue
    Suite 333
    New York, New York 10017

    after saving it becomes:

    New York 10017, Tracy Smith & Jones LLP
    222 Madison Avenue
    Suite 333
    New York

    as apparently it takes whatever was after the comma and moves it to the top line.  Is there a glitch or a setting that is perhaps causing this to happen?  It's annoying my users when they struggle to enter certain address fields only for this to happen.  Please advise, thanks.

    Tuesday, May 7, 2013 5:38 PM

Answers

  • 1. There is no reason for the second line to access the UserProperties collection again. UserProperies.Add returns the UserProperty object:

    UserProperty prop = cntc.UserProperties.Add("AltAddr", Outlook.OlUserPropertyType.olText, true, Outlook.OlUserPropertyType.olText);
    prop.Value = cntc.Children;

    2. Since you are adding the user property to the folder fields (which can be expensive), scratch my previous reply and avoud using Add every time: use Find and call UserProperies.Add only if Find returns null:

    UserProperty prop = cntc.UserProperties.Find("AltAddr");
    if (prop == null)
    {
        prop = cntc.UserProperties.Add("AltAddr", Outlook.OlUserPropertyType.olText, true, Outlook.OlUserPropertyType.olText);
    }
    prop.Value = cntc.Children;


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, May 8, 2013 8:44 PM
  • This way I can keep my coding concise like as follows:

                                string cmdstr = "";
                                cmdstr = "insert into DMtodo(Subject,Categories,DueDate,TodoFirm,TodoReferred,TodoType,TodoMatterType,TodoOnTRL,TodoSysid,TodoStaff,TodoStatus)";
                                cmdstr += " values(";
                                cmdstr += "'" + repq(tsk.Subject) + "',";
                                cmdstr += "'" + tsk.Categories + "',";
                                cmdstr += "'" + tsk.DueDate.ToShortDateString() + "',";
                                cmdstr += "'" + repq(propval(tsk, "TodoFirm")) + "',";
                                cmdstr += "'" + repq(propval(tsk, "TodoReferred")) + "',";
                                cmdstr += "'" + repq(propval(tsk, "TodoType")) + "',";
                                cmdstr += "'" + repq(propval(tsk, "TodoMatterType")) + "',";
                                cmdstr += propval(tsk, "TodoOnTRL") + ",";
                                cmdstr += "'" + repq(propval(tsk, "TodoSysid")) + "',";
                                cmdstr += "'" + repq(propval(tsk, "TodoStaff")) + "',";
                                cmdstr += "'" + repq(propval(tsk, "TodoStatus")) + "'";
                                cmdstr += ");";
                                OleDbCommand cmd = new OleDbCommand(cmdstr, mycon);
                                cmd.ExecuteNonQuery();
    

    Thursday, May 9, 2013 3:59 PM

All replies

  • I bet Outlook sorts the "," separated list alphabetically (N comes before T).

    I would not reuse the Children property, create your own one instead.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!


    Tuesday, May 7, 2013 5:41 PM
  • I see, is there a way to turn off sorting in the Children property?  I've been trying to avoid using user-defined fields due to difficulties I was once having involving form regions while using other add-in programs where it didn't recognize them at all it seemed.
    Tuesday, May 7, 2013 5:49 PM
  • I am now attempting to add a user-defined "AltAddr" field to my contact form region, but when I go to re-add my form region reading pane I get the following error message:

    Error:

    Object reference not set to an instance of an object.

    Help!

    Wednesday, May 8, 2013 4:05 PM
  • No, you cannot turn sorting off

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, May 8, 2013 4:42 PM
  • And what is your code that raises that error?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, May 8, 2013 4:42 PM
  • The following line of code in an add-in I use to export values to an excel spreadsheet:

    oCells[oRow, ++oCol] = contc.UserProperties["AltAddr"].Value;

    where  contc is set to Outlook.ContactItem

    edit: my guess is I need the following code prior to it, not sure why or how though:

    contc.UserProperties.Add("AltAddr", Outlook.OlUserPropertyType.olText, true, Outlook.OlUserPropertyType.olText);
    

    • Edited by jfalberg Wednesday, May 8, 2013 6:59 PM
    Wednesday, May 8, 2013 6:22 PM
  • Your code assumes that the user property exists.

    Change that line to

    oCells[oRow, ++oCol] = contc.UserProperties.Add("AltAddr", olUserProperyType.olText) = Value;

    UserProperties.Add will either add the property if it does not exist or return an existing one.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, May 8, 2013 7:02 PM
  • I also have the following line of code ready for when I have to go make my corrections so hopefully this will continue to work as desired:

    cntc.UserProperties.Add("AltAddr", Outlook.OlUserPropertyType.olText, true, Outlook.OlUserPropertyType.olText);
    cntc.UserProperties["AltAddr"].Value = cntc.Children;
    


    Wednesday, May 8, 2013 8:38 PM
  • 1. There is no reason for the second line to access the UserProperties collection again. UserProperies.Add returns the UserProperty object:

    UserProperty prop = cntc.UserProperties.Add("AltAddr", Outlook.OlUserPropertyType.olText, true, Outlook.OlUserPropertyType.olText);
    prop.Value = cntc.Children;

    2. Since you are adding the user property to the folder fields (which can be expensive), scratch my previous reply and avoud using Add every time: use Find and call UserProperies.Add only if Find returns null:

    UserProperty prop = cntc.UserProperties.Find("AltAddr");
    if (prop == null)
    {
        prop = cntc.UserProperties.Add("AltAddr", Outlook.OlUserPropertyType.olText, true, Outlook.OlUserPropertyType.olText);
    }
    prop.Value = cntc.Children;


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, May 8, 2013 8:44 PM
  • Is there a c# library or reference that UserProperty belongs to?

    I started typing in "UserProperty prop = cntc.UserProperties.Find("AltAddr") 

    but got a red squiggly line indicating the type or namespace name UserProperty could not be found.

    Wednesday, May 8, 2013 8:56 PM
  • It will be defined in the same assembly as the rest of your Outlook objects (Outlook.UserProperty?).

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, May 8, 2013 8:59 PM
  • So far the following seems to work.

    Outlook.UserProperty prop = cntc.UserProperties.Find("AltAddr");
    if (prop == null)
    {
       prop = cntc.UserProperties.Add("AltAddr", Outlook.OlUserPropertyType.olText, true, Outlook.OlUserPropertyType.olText);
    }
    prop.Value = cntc.Children;
    cntc.Save();

    It looks like I could re-use "prop" for other user-defined fields I may add as I was simply avoiding this all the time due to that initial error I reported since I'm using form regions here.


    Thursday, May 9, 2013 2:21 PM
  • A somewhat related question as I'm sure I'll need to use the userproperty find and add methods again here as well and was wondering if I have to do that before every reference of userproperty fields again I have in following code:

                            tsk = tdItem as Outlook.TaskItem;
                            DateTime cmpDate = new DateTime();
                            cmpDate = tsk.DueDate;
                            lbl_firm.Text = "Firm: " + tsk.UserProperties["TodoFirm"].Value;
                            lbl_referred.Text = "Individual: " + tsk.UserProperties["TodoReferred"].Value;
                            lbl_categories.Text = "In/Out: " + tsk.Categories;
                            lbl_firmtype.Text = "Ref Type: " + tsk.UserProperties["TodoType"].Value;
                            lbl_date.Text = "Date: " + tsk.DueDate.ToShortDateString();
                            lbl_client.Text = "Client: " + tsk.Subject;
                            lbl_matter.Text = "Matter: " + tsk.UserProperties["TodoMatterType"].Value;
                            lbl_onTRL.Text = "TRL: " + tsk.UserProperties["TodoOnTRL"].Value;
                            lbl_sysid.Text = "SYSID: " + tsk.UserProperties["TodoSysid"].Value;
                            lbl_staff.Text = "Staff: " + tsk.UserProperties["TodoStaff"].Value;
                            lbl_status.Text = "Status: " + tsk.UserProperties["TodoStatus"].Value;
                            lbl_type.Text = "Type: " + tsk.UserProperties["TodoType"].Value;
                            cntAll++;
    
                            Boolean quald = true;
                            if ((cboStaff.Text.Length > 0 &&
                                 cboStaff.Text != tsk.UserProperties["TodoStaff"].Value) ||
                                (cboReferralType.Text.Length > 0 &&
                                 cboReferralType.Text != tsk.Categories) ||
                                (cboContactType.Text.Length > 0 &&
                                 cboContactType.Text != tsk.UserProperties["TodoType"].Value) ||
                                (cboMatterType.Text.Length > 0 &&
                                 cboMatterType.Text != tsk.UserProperties["TodoMatterType"].Value))
                            {
                                quald = false;
                            }
    
                            if (quald == true)
                            {
                                string cmdstr = "";
                                cmdstr = "insert into DMtodo(Subject,Categories,DueDate,TodoFirm,TodoReferred,TodoType,TodoMatterType,TodoOnTRL,TodoSysid,TodoStaff,TodoStatus)";
                                cmdstr += " values(";
                                cmdstr += "'" + repq(tsk.Subject) + "',";
                                cmdstr += "'" + tsk.Categories + "',";
                                cmdstr += "'" + tsk.DueDate.ToShortDateString() + "',";
                                cmdstr += "'" + repq(tsk.UserProperties["TodoFirm"].Value) + "',";
                                cmdstr += "'" + repq(tsk.UserProperties["TodoReferred"].Value) + "',";
                                cmdstr += "'" + repq(tsk.UserProperties["TodoType"].Value) + "',";
                                cmdstr += "'" + repq(tsk.UserProperties["TodoMatterType"].Value) + "',";
                                cmdstr += tsk.UserProperties["TodoOnTRL"].Value + ",";
                                cmdstr += "'" + repq(tsk.UserProperties["TodoSysid"].Value) + "',";
                                cmdstr += "'" + repq(tsk.UserProperties["TodoStaff"].Value) + "',";
                                cmdstr += "'" + repq(tsk.UserProperties["TodoStatus"].Value) + "'";
                                cmdstr += ");";
                                OleDbCommand cmd = new OleDbCommand(cmdstr, mycon);
                                cmd.ExecuteNonQuery();
                                cntQual++;
                            }
    


    Thursday, May 9, 2013 2:36 PM
  • Yes, you will need to do that for every property. You might want to wrap the Find/Add/Value logic into a separate method and call it for each user property.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Thursday, May 9, 2013 2:41 PM
  • This seemed to work like a charm using following function:

            private string propval(Outlook.TaskItem tsk, string propAttr)
            {
                Outlook.UserProperty prop = tsk.UserProperties.Find(propAttr);
                if (prop == null)
                {
                    prop = tsk.UserProperties.Add(propAttr, Outlook.OlUserPropertyType.olText, true, Outlook.OlUserPropertyType.olText);
                }
                object obj = prop.Value;
                return obj.ToString();
            }
    

    I had one boolean value from a checkbox field while rest were textbox fields

    Thursday, May 9, 2013 3:21 PM
  • You can handle Boolean props as well if you pass the property type as one of the parameters.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!


    Thursday, May 9, 2013 3:48 PM
  • This way I can keep my coding concise like as follows:

                                string cmdstr = "";
                                cmdstr = "insert into DMtodo(Subject,Categories,DueDate,TodoFirm,TodoReferred,TodoType,TodoMatterType,TodoOnTRL,TodoSysid,TodoStaff,TodoStatus)";
                                cmdstr += " values(";
                                cmdstr += "'" + repq(tsk.Subject) + "',";
                                cmdstr += "'" + tsk.Categories + "',";
                                cmdstr += "'" + tsk.DueDate.ToShortDateString() + "',";
                                cmdstr += "'" + repq(propval(tsk, "TodoFirm")) + "',";
                                cmdstr += "'" + repq(propval(tsk, "TodoReferred")) + "',";
                                cmdstr += "'" + repq(propval(tsk, "TodoType")) + "',";
                                cmdstr += "'" + repq(propval(tsk, "TodoMatterType")) + "',";
                                cmdstr += propval(tsk, "TodoOnTRL") + ",";
                                cmdstr += "'" + repq(propval(tsk, "TodoSysid")) + "',";
                                cmdstr += "'" + repq(propval(tsk, "TodoStaff")) + "',";
                                cmdstr += "'" + repq(propval(tsk, "TodoStatus")) + "'";
                                cmdstr += ");";
                                OleDbCommand cmd = new OleDbCommand(cmdstr, mycon);
                                cmd.ExecuteNonQuery();
    

    Thursday, May 9, 2013 3:59 PM