+ Reply to Thread
Results 1 to 8 of 8

VBA Find and Replace multiple word doc throughout entire doc story including header/body

  1. #1
    Registered User
    Join Date
    04-29-2020
    Location
    Monroe, NY
    MS-Off Ver
    10
    Posts
    5

    VBA Find and Replace multiple word doc throughout entire doc story including header/body

    Hi-- I am able to use the below VBA to replace words in the body of a word doc for multiple files and it works great but only for the body of the documents. I need to be able to update the Header as well.
    I have tried various attempts to add in a range story to the code but have been unsuccessful.
    Please Login or Register  to view this content.
    Moderator's note: Please take the time to review our rules. There aren't many, and they are all important. Rule #2 requires code tags. I have added them for you this time because you are a new member. --6StringJazzer
    Last edited by 6StringJazzer; 04-29-2020 at 12:05 PM.

  2. #2
    Registered User
    Join Date
    04-29-2020
    Location
    Monroe, NY
    MS-Off Ver
    10
    Posts
    5

    Re: VBA Find and Replace multiple word doc throughout entire doc story including header/bo

    I figured it out-- whooo hooo -- this allows me to find and replace a word in a word document either in the body or the header.

    Sub FindAndReplaceInFolder()
    Dim objDoc As Document
    Dim strFile As String
    Dim strFolder As String
    Dim strFindText As String
    Dim strReplaceText As String
    Dim rngStory As Word.Range

    ' Pop up input boxes for user to enter folder path, the finding and replacing texts.
    strFolder = InputBox("Enter folder path here:")
    strFile = Dir(strFolder & "\" & "*.docx", vbNormal)
    strFindText = InputBox("Enter finding text here:")
    strReplaceText = InputBox("Enter replacing text here:")

    ' Open each file in the folder to search and replace texts. Save and close the file after the action.
    While strFile <> ""
    Set objDoc = Documents.Open(FileName:=strFolder & "\" & strFile)
    With objDoc
    With Selection
    .HomeKey Unit:=wdStory
    With Selection.Find
    .Text = strFindText
    .Replacement.Text = strReplaceText
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    For Each rngStory In ActiveDocument.StoryRanges
    'Iterate through all linked stories
    Do
    SearchAndReplaceInStory rngStory, strFindText, strReplaceText
    On Error Resume Next
    Select Case rngStory.StoryType
    Case 6, 7, 8, 9, 10, 11
    If rngStory.ShapeRange.Count > 0 Then
    For Each oShp In rngStory.ShapeRange
    If oShp.TextFrame.HasText Then
    SearchAndReplaceInStory oShp.TextFrame.TextRange, _
    strFindText, strReplaceText
    End If
    Next
    End If
    Case Else
    'Do Nothing
    End Select
    On Error GoTo 0
    'Get next linked story (if any)
    Set rngStory = rngStory.NextStoryRange
    Loop Until rngStory Is Nothing
    Next
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    End With
    objDoc.Save
    objDoc.Close
    strFile = Dir()


    End With
    Wend
    End Sub

  3. #3
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner VA USA
    MS-Off Ver
    MS 365 Family 64-bit 2505
    Posts
    27,563

    Re: VBA Find and Replace multiple word doc throughout entire doc story including header/bo

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however you need to include code tags around your code. I added them for you the first time with a request to read the rules.

    Please take a moment to add the tags. Posting code between tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    (Note: this change is not optional. No help to be offered until this moderation request has been fulfilled.)
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  4. #4
    Registered User
    Join Date
    04-29-2020
    Location
    Monroe, NY
    MS-Off Ver
    10
    Posts
    5

    Re: VBA Find and Replace multiple word doc throughout entire doc story including header/bo

    I don't see where tags were added nor do I know what exactly you are looking for. If you can provide full detail on what exactly you are requesting along with a sample, then I can certainly look to implement into my inquiry's.

    Thanks,
    Audie

  5. #5
    Registered User
    Join Date
    04-29-2020
    Location
    Monroe, NY
    MS-Off Ver
    10
    Posts
    5

    Re: VBA Find and Replace multiple word doc throughout entire doc story including header/bo

    "SOLVED ----- I missed the ending of the VBA when I posted it, this is the working code--


    'Use below to update words in documents--- can either be in a header or in the body of the text.

    Please Login or Register  to view this content.
    Last edited by 4321aud; 05-08-2020 at 11:35 AM. Reason: Missing tag

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner VA USA
    MS-Off Ver
    MS 365 Family 64-bit 2505
    Posts
    27,563

    Re: VBA Find and Replace multiple word doc throughout entire doc story including header/bo

    There are two ways to add code tags. One is to select the code part of the text so it is highlighted, then press the "#" button in the edit controls. The other is to simply type in the tags:

    [code]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/code]


    This information is available in the rule.

  7. #7
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2509 (Windows 11 Home 24H2 64-bit)
    Posts
    92,773

    Re: VBA Find and Replace multiple word doc throughout entire doc story including header/bo

    Quote Originally Posted by 4321aud View Post
    I don't see where tags were added nor do I know what exactly you are looking for. If you can provide full detail on what exactly you are requesting along with a sample, then I can certainly look to implement into my inquiry's.

    Thanks,
    Audie
    Read the very detailed instructions you were given in post #3 and then add the tags to the posts here where you didn’t do so.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  8. #8
    Registered User
    Join Date
    04-29-2020
    Location
    Monroe, NY
    MS-Off Ver
    10
    Posts
    5

    Re: VBA Find and Replace multiple word doc throughout entire doc story including header/bo

    So I get demerited for not understanding -- got it-- by the way -- the code I have left will be very useful for someone who needs to be able to process this type of activity.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Macro for find and Replace in multiple Word documents at a time
    By Pushpalathabh in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-12-2019, 05:24 AM
  2. Replies: 1
    Last Post: 02-21-2016, 02:39 AM
  3. [SOLVED] Help with macro to find a specific word and replace the entire cell with the return word.
    By BG1983 in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 08-24-2015, 09:40 PM
  4. [SOLVED] Find & replace text in a table nested within a header for a batch of word files
    By Tino XXL in forum Word Programming / VBA / Macros
    Replies: 2
    Last Post: 09-29-2014, 08:38 AM
  5. Find multiple words and replace the adjacent column with one word
    By ic3cold in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-13-2011, 05:04 PM
  6. Create a word template including header and footer from excel
    By priyamvada in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-14-2010, 12:51 AM
  7. [SOLVED] How do I replace the last entire word in text cells?
    By George in forum Excel General
    Replies: 7
    Last Post: 12-15-2005, 05:35 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1