This is a migrated thread and some comments may be shown as answers.

Find / Replace in Header and Footer

6 Answers 177 Views
RichTextEditor
This is a migrated thread and some comments may be shown as answers.
Jeff
Top achievements
Rank 1
Jeff asked on 27 Jun 2019, 02:14 PM

I'm attempting to replace the use of MS Word in our application and I'm having trouble with one of the functions we need. We use DOCX templates and fill them with data from our application. Using Office Interop, I can set the header, footer or main document as the "SeekView" before a find replace like this - 

WordApp.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageHeader

WordApp.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageFooter

WordApp.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument

 

Is there any function for the RichTextEditor that can allow me to perform a find/replace on the header and footer? I can only get the find/replace code to work on the main body.

6 Answers, 1 is accepted

Sort by
0
Tanya
Telerik team
answered on 28 Jun 2019, 02:28 PM
Hi Jeff,

The implementation of RadRichTextBox is a bit different than the one provided by MS Office Interop and the API of the control doesn't include something similar to the SeekView.

Since the headers and footers in RadRichTextBox are implemented as instances of the RadDocument class and the FindReplace functionality works with a RadDocument instance, this is the default behavior of the functionality. However, you can customize it by handling the command and executing it over the headers and footers as well.

The headers and footers of a document can be obtained through the sections. Please, find below a sample code showing how you can obtain a header of a section. The approach for the footer is the same.
RadDocument defaultHeaderDocument = this.radRichTextBox.Document.Sections.First.Headers.Default.Body;

Additionally, we have logged a task to implement a similar functionality and you can vote to increase its priority as well as track its status using the related public item.

If you need further assistance in implementing this, please let us know.

Regards,
Tanya
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Jeff
Top achievements
Rank 1
answered on 28 Jun 2019, 02:39 PM
Thank you. I'm going to give this a try.
0
Jeff
Top achievements
Rank 1
answered on 28 Jun 2019, 05:59 PM

So here's what I've got and it doesn't work. The find replace doesn't find any of the search strings I specify even though I can see they are there. fine replace works great on the main body. I suppose I'm doing something wrong but not sure what to try next.

' This reads through an array with my search strings and the string to replace it with.
HeaderDocument = radRichTextEditor1.Document.Sections.First.Headers.Default.Body
                For X = 0 To UBound(TheData)
                    Try
                        DataArray = Split(TheData(X), "~")
                        FindReplaceHeader("{" & DataArray(0) & "}", DataArray(1))
                    Catch ex As Exception
                    End Try
                Next
                Dim TheHeader As New Header() With {.Body = HeaderDocument, .IsLinkedToPrevious = False}
                radRichTextEditor1.UpdateHeader(Me.radRichTextEditor1.Document.Sections.First, HeaderFooterType.Default, TheHeader)
 
'This is the fine replace code for the header.
 
    Private Sub FindReplaceHeader(ByVal SearchString As String, ByVal ReplaceWith As String)
        HeaderDocument.Selection.Clear()
        Dim search As New Telerik.WinForms.Documents.TextSearch.DocumentTextSearch(HeaderDocument)
        Dim rangesTrackingDocumentChanges As New List(Of Telerik.WinForms.Documents.TextSearch.TextRange)()
        For Each textRange In search.FindAll(SearchString)
            Dim newRange As New Telerik.WinForms.Documents.TextSearch.TextRange(New Telerik.WinForms.Documents.DocumentPosition(textRange.StartPosition, True), New Telerik.WinForms.Documents.DocumentPosition(textRange.EndPosition, True))
            rangesTrackingDocumentChanges.Add(newRange)
        Next textRange
        For Each textRange In rangesTrackingDocumentChanges
            HeaderDocument.Selection.AddSelectionStart(textRange.StartPosition)
            HeaderDocument.Selection.AddSelectionEnd(textRange.EndPosition)
            Me.radRichTextEditor1.Insert(ReplaceWith)
            textRange.StartPosition.Dispose()
            textRange.EndPosition.Dispose()
        Next textRange
    End Sub
0
Accepted
Tanya
Telerik team
answered on 01 Jul 2019, 10:04 AM
Hi Jeff,

The Insert() method of RadRichTextEditor adds the content at the current caret position. I would suggest you using the RadDocumentEditor class so that you can ensure you are inserting the content at the desired document part. I modified the code so I can build it and here is how it looks like after changing the way the content is being inserted:
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
    ' This reads through an array with my search strings and the string to replace it with.
    HeaderDocument = Me.richTextEditor.Document.Sections.First.Headers.Default.Body
    FindReplaceHeader("test", "result")
 
    Dim TheHeader As New Header() With {.Body = HeaderDocument, .IsLinkedToPrevious = False}
    richTextEditor.UpdateHeader(Me.richTextEditor.Document.Sections.First, HeaderFooterType.Default, TheHeader)
End Sub
 
'This is the fine replace code for the header.
Private Sub FindReplaceHeader(ByVal SearchString As String, ByVal ReplaceWith As String)
    HeaderDocument.Selection.Clear()
    Dim search As New DocumentTextSearch(HeaderDocument)
    Dim rangesTrackingDocumentChanges As New List(Of TextRange)()
    For Each textRange In search.FindAll(SearchString)
        Dim newRange As New TextRange(New DocumentPosition(textRange.StartPosition, True), New DocumentPosition(textRange.EndPosition, True))
        rangesTrackingDocumentChanges.Add(newRange)
    Next textRange
    For Each textRange In rangesTrackingDocumentChanges
        HeaderDocument.Selection.AddSelectionStart(textRange.StartPosition)
        HeaderDocument.Selection.AddSelectionEnd(textRange.EndPosition)
        Dim editor = New RadDocumentEditor(HeaderDocument)
        editor.Insert(ReplaceWith)
        textRange.StartPosition.Dispose()
        textRange.EndPosition.Dispose()
    Next textRange
End Sub

Does that work for you?

Regards,
Tanya
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Jeff
Top achievements
Rank 1
answered on 01 Jul 2019, 03:49 PM

This is working for me. Thank you!

I am running this 3 times, once each for Default, Even and First header. 

0
Tanya
Telerik team
answered on 03 Jul 2019, 01:57 PM
Hi Jeff,

Thank you for writing back. 

Indeed, you will need to run it for each additional RadDocument instance used. Something that worth mentioning here is that would be good to check whether the header/footer document is not null. Such case would be if the current document doesn't have applied header or footer.

Regards,
Tanya
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
RichTextEditor
Asked by
Jeff
Top achievements
Rank 1
Answers by
Tanya
Telerik team
Jeff
Top achievements
Rank 1
Share this question
or