Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Wed May 23, 2018 1:27 am

Dear mando,

Thanks for your information.
In order to investigate further, please provide your template Word file rather than the screenshot of it. Then I will look into it and provide VB code for you.

Many thanks,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Fri May 25, 2018 2:49 am

Dear mando,

Hope you are doing well.
I changed C# code provided before to VB code, you could have a try. If you still have the issue, please share us your input template Word file for investigation.
Code: Select all
SurroundingClass.FillSDT13836()
......
Class SurroundingClass
        Public Shared structureTags As StructureTags = New StructureTags()

        Public Shared Sub FillSDT13836()
            Using document As Document = New Document("F:\testing\doc form\original document\SDTsample.docx")
                Dim structureTags As StructureTags = GetAllTags(document)
                Dim tagInlines As List(Of StructureDocumentTag) = structureTags.tags

                For i As Integer = 0 To tagInlines.Count - 1
                    Dim [alias] As String = tagInlines(i).SDTProperties.[Alias]
                    Dim tag As String = tagInlines(i).SDTProperties.Tag

                    If tag = "header tag" AndAlso [alias] = "header title" Then
                        Dim par As Paragraph = TryCast(tagInlines(i).ChildObjects.FirstItem, Paragraph)
                        par.Text = "text"
                    End If
                Next

                document.SaveToFile("13836.docx", FileFormat.Docx)
                System.Diagnostics.Process.Start("13836.docx")
            End Using
        End Sub

        Private Shared Function GetAllTags(ByVal document As Document) As StructureTags
            For Each section As Section In document.Sections
                Dim header As HeaderFooter = section.HeadersFooters.Header

                For Each obj As DocumentObject In header.ChildObjects
                    GetSDTInDocumentObject(obj)
                Next

                Dim footer As HeaderFooter = section.HeadersFooters.Footer

                For Each obj As DocumentObject In footer.ChildObjects
                    GetSDTInDocumentObject(obj)
                Next

                For Each obj As DocumentObject In section.Body.ChildObjects
                    GetSDTInDocumentObject(obj)
                Next
            Next

            Return structureTags
        End Function

        Public Shared Sub GetSDTInDocumentObject(ByVal obj As DocumentObject)
            If obj.DocumentObjectType = DocumentObjectType.Paragraph Then

                For Each pobj As DocumentObject In (TryCast(obj, Paragraph)).ChildObjects

                    If pobj.DocumentObjectType = DocumentObjectType.StructureDocumentTagInline Then
                        structureTags.tagInlines.Add(TryCast(pobj, StructureDocumentTagInline))
                    End If
                Next
            ElseIf TypeOf obj Is StructureDocumentTag Then
                structureTags.tags.Add(TryCast(obj, StructureDocumentTag))
            ElseIf obj.DocumentObjectType = DocumentObjectType.Table Then

                For Each row As TableRow In (TryCast(obj, Table)).Rows

                    For Each cell As TableCell In row.Cells

                        For Each cellChild As DocumentObject In cell.ChildObjects

                            If cellChild.DocumentObjectType = DocumentObjectType.StructureDocumentTag Then
                                structureTags.tags.Add(TryCast(cellChild, StructureDocumentTag))
                            ElseIf cellChild.DocumentObjectType = DocumentObjectType.Paragraph Then

                                For Each pobj As DocumentObject In (TryCast(cellChild, Paragraph)).ChildObjects

                                    If pobj.DocumentObjectType = DocumentObjectType.StructureDocumentTagInline Then
                                        structureTags.tagInlines.Add(TryCast(pobj, StructureDocumentTagInline))
                                    End If
                                Next
                            End If
                        Next
                    Next
                Next
            End If
        End Sub
    End Class

    Public Class StructureTags
        Private m_tagInlines As List(Of StructureDocumentTagInline)

        Public Property tagInlines As List(Of StructureDocumentTagInline)
            Get
                If m_tagInlines Is Nothing Then m_tagInlines = New List(Of StructureDocumentTagInline)()
                Return m_tagInlines
            End Get
            Set(ByVal value As List(Of StructureDocumentTagInline))
                m_tagInlines = value
            End Set
        End Property

        Private m_tags As List(Of StructureDocumentTag)

        Public Property tags As List(Of StructureDocumentTag)
            Get
                If m_tags Is Nothing Then m_tags = New List(Of StructureDocumentTag)()
                Return m_tags
            End Get
            Set(ByVal value As List(Of StructureDocumentTag))
                m_tags = value
            End Set
        End Property
    End Class


Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Fri May 25, 2018 4:42 pm

