Word plain text content control, replace text RRS feed

  • Question

  • Hello,

    So my question is rather straightforward. I have a document where I need to replace the text of plain text controls.

    Let's say the docment name is input.docx and the plain text controls name is CustomerName. I have done the following, which probably is overkill/not efficient:

     Using wpdoc As WordprocessingDocument = WordprocessingDocument.Open("C:\temp\input.docx", True)
                Dim doc As Document = wpdoc.MainDocumentPart.Document
                For Each myPart As DocumentFormat.OpenXml.OpenXmlElement In doc.Descendants
                    If myPart.HasChildren = True Then
                        For Each myItem As DocumentFormat.OpenXml.OpenXmlElement In myPart.ChildElements
                            If myItem.HasChildren = True Then
                                For Each superSubItem As DocumentFormat.OpenXml.OpenXmlElement In myItem.ChildElements
                                    If TypeOf superSubItem Is SdtAlias Then
                                        Dim ppa As DocumentFormat.OpenXml.Wordprocessing.SdtAlias = superSubItem
                                        If ppa.Val.ToString() = "CustomerName" Then
         'This is true, but where do I go from here??? How do I edit the text of this item?
                                        End If
                                    End If
                            End If
                    End If
            End Using

    Hoping the above example explains my problem, as I'm pretty sure there is some better way of doing this. Currently I can't even replace the text of the control. I only find the control, but how to replace the text of it, I have no clue.

    So, anyone knows how to edit the text of this object?

    Thanks beforehand for any and all replies!

    Tuesday, April 5, 2011 2:00 PM