Highlight Words

Home Forums Open-Xml-PowerTools Highlight Words

This topic contains 1 reply, has 2 voices, and was last updated by  BrettSmith 7 months, 2 weeks ago.

Viewing 2 posts - 1 through 2 (of 2 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!



    Thanks mate for sharing this. I inserted this block of code in my site EssayWritingLibrary and got perfect results.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.