I’m using OpenXml and OpenXmlPowerTools to try and highlight words in a word file. I can find them ok but I can for the life of me work out how to highlight the text once I’ve found it!
Here’s my code:
Public Sub Highlight()
Dim docFilePath As String = "test.docx"
Using wDoc As WordprocessingDocument = WordprocessingDocument.Open(docFilePath, True)
Dim xDoc As XDocument = wDoc.MainDocumentPart.GetXDocument()
Dim count As Integer = OpenXmlRegex.Match(xDoc.Descendants(W.p), New Regex("Test"),
Function(element, match)
Dim runProperties As RunProperties = New RunProperties(New Highlight() With {.Val = HighlightColorValues.Yellow})
'Dim r As Run = element '<-- I need to to something else here as this obviously fails
r.PrependChild(Of RunProperties)(runProperties)
Return True
End Function)
wDoc.MainDocumentPart.PutXDocument()
End Using
Process.Start(docFilePath)
End Sub
So my question is how do I apply run properties to an XElement? Or alternatively is there a better approach?
Thanks for any help!