Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 09-12-2014, 12:04 AM
QA_Compliance_Advisor QA_Compliance_Advisor is offline Created VBA to Find and Replace in Body, Header and Footer with Highlighting the replacement text Windows 7 32bit Created VBA to Find and Replace in Body, Header and Footer with Highlighting the replacement text Office 2010 32bit
Advanced Beginner
Created VBA to Find and Replace in Body, Header and Footer with Highlighting the replacement text
 
Join Date: Jul 2014
Posts: 44
QA_Compliance_Advisor is on a distinguished road
Default

With the Help of many different MVP and forums on here I have been able to find and replace from a excel doc list within the Body of a document which is automated to do every word document in a folder. Much appreciated guys.

However, I am having problems with getting the Header and Footer to actually do the same to find and replace from the excel doc.

I have attached the Code because it is so big in a txt file. it’s a bit messy, apologises in advance.



the code seems to process changes the body text but does nothing to the Header or Footer.

From: https://www.msofficeforums.com/word-...s-newpost.html

Code:
Sub UpdateDocuments()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, wdDoc As Document
Dim Sctn As Section, HdFt As HeaderFooter, Shp As Shape
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.doc", vbNormal)
While strFile <> ""
  Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, _
    AddToRecentFiles:=False, Visible:=False)
  With wdDoc
    'Process the body
    Call Update(.Range)
    'Process textboxes etc in the body
    For Each Shp In .Shapes
      With Shp.TextFrame
        If .HasText Then
          Call Update(.TextRange)
        End If
      End With
    Next
    For Each Sctn In .Sections
      For Each HdFt In Sctn.Headers
        With HdFt
          If .LinkToPrevious = False Then
            'Process the header
            Call Update(.Range)
          End If
        End With
      Next
    Next
    .Close SaveChanges:=True
  End With
  strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub
 
Sub Update(Rng As Range)
With Rng.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Text = "04-15-09"
  .Replacement.Text = "05-05-14"
  .Forward = True
  .Wrap = wdFindStop
  .Format = False
  .Execute Replace:=wdReplaceAll
End With
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
How would I incoroprate code inthe above wo allow find and replace from an excel Document to replace word in body, header and footer?
Reply With Quote
 

Tags
find & replace, header; footer; body, vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
Created VBA to Find and Replace in Body, Header and Footer with Highlighting the replacement text Find & Replace in Header/Footer in 1000 files amodiammmuneerk@glenmarkp Word 12 03-05-2018 03:31 AM
Created VBA to Find and Replace in Body, Header and Footer with Highlighting the replacement text Find & Replace in Header/Footer PReinie Word 6 01-22-2014 06:45 PM
Footer Find & Replace Operation? binar Word 1 02-05-2013 10:39 PM
Created VBA to Find and Replace in Body, Header and Footer with Highlighting the replacement text Insert a header name in the text (body) bal-007 Word 3 11-25-2011 01:08 PM
Find and replace page numbers in body of text tollanarama Word 3 02-13-2011 06:00 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:05 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