Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text replacement in muliple files? 1

Status
Not open for further replies.

Pyrrhus

Technical User
Dec 18, 2001
112
AU
I have multiple .html files which are 90% identical content. Can anybody please tell me how to replace a specific block of text, common to all the files, with a different block of text, simultaneously in all of the files in the folder?

Thanks.
 
Try something based on:
Code:
Sub UpdateContents()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, wdDoc As Document
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.htm", vbNormal)
While strFile <> ""
  Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
  With wdDoc.Range.Find
    .ClearFormatting
    .Text = "Old String"
    .Replacement.Text = "New String"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = True
    .MatchWholeWord = True
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute Replace:=wdReplaceAll
  End With
  wdDoc.Close SaveChanges:=True
  strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub

Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
Simply replace the 'Old String' and 'New String' data with your own then run the macro.

Cheers
Paul Edstein
[MS MVP - Word]
 
Hi Paul,
Many thanks for your generous help with my problem. I haven't used macros / vba routines for a few years, and forgotten whatever I knew, and I'll have to work how to install and trigger it. For that reason, I haven't actually used it yet and can't report success, but I thought I'd better thank you first while I screw around with my incompetence :)
Thanks again,
Kevin Morrisey / "Pyrrhus".
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top