Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 12-26-2016, 08:19 PM
Jude24Joy Jude24Joy is offline Windows 8 Office 2013
Novice
 
Join Date: Dec 2016
Posts: 10
Jude24Joy is on a distinguished road
Default Altering this VBA code to make several changes at once


I'm using the code here (https://www.extendoffice.com/documen...ple-files.html) to basically "Find and Replace" in 100 documents at a time. So far, I have 8 versions for each change I need to make, and I have to run them individually for each 100 files. This is still faster than doing this all manually, but I'd love to run all 8 changes at once. In the link above, someone made several comments in reply with directions to do this, but their code didn't work for me. I think there were a cut and pasting issues. When I pasted it, there were random spaces thrown in everywhere.

I feel like I'm pressing my luck--I already got a great answer to my first question here today. Forum contributors are so often my saving grace. I really appreciate the time you all put in. I hope to be as skilled someday to pass on the favor.
Reply With Quote
  #2  
Old 12-26-2016, 08:56 PM
gmayor's Avatar
gmayor gmayor is online now Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 1,793
gmayor will become famous soon enoughgmayor will become famous soon enough
Default

Create the 8 functions that you require and call them sequentially from your loop. You haven't said what the changes are, but if they are text replacements, you can use the Find and Replace (with table defined pairs) option in
__________________
Graham Mayor - MS MVP (Word)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 12-26-2016, 09:33 PM
Jude24Joy Jude24Joy is offline Windows 8 Office 2013
Novice
 
Join Date: Dec 2016
Posts: 10
Jude24Joy is on a distinguished road
Default

They are text replacements. That's a pretty cool add-in you've created!

I'd still like to know how to call the functions sequentially in the loop, though. I took a Introduction to Visual Basic class a few years ago, but I have trouble parsing out big chunks of code--especially the kind using loops!

I copied the text from the URL I included exactly for each variation, changing the name each time, CommandButton1_Click(), CommandButton2_Click(), etc. Part of my trouble is I don't know how to separate the part of the code that lets me select which files to run the code on from the part where I put in the text replacements.
Reply With Quote
  #4  
Old 12-27-2016, 01:29 AM
macropod's Avatar
macropod macropod is online now Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 15,013
macropod is a name known to allmacropod is a name known to allmacropod is a name known to allmacropod is a name known to allmacropod is a name known to allmacropod is a name known to all
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:
http://www.msofficeforums.com/word-v...html#post34254
http://www.msofficeforums.com/word-v...html#post93796
http://www.msofficeforums.com/word-v...html#post70765
http://www.msofficeforums.com/word-v...html#post31849
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
Reply
Please reply to this thread with any new information or opinions.

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
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
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 Column Widths on Two Col Page abrogard Word 2 12-30-2010 06:03 AM


All times are GMT -7. The time now is 01:36 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft