none
Multiple File- Find and Replace Module> Need fine tuning to only replace header section and not body RRS feed

  • Question

  • I found a macro that works perfectly for my job where I will be able to find and replace what I need in multiple files, however, I need someone to fine tune it to where it will only find and replace the first line of each page. Each page has the following as a "header" but not in the actual header field. See example below :

    File Number 12345     Vol. X       Sec. X         Issued Date: XXXX-XX-XX

    I do NOT want to replace "File 12345" anywhere else in the body of each page.  Here is the code:

    Sub CommandButton1_Click()
    Dim MyDialog As FileDialog, GetStr(1 To 100) As String '100 files is the maximum applying this code
    On Error Resume Next
    Set MyDialog = Application.FileDialog(msoFileDialogFilePicker)
    With MyDialog
    .Filters.Clear
    .Filters.Add "All WORD File ", "*.rtf", 1
    .AllowMultiSelect = True
    i = 1
    If .Show = -1 Then
    For Each stiSelectedItem In .SelectedItems
    GetStr(i) = stiSelectedItem
    i = i + 1
    Next
    i = i - 1
    End If
    Application.ScreenUpdating = False
    For j = 1 To i Step 1
    Set Doc = Documents.Open(FileName:=GetStr(j), Visible:=True)
    Windows(GetStr(j)).Activate
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
    .Text = "File 12345" 'Find What
    .Replacement.Text = "File 54321" 'Replace With
    .Forward = True
    .Wrap = wdFindAsk
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchByte = True
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    Application.Run macroname:="NEWMACROS"
    ActiveDocument.Save
    ActiveWindow.Close
    Next
    Application.ScreenUpdating = True
    End With
    MsgBox "operation end, please view", vbInformation
    End Sub

    Wednesday, February 15, 2017 3:30 PM

All replies

  • Hi Vee670,

    I made some changes in your code. please try to test it on your side.

    I tested it with one file and its working.

    Private Sub CommandButton1_Click()
    Dim MyDialog As FileDialog, GetStr(1 To 100) As String '100 files is the maximum applying this code
     On Error Resume Next
     Set MyDialog = Application.FileDialog(msoFileDialogFilePicker)
     With MyDialog
     .Filters.Clear
     .Filters.Add "All WORD File ", "*.rtf", 1
     .AllowMultiSelect = True
     Dim i, j As Integer
     Dim Doc As Document
     Dim stiSelectedItem As Variant
     i = 1
     If .Show = -1 Then
     For Each stiSelectedItem In .SelectedItems
     GetStr(i) = stiSelectedItem
     i = i + 1
     Next
     i = i - 1
     End If
     Application.ScreenUpdating = False
     For j = 1 To i Step 1
     Set Doc = Documents.Open(FileName:=GetStr(j), Visible:=True)
     Windows(GetStr(j)).Activate
     Doc.Paragraphs(1).Range.Select
     Selection.Find.ClearFormatting
     Selection.Find.Replacement.ClearFormatting
     With Selection.Find
     .Text = "File 12345" 'Find What
     .Replacement.Text = "File 54321" 'Replace With
     .Forward = False
     .Wrap = wdFindAsk
     .Format = False
     .MatchCase = False
     .MatchWholeWord = False
     .MatchByte = True
     .MatchWildcards = False
     .MatchSoundsLike = False
     .MatchAllWordForms = False
     End With
     Selection.Find.Execute Replace:=wdReplaceOne
     Application.Run macroname:="NEWMACROS"
     ActiveDocument.Save
     ActiveWindow.Close
     Next
     Application.ScreenUpdating = True
     End With
     MsgBox "operation end, please view", vbInformation
    End Sub

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, February 16, 2017 2:57 AM
    Moderator