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
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
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