How to change the value of SdtContentDate of a word document.docm using dotnet C# & Is there a role to change the value of anykind of contentControl?

omar wasfi 1 Reputation point
2022-08-22T08:21:41.317+00:00

Is there A role to change anykind of content control in a word.docm without breaking the file itself ?
In this example I am trying to change the value of SdtContentDate. After changing and save the file I get the following error.

I'm using this code to get the element by tag

        public  IEnumerable<OpenXmlElement> ContentControls( OpenXmlPart part)  
    {  
        if (part.RootElement != null)  
        {  
              
            return part.RootElement.Descendants().Where(e => e is SdtElement);  
        }  

        return new List<OpenXmlElement>();  
    }  

    public  IEnumerable<OpenXmlElement> ContentControls( WordprocessingDocument doc)  
    {  
        if (doc.MainDocumentPart != null)  
        {  
            foreach (var cc in ContentControls(doc.MainDocumentPart))  
            {  
                yield return cc;  
            }  

            foreach (var header in doc.MainDocumentPart.HeaderParts)  
            {  
                foreach (var cc in ContentControls(header))  
                {  
                    yield return cc;  
                }  
            }  

            foreach (var footer in doc.MainDocumentPart.FooterParts)  
            {  
                foreach (var cc in ContentControls(footer))  
                {  
                    yield return cc;  
                }  
            }  

            if (doc.MainDocumentPart.FootnotesPart != null)  
            {  
                foreach (var cc in ContentControls(doc.MainDocumentPart.FootnotesPart))  
                {  
                    yield return cc;  
                }  
            }  

            if (doc.MainDocumentPart.EndnotesPart != null)  
            {  
                foreach (var cc in ContentControls(doc.MainDocumentPart.EndnotesPart))  
                {  
                    yield return cc;  
                }  
            }  
        }  
    }  

    public  List<OpenXmlElement> FindContentControlByTag( WordprocessingDocument doc, string tag)  
    {  
        IEnumerable<OpenXmlElement>? ccs = ContentControls(doc);  
        List<OpenXmlElement>? output = new();  

        if (ccs != null)  
        {  
            foreach (var cc in ccs)  
            {  
                SdtProperties? props = cc.Elements<SdtProperties>().FirstOrDefault();  

                if (props != null)  
                {  
                    DocumentFormat.OpenXml.Wordprocessing.Tag? tagElement = props.Elements<DocumentFormat.OpenXml.Wordprocessing.Tag>().FirstOrDefault();  

                    if (tagElement != null)  
                    {  
                        //Console.WriteLine(tagElement.Val);  

                        if (tagElement.Val == tag)  
                        {  
                            output.Add(cc);  
                        }  
                    }  
                }  

            }  
        }  

        return output;  
    }  

My problem after I select the sdtContentControl I can't change it's value

        List<OpenXmlElement> elements =  FindContentControlByTag(document, "sdate");  

        OpenXmlElement cc = elements.First();  
        var datePickers = cc.Descendants<SdtContentDate>().ToList();  

        SdtContentDate datePicker = datePickers.First();  

        datePicker.FullDate = new DateTimeValue(DateTime.Now);  

After saving the document I get this error

233425-screen-shot-2022-08-22-at-100227.png

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,415 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,306 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,531 questions
0 comments No comments
{count} votes