Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-31-2016, 02:10 PM
mbreggs mbreggs is offline New to VBA - Find/Replace in MS Word 2010 that Searches Header/Footer Windows 10 New to VBA - Find/Replace in MS Word 2010 that Searches Header/Footer Office 2010 64bit
Novice
New to VBA - Find/Replace in MS Word 2010 that Searches Header/Footer
 
Join Date: May 2016
Posts: 3
mbreggs is on a distinguished road
Default New to VBA - Find/Replace in MS Word 2010 that Searches Header/Footer

Hi there,

I am relatively new to VBA and am recording a number of macros to automate my work. I have come across this problem: when I do a Find & Replace WITHOUT recording my Macro, the Find & Replace searches through the body, the headers, and the footers.

However, when I try to record the Macro, the Find & Replace tool does not search through the footer or header, just the body.



I am looking to update a template - using the same MS Word doc for a number of projects going forward and would be greatly appreciative if there is a work around to this problem.

If there is code for this problem, please pass it along!
If you could also be so kind to explain to me where the "Find" criteria will go in the code as well as the "Replace" criteria, I would greatly greatly appreciate it!

Thank you for your time in advance.
Reply With Quote
  #2  
Old 05-31-2016, 03:11 PM
macropod's Avatar
macropod macropod is offline New to VBA - Find/Replace in MS Word 2010 that Searches Header/Footer Windows 7 64bit New to VBA - Find/Replace in MS Word 2010 that Searches Header/Footer Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

There are plenty of code samples on this forum for using Find/Replace in headers & footers, etc. (e.g. https://www.msofficeforums.com/word-...html#post90012 https://www.msofficeforums.com/word-...s-footers.html) but it's not clear why you'd be needing that for a template. Templates should ordinarily be fairly static affairs, meaning there's be little occasion for changing their header/footer content.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 06-01-2016, 05:54 AM
mbreggs mbreggs is offline New to VBA - Find/Replace in MS Word 2010 that Searches Header/Footer Windows 10 New to VBA - Find/Replace in MS Word 2010 that Searches Header/Footer Office 2010 64bit
Novice
New to VBA - Find/Replace in MS Word 2010 that Searches Header/Footer
 
Join Date: May 2016
Posts: 3
mbreggs is on a distinguished road
Default Trying again

Sorry, I think I am using template incorrectly here. Mainly, I am looking to do an extensive find and replace with in a macro, and ideally need it to seamlessly sift through headers, footers, and the body to find and replace.

If you have any code that could point me in the right direction, I would greatly appreciate it
Reply With Quote
  #4  
Old 06-01-2016, 06:28 AM
macropod's Avatar
macropod macropod is offline New to VBA - Find/Replace in MS Word 2010 that Searches Header/Footer Windows 7 64bit New to VBA - Find/Replace in MS Word 2010 that Searches Header/Footer Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

Quote:
Originally Posted by mbreggs View Post
If you have any code that could point me in the right direction, I would greatly appreciate it
I have already provided two links...
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 06-01-2016, 08:02 AM
mbreggs mbreggs is offline New to VBA - Find/Replace in MS Word 2010 that Searches Header/Footer Windows 7 32bit New to VBA - Find/Replace in MS Word 2010 that Searches Header/Footer Office 2010 32bit
Novice
New to VBA - Find/Replace in MS Word 2010 that Searches Header/Footer
 
Join Date: May 2016
Posts: 3
mbreggs is on a distinguished road
Default

Thank you for your response macropod. This code ended up working for me perfectly:


Public Sub FindReplaceAnywhere()
Dim rngStory As Word.Range
Dim pFindTxt As String
Dim pReplaceTxt As String
Dim lngJunk As Long
Dim oShp As Shape
pFindTxt = InputBox("Enter the text that you want to find.", _
"FIND")
If pFindTxt = "" Then
MsgBox "Cancelled by User"
Exit Sub
End If
Tryagain:
pReplaceTxt = InputBox("Enter the replacement.", "REPLACE")
If pReplaceTxt = "" Then
If MsgBox("Do you just want to delete the found text?", vbYesNoCancel) = vbNo Then
GoTo Tryagain
ElseIf vbCancel Then
MsgBox "Cancelled by User."
Exit Sub
End If
End If
'Fix the skipped blank Header/Footer problem
lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryT ype
ResetFRParameters
'Iterate through all story types in the current document
For Each rngStory In ActiveDocument.StoryRanges
'Iterate through all linked stories
Do
SrcAndRplInStory rngStory, pFindTxt, pReplaceTxt
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
SrcAndRplInStory oShp.TextFrame.TextRange, _
pFindTxt, pReplaceTxt
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 Sub
Public Sub SrcAndRplInStory(ByVal rngStory As Word.Range, _
ByVal strSearch As String, _
ByVal strReplace As String)
With rngStory.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = strSearch
.Replacement.Text = strReplace
.Execute Replace:=wdReplaceAll
End With
End Sub
Sub ResetFRParameters()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub
Reply With Quote
Reply

Tags
find & replace, find and replace, header and footer

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
New to VBA - Find/Replace in MS Word 2010 that Searches Header/Footer Find & Replace in Header/Footer in 1000 files amodiammmuneerk@glenmarkp Word 12 03-05-2018 03:31 AM
New to VBA - Find/Replace in MS Word 2010 that Searches Header/Footer Find/replace font colour in all header/footer trillium Word VBA 4 10-20-2015 10:39 PM
New to VBA - Find/Replace in MS Word 2010 that Searches Header/Footer Find & replace footer text in a folder of Word 2010 documents kennethc Word 3 03-28-2015 02:49 AM
New to VBA - Find/Replace in MS Word 2010 that Searches Header/Footer Created VBA to Find and Replace in Body, Header and Footer with Highlighting the replacement text QA_Compliance_Advisor Word VBA 11 09-23-2014 04:40 AM
New to VBA - Find/Replace in MS Word 2010 that Searches Header/Footer Find & Replace in Header/Footer PReinie Word 6 01-22-2014 06:45 PM

Other Forums: Access Forums

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


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