Solved

Macro to Find & Replace Footer after section break

Posted on 2008-10-10
3
804 Views
Last Modified: 2012-05-05
Ok, i've a very simple macro that asks for user input on various things (such as Client Name) and uses this to run a find and replace through my body text.

As part of the document i have a section Break after the 1st page, which has a footer, a section break after the second page, which has no footer and from there on in there is a consistent footer.

A simple find and replace does not check the footer and when i add the instruction WordBasic.ViewFooterOnly it only checks the 1st footer when i need it to check after the second section break...

All help appreciated!
'This is my basic user input & find and replace
 
sPrompt = "Please enter Client Name"
    sTitle = "Client"
    sDefault = "CLIENT PROPERTIES LTD"
    sClient = InputBox(sPrompt, sTitle, sDefault)
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = """CLIENT"""
        .Replacement.Text = sClient
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
 
'This is where it should be checking the footer
 
sPrompt = "Please enter your Project Number"
    sTitle = "Project Number"
    sDefault = "Project Number"
    sPNo = InputBox(sPrompt, sTitle, sDefault)
    WordBasic.ViewFooterOnly
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = """Proj No"""
        .Replacement.Text = sPNo
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

Open in new window

0
Comment
Question by:NAORC
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 23

Expert Comment

by:irudyk
ID: 22692745
Try something like the following:
'This is my basic user input & find and replace
 
sPrompt = "Please enter Client Name"
    sTitle = "Client"
    sDefault = "CLIENT PROPERTIES LTD"
    sClient = InputBox(sPrompt, sTitle, sDefault)
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = """CLIENT"""
        .Replacement.Text = sClient
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
 
 
'This is where it should be checking the footer
Dim rng As Range
 
sPrompt = "Please enter your Project Number"
    sTitle = "Project Number"
    sDefault = "Project Number"
    sPNo = InputBox(sPrompt, sTitle, sDefault)
    For Each rng In ActiveDocument.StoryRanges
        Do
            With rng.Find
                .ClearFormatting
                .Replacement.ClearFormatting
                .Text = """Proj No"""
                .Replacement.Text = sPNo
                .Forward = True
                .Wrap = wdFindContinue
                .Format = False
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
                .Execute Replace:=wdReplaceAll
            End With
            Set rng = rng.NextStoryRange
        Loop Until rng Is Nothing
    Next rng

Open in new window

0
 
LVL 5

Author Comment

by:NAORC
ID: 22701280
Irudyk,

Thanks - Not to sound ungrateful, but while this solves the problem it takes about 2 minutes to run that section of the code. It's only a 14 page document (Office 2007). Any ideas on how it can be sped up at all?

Thanks!
0
 
LVL 23

Accepted Solution

by:
irudyk earned 125 total points
ID: 22702321
Hmm, okay well try modifying that section of the code to be:
    For Each rng In ActiveDocument.StoryRanges
        Select Case rng.Information(wdHeaderFooterType)
            Case 2, 3, 5    'range is in a footer section
                Do
                    With rng.Find
                        .ClearFormatting
                        .Replacement.ClearFormatting
                        .Text = """Proj No"""
                        .Replacement.Text = sPNo
                        .Forward = True
                        .Wrap = wdFindContinue
                        .Format = False
                        .MatchCase = False
                        .MatchWholeWord = False
                        .MatchWildcards = False
                        .MatchSoundsLike = False
                        .MatchAllWordForms = False
                        .Execute Replace:=wdReplaceAll
                    End With
                    Set rng = rng.NextStoryRange
                Loop Until rng Is Nothing
            Case Else
        End Select
    Next rng

Open in new window

0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is often necessary in this forum and others to illustrate Word fields as text with the field delimiters replaced with the curly brackets that the delimiters resemble when field codes are being displayed on the document. This means that the text c…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question