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
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
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 SubThe 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 textRangeEnd SubDoes that work for you?
Regards,
Tanya
Progress Telerik
This is working for me. Thank you!
I am running this 3 times, once each for Default, Even and First header.
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
