Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #4  
Old 12-27-2016, 01:29 AM
macropod's Avatar
macropod macropod is offline Altering this VBA code to make several changes at once Windows 7 64bit Altering this VBA code to make several changes at once Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

A similar approach, that doesn't require an addin is:
Code:
Sub UpdateDocuments()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, strDocNm As String, wdDoc As Document
strDocNm = ActiveDocument.FullName
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.doc", vbNormal)
While strFile <> ""
  If strFolder & "\" & strFile <> strDocNm Then
    Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
    With wdDoc
      Call FindRep(wdDoc, "String to Find", "String for Replacement")
      .Close SaveChanges:=True
    End With
  End If
  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

Sub FindRep(wdDoc As Document, StrFind As String, StrRep As String)
With wdDoc.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = StrFind
    .Replacement.Text = StrRep
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute Replace:=wdReplaceAll
  End With
End With
With the above macro, you can simply add as many lines with:
Call FindRep(wdDoc, "String to Find", "String for Replacement")
as you need. Elsewhere on this forum you'll find implementations that use Excel workbooks to hold the Find/Replace strings - and even one that incorporates the use of wildcards, text formatting, and so on and another that processes sub-folders as well. See, for example:
https://www.msofficeforums.com/word-...html#post34254
https://www.msofficeforums.com/word-...html#post93796
https://www.msofficeforums.com/word-...html#post70765
https://www.msofficeforums.com/word-...html#post31849
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Altering this VBA code to make several changes at once a code to add altering time to cells tesoke Excel Programming 3 12-17-2015 12:31 PM
Deleting a spare page without altering format SoLongKid Word 3 01-07-2015 11:34 PM
Altering this VBA code to make several changes at once Insert the page before table of content without altering it kbhalakiya Word 2 08-29-2012 03:53 AM
Can't make code work. gbaker Excel Programming 2 07-13-2012 10:01 PM
Altering this VBA code to make several changes at once Altering Column Widths on Two Col Page abrogard Word 2 12-30-2010 06:03 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:25 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft