Results 1 to 5 of 5
  1. #1
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    UK
    Posts
    239
    Thanks
    4
    Thanked 1 Time in 1 Post

    Macro does not run first time (Word 2000 SR1a)

    I have created a macro that is linked to a userform.

    The user form has fields for search and replace text. When the user clicks Search inc Sub Folders, a dialog box is presented allowing the user to select the folder on which the operation is to be performed.

    After the folder has been selected, the macro opens each html file as a text file, makes the change and saves it, then moves on to the next file.

    The first time it is run in any Word session, it only operates on the selected folder. The sub folders will be ignored. Run the macro again and it will work as required.

    Can anyone please identify why this happening and help me with modifying the code?

    The code is below.

    Many thanks

    *********************************


    Private Sub SearchIncSubFolders_Click()
    Dim strFolder As String
    Dim strSearch As String, strReplace As String

    strSearch = HTMLSR.txtSearch.Text
    strReplace = HTMLSR.txtReplace.Text

    With Dialogs(wdDialogCopyFile)
    If .Display = -1 Then strFolder = .Directory
    End With

    With Application.FileSearch
    .FileName = ".htm"
    .LookIn = strFolder
    .Execute
    .SearchSubFolders = True
    For i = 1 To .FoundFiles.Count
    WordBasic.disableautomacros
    Documents.Open .FoundFiles(i), Format:=wdOpenFormatText
    Selection.Find.ClearFormatting
    With Selection.Find
    .Forward = True
    .Wrap = wdFindStop
    .Text = strSearch
    .Replacement.Text = strReplace
    .Execute Replace:=wdReplaceAll
    End With
    ActiveDocument.Close wdSaveChanges
    Next i
    End With

    Unload HTMLSR

    MsgBox ("String Searched and Replaced")

    End Sub

  2. #2
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Macro does not run first time (Word 2000 SR1a)

    Try placing

    .SearchSubFolders = True

    before

    .Execute.

    and see if that helps.

    Andrew C

  3. #3
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    UK
    Posts
    239
    Thanks
    4
    Thanked 1 Time in 1 Post

    Re: Macro does not run first time (Word 2000 SR1a)

    Thanks Andrew, that fixed it.

    If the ability to search and replace a string in all html files in a folder (and its sub folders if required) is of use to anyone on the forum, mail me direct. It's all in a Word template. Only 41K download.

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Macro does not run first time (Word 2000 SR1a)

    Gosh, will it run inside FrontPage? 41K is small enough to post here if you're feeling generous.

  5. #5
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    UK
    Posts
    239
    Thanks
    4
    Thanked 1 Time in 1 Post

    Re: Macro does not run first time (Word 2000 SR1a)

    I tried importing the form and code from Word to FrontPage but it failed to work. I don't use Front Page much so it may be something simple.

    However, here's the Word template and obviously you can point the macro at your html files created by FrontPage, or anything else.

    Just double click the template and follow the instructions.
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •