none
Wrong replacement in content controls RRS feed

  • Question

  • Hi Experts,

    i have some Word-Documents with Plain-Text Content-Controls. Each one of them has a unique TAG to identify them later by code. In that code part i iterate over all Content Controls to see if the TAG matches my requirements. If so, some Text will be set. This is working fine but for one user some controls having the text value of another control in that document. e.g. The Control 'Worker' has the value of Control 'Deliverydate'. This happens ONLY to one specific user. The code component is having a verbose logger and i can clearly see that the correct values will be written to the controls.

    So i think, there is something with the Client. Does anybody may run into the same issue?

    Thanks, Ronny

    Thursday, July 17, 2014 10:13 AM

All replies

  • Hi Ronny,

    I don't understand what you want.

    From your description, Each content control has an unique TAG. Some content controls have the same text. But they will not effect each other since you want to change text based on TAG.

    To help us understand this issue exactly, could you please provide more information? Maybe code sample or a simple document would be better.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, July 18, 2014 9:49 AM
    Moderator
  • Hi Ronny

    You've asked this in the Open XML SDK forum, but I get the impression that you're actually using the interop? Could you show us the relevant code that identifies content controls and writes the value to them? And are these content controls mapped to a custom xml part?


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, July 18, 2014 5:03 PM
    Moderator
  • Hi George,

    thank you for your quick Response.

    I really dont' see why my Explanation should be misunderstood. Please see part of the code (partially pseudo):

    // Read Document
                byte[] data  = File.ReadAllBytes(@"D:\Test.docx");
                string find  = "";
                string value = "";
    
                using (MemoryStream mem = new MemoryStream())
                {
                    mem.Write(data, 0, (int)data.Length);
    
                    using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(mem, true))
                    {
                        // Iterate over all Content Controls of the document
                        foreach (var cc in wordDoc.ContentControls())
                        {
                            // Determine the tag
                            SdtProperties props = cc.Elements<SdtProperties>().FirstOrDefault();
                            Tag tag             = props.Elements<Tag>().FirstOrDefault();
                            find                = tag.Val.Value;
    
                            // Get the value by Tag
                            value               = GetValueFromTag(find);
                            
                            if (!string.IsNullOrEmpty(value))
                            {
                                // Line Breaks will be ignored, so we have to add them manually
                                string[] newLineArray = { Environment.NewLine };
                                string[] textArray = value.Split(newLineArray, StringSplitOptions.None);
    
                                bool first = true;
    
                                foreach (string line in textArray)
                                {
                                    if (!first)
                                    {
                                        cc.Descendants<Run>().FirstOrDefault<Run>().Append(new Break());
                                    }
    
                                    first = false;
    
                                    // Set the text to the content control
                                    Text txt = new Text();
                                    txt.Text = line;
                                    cc.Descendants<Run>().FirstOrDefault<Run>().Append(txt);
                                }
                            }
                        }
                    }
                }

    Suppose the data that will be assigned to the Content controls are like this:

    Deliverydate ='21.07.2014'

    Worker = 'Ronny'

    This is working for every user which is using this Application.

    ONLY for ONE particular user the document is like this:

    Deliverydate ='21.07.2014'

    Worker = '21.07.2014'

    I hope it is clearer now to you.

    With best regards, Ronny

    Monday, July 21, 2014 6:42 PM
  • Hi Cindy, thank you for your Response,

    as you can see in my reply to George, i'm not using Interop.

    With best regards, Ronny

    Monday, July 21, 2014 6:43 PM
  • Hi Ronny

    Thanks for the sample code, which appears clear enough with the exception that you don't show where the variable "line" originates:  txt.Text = line;

    Also, what does GetValueFromTag actually do?

    Given that the behavior happens only with one user, IF this were Interop, I'd worry about an Add-in running in process with Word that's somehow affecting the content controls' properties. This document that contains the content controls: is it ever opened in Word, by the user, before your code writes the values?

    AND: are these content controls linked to a Custom XML Part?

    Where is the data coming from that you're writing to the content controls? Could there be something interfering with it that could be originating with a particular user profile? Does your logger write out the name of the content control TAG together with the value you expect to be written, as part of the code you show us?


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, July 22, 2014 3:09 PM
    Moderator
  • Hi Cindy,

    sorry for my late Response.

    Variable line comes from foreach (string line in textArray)
    No XML Part.

    Data is coming from SharePoint (GetValueFromTag).

    Logger writes Tag and value, everything ok here.

    I will check interfering with another open Document of the same type when this process is running.

    With Kind Regards, Ronny

    Wednesday, August 27, 2014 10:14 AM