Highlight Words

Home Forums Open-Xml-PowerTools Highlight Words

This topic contains 0 replies, has 1 voice, and was last updated by  qu1ckdry 6 days, 22 hours ago.

Viewing 1 post (of 1 total)
  • Author
  • #5356


    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)
        End Using
    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!

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.