No Reply !!! ?

mando
 
Posts: 17
Joined: Wed May 16, 2018 6:05 pm

Fri May 25, 2018 7:31 pm

there is any reply ?
here is my template
i want read and set values for all fields

mando
 
Posts: 17
Joined: Wed May 16, 2018 6:05 pm

Sun May 27, 2018 1:07 am

I attached the template
please get and set value for all fields of template header , body and footer

mando
 
Posts: 17
Joined: Wed May 16, 2018 6:05 pm

Mon May 28, 2018 7:24 am

Dear mando,

Sorry for late reply as weekend.
After checking your document, I found the content control in header is placed in textbox, so collecting all content controls need to traverse the textbox. Besides, other SDTs are all gathered using previous code, they are just in different properties as theirs different placement in document. Please refer to following code to change the text in SDT.
Code: Select all
    SurroundingClass.FillSDT13836()
    ......
    Class SurroundingClass
        Public Shared structureTags As StructureTags = New StructureTags()

        Public Shared Sub FillSDT13836()
            Using document As Document = New Document("F:\Template_1.docx")
                Dim structureTags As StructureTags = GetAllTags(document)

                'StructureDocumentTagInline which is included in paragraph
                Dim tagInlines As List(Of StructureDocumentTagInline) = structureTags.tagInlines

                For i As Integer = 0 To tagInlines.Count - 1
                    Dim [alias] As String = tagInlines(i).SDTProperties.[Alias]
                    Dim tag As String = tagInlines(i).SDTProperties.Tag

                    If tag = "T_info:" AndAlso [alias] = "T_info:" Then
                        Dim par As Paragraph = tagInlines(i).OwnerParagraph
                        par.Text = "T_info changed"
                        Continue For
                    End If

                    If tag = "Date_issued:" AndAlso [alias] = "Date_issued:" Then
                        Dim par As Paragraph = tagInlines(i).OwnerParagraph
                        par.Text = "Date_issued changed"
                        Continue For
                    End If

                    'the paragraph which has the content control has other textranges "Dear"
                    'you only need to change the textranges in content control
                    If tag = "D_Name:" AndAlso [alias] = "D_Name:" Then

                        For Each obj As DocumentObject In tagInlines(i).ChildObjects

                            If TypeOf obj Is TextRange Then
                                Dim tr As TextRange = TryCast(obj, TextRange)
                                tr.Text = "changed"
                            End If
                        Next

                        Continue For
                    End If

                    If tag = "Sincerely:" AndAlso [alias] = "Sincerely:" Then
                        Dim par As Paragraph = tagInlines(i).OwnerParagraph
                        par.Text = "Sincerely changed"
                        Continue For
                    End If
                Next
                'the level of StrctureTag is equal to paragraph
                Dim tags As List(Of StructureDocumentTag) = structureTags.tags
                For j As Integer = 0 To tags.Count - 1
                    Dim aliaT As String = tags(j).SDTProperties.[Alias]
                    Dim tagT As String = tags(j).SDTProperties.Tag
                    If tagT = "RQ_Type" AndAlso aliaT = "RQ_Type" Then
                        For Each obj As DocumentObject In tags(j).ChildObjects
                            If TypeOf obj Is Paragraph Then
                                Dim par As Paragraph = TryCast(obj, Paragraph)
                                par.Text = "RQ_Type changed"
                            End If
                        Next
                        Continue For
                    End If
                    If tagT = "Message body:" AndAlso aliaT = "Message body:" Then

                        For Each obj As DocumentObject In tags(j).ChildObjects

                            If TypeOf obj Is Paragraph Then
                                Dim par As Paragraph = TryCast(obj, Paragraph)
                                par.Text = "Message body changed"
                            End If
                        Next
                        Continue For
                    End If
                    If tagT = "rq_user:" AndAlso aliaT = "rq_user:" Then
                        Dim par As Paragraph = TryCast(tags(j).ChildObjects.FirstItem, Paragraph)
                        'this sdt has been binding to Author, so it needs to change Author if you want to change it.
                        document.BuiltinDocumentProperties.Author = "rq_user changed"

                        Continue For
                    End If
                Next

                document.SaveToFile("13836.docx", FileFormat.Docx2013)
                System.Diagnostics.Process.Start("13836.docx")
            End Using
        End Sub
        Private Shared Function GetAllTags(ByVal document As Document) As StructureTags
            For Each section As Section In document.Sections
                Dim header As HeaderFooter = section.HeadersFooters.Header
                For Each obj As DocumentObject In header.ChildObjects
                    GetSDTInDocumentObject(obj)
                Next
                Dim footer As HeaderFooter = section.HeadersFooters.Footer
                For Each obj As DocumentObject In footer.ChildObjects
                    GetSDTInDocumentObject(obj)
                Next

                For Each obj As DocumentObject In section.Body.ChildObjects
                    GetSDTInDocumentObject(obj)
                Next
            Next
            Return structureTags
        End Function

        Public Shared Sub GetSDTInDocumentObject(ByVal obj As DocumentObject)
            If obj.DocumentObjectType = DocumentObjectType.Paragraph Then

                For Each pobj As DocumentObject In (TryCast(obj, Paragraph)).ChildObjects

                    If pobj.DocumentObjectType = DocumentObjectType.StructureDocumentTagInline Then
                        structureTags.tagInlines.Add(TryCast(pobj, StructureDocumentTagInline))
                    End If

                    If TypeOf pobj Is TextBox Then

                        For Each bobj As DocumentObject In pobj.ChildObjects

                            If TypeOf bobj Is StructureDocumentTag Then
                                structureTags.tags.Add(TryCast(bobj, StructureDocumentTag))
                            End If
                        Next
                    End If
                Next
            ElseIf TypeOf obj Is StructureDocumentTag Then
                structureTags.tags.Add(TryCast(obj, StructureDocumentTag))
            ElseIf obj.DocumentObjectType = DocumentObjectType.Table Then

                For Each row As TableRow In (TryCast(obj, Table)).Rows

                    For Each cell As TableCell In row.Cells

                        For Each cellChild As DocumentObject In cell.ChildObjects

                            If cellChild.DocumentObjectType = DocumentObjectType.StructureDocumentTag Then
                                structureTags.tags.Add(TryCast(cellChild, StructureDocumentTag))
                            ElseIf cellChild.DocumentObjectType = DocumentObjectType.Paragraph Then

                                For Each pobj As DocumentObject In (TryCast(cellChild, Paragraph)).ChildObjects

                                    If pobj.DocumentObjectType = DocumentObjectType.StructureDocumentTagInline Then
                                        structureTags.tagInlines.Add(TryCast(pobj, StructureDocumentTagInline))
                                    End If
                                Next
                            End If
                        Next
                    Next
                Next
            End If
        End Sub
    End Class
    Public Class StructureTags
        Private m_tagInlines As List(Of StructureDocumentTagInline)

        Public Property tagInlines As List(Of StructureDocumentTagInline)
            Get
                If m_tagInlines Is Nothing Then m_tagInlines = New List(Of StructureDocumentTagInline)()
                Return m_tagInlines
            End Get
            Set(ByVal value As List(Of StructureDocumentTagInline))
                m_tagInlines = value
            End Set
        End Property
        Private m_tags As List(Of StructureDocumentTag)
        Public Property tags As List(Of StructureDocumentTag)
            Get
                If m_tags Is Nothing Then m_tags = New List(Of StructureDocumentTag)()
                Return m_tags
            End Get
            Set(ByVal value As List(Of StructureDocumentTag))
                m_tags = value
            End Set
        End Property
    End Class

Any question, welcome to get it back to us.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Mon May 28, 2018 12:51 pm

i have this issue

by the way my application is WPF VB.NET

mando
 
Posts: 17
Joined: Wed May 16, 2018 6:05 pm

Tue May 29, 2018 1:48 am

Dear mando,

Thanks for your feedback.
The class TextBox is in the namespace Spire.Doc.Fields, please use "Spire.Doc.Fields.TextBox" or Import the namespace on the top of your project.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Wed May 30, 2018 2:09 pm

Thank u Betsy.jiang for Helping

when i applied ur code the Header Title not appear for RQ_Type field
just two appear the Message body and rq_user

plz help me to show it and change

mando
 
Posts: 17
Joined: Wed May 16, 2018 6:05 pm

Thu May 31, 2018 2:25 am

Dear mando,

Thanks for your feedback.
Please make sure you were using the latest version, and I have attached my sample project, please download it from following link and have a check.
http://www.e-iceblue.com/downloads/demo/13836WPFVB.zip

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Tue Jun 05, 2018 6:28 am

Dear mando,

Hope you are doing well.
Did you try the demo I provided ? Has your issue been resolved ?

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.Doc

cron