Reputation: 587
I'm using Open XML and I should change the text in the header of a word file. To change a specific paragraph in the document I have used the following code:
Dim body = wdDoc.MainDocumentPart.Document.Body
Dim paras = body.Elements(Of DocumentFormat.OpenXml.Wordprocessing.Paragraph)()
Dim header = body.Elements(Of DocumentFormat.OpenXml.Wordprocessing.Header)()
For Each para In paras
For Each run In para.Elements(Of DocumentFormat.OpenXml.Wordprocessing.Run)()
For Each testo In run.Elements(Of DocumentFormat.OpenXml.Wordprocessing.Text)()
If (testo.Text.Contains("<$doc_description$>")) Then
testo.Text = testo.Text.Replace("<$doc_description$>", "replaced-text")
End If
Next
Next
Next
thanks in advance!
Upvotes: 10
Views: 12540
Reputation: 1846
Ported to C# from Hans answer!
//Gets all the headers
foreach (var headerPart in doc.MainDocumentPart.HeaderParts)
{
//Gets the text in headers
foreach(var currentText in headerPart.RootElement.Descendants<DocumentFormat.OpenXml.Wordprocessing.Text>())
{
currentText.Text = currentText.Text.Replace("[Thanks]", "Thanks");
}
}
Upvotes: 17
Reputation: 13040
You can use the following code to replace a tag in the header of a word document:
Using wdDoc = WordprocessingDocument.Open("header.docx", True)
For Each headerPart In wdDoc.MainDocumentPart.HeaderParts
For Each currentParagraph In headerPart.RootElement.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Paragraph)()
For Each currentRun In currentParagraph.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Run)()
For Each currentText In currentRun.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Text)()
If (currentText.Text.Contains("$doc-description$")) Then
Console.WriteLine("found")
currentText.Text = currentText.Text.Replace("$doc-description$", "replaced-text")
End If
Next
Next
Next
Next
End Using
First, enumerate all HeaderParts
of the word document. Then search for all Text
elements
containing the tag to replace. Then replace the tag with your text.
Please note that you should use a tag without <>
and _
characters. If your
tag contains these characters then word splits the text among multiple Text
elements.
If you want to change the text in a table (or in any other element) just search
for all Text
elements:
Using wdDoc = WordprocessingDocument.Open("header.docx", True)
For Each headerPart In wdDoc.MainDocumentPart.HeaderParts
For Each currentText In headerPart.RootElement.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Text)()
If (currentText.Text.Contains("$doc-description$")) Then
Console.WriteLine("found")
currentText.Text = currentText.Text.Replace("$doc-description$", "replaced-text")
End If
Next
Next
End Using
Upvotes: 8
Reputation: 587
thanks for the reply actually works :) I also tried with the following code:
For Each headref In mainDoc.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.HeaderReference)() headerRelationshipId = headref.Id.Value headerType = headref.Type.Value.ToString() Dim header01 As DocumentFormat.OpenXml.Wordprocessing.Header = DirectCast(wdDoc.MainDocumentPart.GetPartById(headerRelationshipId), HeaderPart).Header Dim headerText As New StringBuilder() For Each text00 As DocumentFormat.OpenXml.Wordprocessing.Text In header01.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Text)() If (text00.Text.Contains("")) Then text00.Text = text00.Text.Replace("", "replaced-text") End If Next Next
But if I wanted to change the text in a table (instead of a paragraph)?
Upvotes: 0