none
How to get notified after Find and Replace? RRS feed

  • Question

  • I'm using VSTO to create a Word application level addin that can save the contents from ContentControls into a database. For this I use the ContentControlOnExit event of the Microsoft.Office.Interop.Word.Document object.

    However, if a user uses Find & Replace to modify a document I don't get these ContentControlOnExit events and the changes aren't saved to the database. So I need some notification after a Find & Replace operation has taken place to update my database. Unfortunately I cannot find such an event.

    Any help appreciated!

    Guido

    Tuesday, March 11, 2014 11:48 AM

Answers

  • If by that you mean you need to know whether the user has done a Find/Replace through the built-in dialogue, or even via a macro outside your control, then no, there is no means of trapping that.

    About all that leaves you with is the task of implementing your own Find/Replace dialogue to replace the built-in one, or the ability to capture & compare the content control values when the document is opened & closed.

    If the Find/Replace is under your control, it's easy enough to ensure that process runs in such a way that you can test whether what is being replaced is inside a content control.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by Guido Kraus Tuesday, March 11, 2014 1:22 PM
    Tuesday, March 11, 2014 1:04 PM

All replies

  • You can call the Document_ContentControlOnExit macro from your other code. For example (VBA):

    Sub Demo()
    Dim CCtrl As ContentControl
    For Each CCtrl In ActiveDocument.ContentControls
      Application.Run "ThisDocument.Document_ContentControlOnExit", CCtrl, False
    Next
    End Sub

    In the context of a Find/Replace operation, you can test whether each found range is in a Content Control and, if so, use  Application.Run to run the ContentControlOnExit macro for that Content Control.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, March 11, 2014 12:26 PM
  • Thanks for your reply! I know how to save my ContentControls to a database. The problem is that I have no idea when to do that. Ideally there would be an 'OnAfterFindAndReplace' event which I could use - but I cannot find such an event. Somehow I need to detect if a Find & Replace operation has taken place.

    Guido

    Tuesday, March 11, 2014 12:35 PM
  • If by that you mean you need to know whether the user has done a Find/Replace through the built-in dialogue, or even via a macro outside your control, then no, there is no means of trapping that.

    About all that leaves you with is the task of implementing your own Find/Replace dialogue to replace the built-in one, or the ability to capture & compare the content control values when the document is opened & closed.

    If the Find/Replace is under your control, it's easy enough to ensure that process runs in such a way that you can test whether what is being replaced is inside a content control.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by Guido Kraus Tuesday, March 11, 2014 1:22 PM
    Tuesday, March 11, 2014 1:04 PM