![]() |
#1
|
|||
|
|||
![]() 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. |
#2
|
||||
|
||||
![]()
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] |
#3
|
|||
|
|||
![]()
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 |
#4
|
||||
|
||||
![]()
I have already provided two links...
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
![]()
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 |
![]() |
Tags |
find & replace, find and replace, header and footer |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
amodiammmuneerk@glenmarkp | Word | 12 | 03-05-2018 03:31 AM |
![]() |
trillium | Word VBA | 4 | 10-20-2015 10:39 PM |
![]() |
kennethc | Word | 3 | 03-28-2015 02:49 AM |
![]() |
QA_Compliance_Advisor | Word VBA | 11 | 09-23-2014 04:40 AM |
![]() |
PReinie | Word | 6 | 01-22-2014 06:45 PM |