none
Using wdDialogEditReplace to replace known values throughout a document RRS feed

  • Question

  • I am trying to replace a list of placeholder text with values in a Word document.  We need the values to be replaced in the body, all headers and footers, as well as in tables, text boxes, and other locations.

    We have an elaborate mechanism of looping through all stories and performing find/replace within those stories.  We have had this working for the most part, but it is slower than we'd like it to be.

    It is painfully apparent that Word's find/replace functionality has always been faster and more reliable than using the VBA that is behind the scenes.

    We were recently attempting to use Word's Replace dialog (wdDialogEditReplace) to perform this functionality.  We do not need or want to actually display this dialog (as we already know the values to replace, and do not want to require end user interaction).  However, we are still attempting to use the dialog to improve performance over our existing implementation.  To do this, we are setting properties on the dialog, and then calling the .Execute command to perform the replacement.  This seems to work fine for text in the body of the document, but we are thus far unable to get this to work well across the entire document.

    From the Word 2010 interface, the visible Replace dialog has a Search dropdown with the values Up, Down, and All.  When we select All from the interface, and click Replace All, this seems to do roughly what we need it to do.  However, we could not recreate this programmatically.  There is no "Search" property available in code for this dialog.  However, there is a "Direction" property, which supports 0 for down, and 1 for up, but we cannot deduce what to set in the properties of the wdDialogEditReplace dialog to match the "All" functionality.

    Here's a simple example of the code we were trying to use:

        With Dialogs(wdDialogEditReplace)
            .Find = "<<placeholder>>"
            .Replace = "VALUE"
            .Direction = 0 '0 = down, 1 = up
            .findnext = wdFindContinue 
            .Format = True
            .MatchCase = False
            .wholeword = True
            .PatternMatch = False
            .SoundsLike = False
            .findallwordforms = False
            .Wrap = 1 '0 = no, 1 = yes, 2 = ask
            .MatchByte = False
            .FuzzyFind = False
            .MatchKashida = False
            .MatchDiacritics = False
            .MatchAlefHamza = False
            .MatchControl = False
            .ReplaceAll = True
            .Execute
        End With
    Can anyone point me to what I'm doing wrong if this is even possible?

    Monday, November 26, 2012 7:56 PM

All replies

  • The code below I  modify from http://msdn.microsoft.com/en-us/library/bhy5dcs0.aspx

            Dim dialog As Word.Dialog = Application.Dialogs(Word.WdWordDialog.wdDialogEditReplace)

            With dialog
                .Find = "<<placeholder>>"
                .Replace = "VALUE"
                .Direction = 0 '0 = down, 1 = up
                '.findnext = wd wdFindContinue
                .Format = True
                .MatchCase = False
                .wholeword = True
                .PatternMatch = False
                .SoundsLike = False
                .findallwordforms = False
                .Wrap = 1 '0 = no, 1 = yes, 2 = ask
                .MatchByte = False
                .FuzzyFind = False
                .MatchKashida = False
                .MatchDiacritics = False
                .MatchAlefHamza = False
                .MatchControl = False
                .ReplaceAll = True
                .Execute()
            End With

    Thursday, December 4, 2014 4:25 PM