PDA

View Full Version : Solved: Find and Replace in Headers



pimpzter
02-25-2013, 08:04 AM
I have a bunch of word docs to make corrections to. One of the things I need to do with each document is change the headers. Every page has a header that states "Revision ###" and I need to find and replace that with "Revision ###+1"

For example. Page 1 header states "Revision 301" I need to find and replace every "Revision 301" with "Revision 302"

thanks in advance
:banghead:

fumei
02-25-2013, 12:35 PM
What have you tried so far.

pimpzter
02-25-2013, 12:41 PM
I havent found this particular question anywhere so I searched similar questions and copy/pasted bits and pieces or different codes but nothing that has come even close to working. As I tried other things I found I just overwrote what didnt work previously so Im not sure to tell you what all I have tried so far. As you can tell im pretty new to this. If I was not detailed enough in what Im trying to accomplish please let me know.

fumei
02-25-2013, 03:58 PM
You have actually tried some code...well then, try posting that.

It actually is a fairly common thing to do. Break it down into two major chunks.

1. Doing it once.
2. Doing it with a bunch of files. This is easy; use the DIR function.

Sub ChangeHeader()
Dim WhatIsLeft As String
WhatIsLeft = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text
WhatIsLeft = Mid(WhatIsLeft, 9)
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text = _
"Revision " & WhatIsLeft + 1
End SubGets the number - what is left after "Revision " - adds 1 to it; replaces the header text.

Revision 301 becomes Revision 302.

You do NOT state:

1. are you only using Primary header - no DifferentFirstpage or OddEven
2. whether there is any other text.

The code above assumes yes to #1, no to #2.

"I havent found this particular question anywhere so I searched similar questions and copy/pasted bits and pieces or different codes but nothing that has come even close to working"

Hmmm. Like I stated, this is a common request (changing header content), so there is lots of code out there, that should have at least shown you something. As it is, the code I posted is possibly more complicated than needed. Unless of course your headers are more complicated than you are stating.

pimpzter
02-26-2013, 04:34 AM
1. are you only using Primary header - no DifferentFirstpage or OddEven

the header is the same on every page. I have been , without code, just find and replace 'revision xxx' with revision xxx+1' manually.


2. whether there is any other text.

there is other text in the header. The header is a table with 4 cells. The cell that states the Revision number is the lower right cell. There are attachments in the documents and on the attachment pages the 'Revision ###' is in the footer. The footer has a 3 cell table and the revision is in the left cell with other text before the Revision number.


The code above assumes yes to #1, no to #2.



Hmmm. Like I stated, this is a common request (changing header content), so there is lots of code out there, that should have at least shown you something. As it is, the code I posted is possibly more complicated than needed. Unless of course your headers are more complicated than you are stating.


the code you posted gave me an error and highlighted
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text = _
"Revision " & WhatIsLeft + 1

hover mouse over wdHeaderFooterPrimary and says "=1"
hover mouse over WhatIsLeft and says "what is left= and list all the other text in the header with the exception of the first letter.


thank you for your help and time.

pimpzter
02-26-2013, 05:59 AM
tried

Dim myStoryRange As Range

For Each myStoryRange In ActiveDocument.StoryRanges
With myStoryRange.Find
.Text = ("Revision " & 3)
.Replacement.Text = ("Revision " & 1)
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Next myStoryRange

but it only takes Revision 301 and changes it to Revision 101

so then I tried

Dim myStoryRange As Range

For Each myStoryRange In ActiveDocument.StoryRanges
With myStoryRange.Find

.Text = "Revision " & "([^#]{3})"
.Replacement.Text = "+1"
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Next myStoryRange

but it dont seem to be doing anything

macropod
02-26-2013, 09:00 AM
Here are two approaches to the problem:
Sub Demo1()
Application.ScreenUpdating = False
Dim Sctn As Section, HdFt As HeaderFooter
For Each Sctn In ActiveDocument.Sections
For Each HdFt In Sctn.Headers
If HdFt.LinkToPrevious = False Then
With HdFt.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "Revision [0-9]{1,}"
.Replacement.Text = ""
.MatchWildcards = True
.Format = False
.Forward = True
.Wrap = wdFindStop
.Execute
End With
Do While .Find.Found
.Text = Replace(.Duplicate.Text, Split(.Duplicate.Text, " ")(1), Split(.Duplicate.Text, " ")(1) + 1)
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
End If
Next
Next
Application.ScreenUpdating = True
End Sub
Sub Demo2()
Application.ScreenUpdating = False
Dim Rng As Range
With ActiveDocument
For Each Rng In .StoryRanges
Select Case Rng.StoryType
Case 6, 7, 10
With Rng
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "Revision [0-9]{1,}"
.Replacement.Text = ""
.MatchWildcards = True
.Format = False
.Forward = True
.Wrap = wdFindStop
.Execute
End With
Do While .Find.Found
.Text = Replace(.Duplicate.Text, Split(.Duplicate.Text, " ")(1), Split(.Duplicate.Text, " ")(1) + 1)
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
End Select
Next
End With
Application.ScreenUpdating = True
End Sub

pimpzter
02-26-2013, 10:12 AM
Both of these codes are outstanding!! thanks for the help. They do change the revision to the next revision on almost all the pages. There are some at the bottom of the document that is does not though. The ones in the last few pages are in the footer and to make the find and replace work even manually for these I have to click the small ball on the right side below the scrolling bar
" select browse object" then "browse by section". Then I can manually use the find and replace to work on the entire document. Is there code to make this selection to run before the main chunk of code you provided?

thank you again



Here are two approaches to the problem:
Sub Demo1()
Application.ScreenUpdating = False
Dim Sctn As Section, HdFt As HeaderFooter
For Each Sctn In ActiveDocument.Sections
For Each HdFt In Sctn.Headers
If HdFt.LinkToPrevious = False Then
With HdFt.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "Revision [0-9]{1,}"
.Replacement.Text = ""
.MatchWildcards = True
.Format = False
.Forward = True
.Wrap = wdFindStop
.Execute
End With
Do While .Find.Found
.Text = Replace(.Duplicate.Text, Split(.Duplicate.Text, " ")(1), Split(.Duplicate.Text, " ")(1) + 1)
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
End If
Next
Next
Application.ScreenUpdating = True
End Sub
Sub Demo2()
Application.ScreenUpdating = False
Dim Rng As Range
With ActiveDocument
For Each Rng In .StoryRanges
Select Case Rng.StoryType
Case 6, 7, 10
With Rng
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "Revision [0-9]{1,}"
.Replacement.Text = ""
.MatchWildcards = True
.Format = False
.Forward = True
.Wrap = wdFindStop
.Execute
End With
Do While .Find.Found
.Text = Replace(.Duplicate.Text, Split(.Duplicate.Text, " ")(1), Split(.Duplicate.Text, " ")(1) + 1)
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
End Select
Next
End With
Application.ScreenUpdating = True
End Sub

fumei
02-26-2013, 12:32 PM
For later possible threads you may post...

Please try and give all relevant information. The fact the header has a table is extremely significant. My suggestion - as I stated - works fine if there is nothing but WHAT YOU STATED: "Every page has a header that states "Revision ###" and I need to find and replace that with "Revision ###+1"

As for you code: but it only takes Revision 301 and changes it to Revision 101

Yes, because that is what you told it to do.

pimpzter
02-26-2013, 12:59 PM
Thank you for the advice and the input.

One thing I just noticed is it takes Revision 008 and makes it Revision 9. It omits the 00. Is this an easy fix?

Additionally, I am not sure what is in the file that makes it where I have to hit the small ball on the right side below the scrolling bar "select browse object" then "browse by section" to be able to manually use the find and replace to work on the entire document much less the code you gave.

again
thanks for your time and effort

macropod
02-26-2013, 02:07 PM
They do change the revision to the next revision on almost all the pages. There are some at the bottom of the document that is does not though.
...
Is there code to make this selection to run before the main chunk of code you provided?
The issue there is almost certainly that the revsion # is not in the form of 'Revision'<single space>'#'. I suspect you'll have multiple spaces between 'Revision' and the number, or perhaps a hard space. You should decide what ought to be there, so the process can correct it.

One thing I just noticed is it takes Revision 008 and makes it Revision 9. It omits the 00. Is this an easy fix?
Yes. Clarify the first point and I'll look at refining the code.

PS: There is no need to quote large blocks of code, etc in your replies. If you need to refer to something in a previous post, quote only the pertinent bits (as I've done above).

pimpzter
02-26-2013, 02:31 PM
The issue there is almost certainly that the revision # is not in the form of 'Revision'<single space>'#'. I suspect you'll have multiple spaces between 'Revision' and the number, or perhaps a hard space. You should decide what ought to be there, so the process can correct it.

I double checked. The ones that arent getting changed are singles spaced 'Revision' and the number. Im not sure what hard space is. I went in the footer by double clicking and backspaced till revision and the number were touching then went between the 2 and hit the space bar. I wish there was a way to shorten the doc to about 4 pages and send it to you so you can see what changes and what dont. Im sure my explanation isnt as clear as it should be.



Yes. Clarify the first point and I'll look at refining the code.


I appreciate it. Ive been tweaking with it but i dont even know a fraction what yall do.. lol

macropod
02-26-2013, 04:57 PM
Either macro will update all revision numbers if they're in the form of 'Revision'<single space>'#'. Anything else will be left alone - it won't update:
• revision 009
• Revision 009
• Revisions 009
• Revision<non-breaking space>009
• Revision<tab>009
• etc.
It also won't do anything if the content is in a textbox (even if that's in the header).

If you click on the ¶ symbol on the Ribbon's Home tab, that toggles Word's formatting display on/off. Click on it so you can see the ¶ symbol at the end of every paragraph. Now, between the problem 'Revision' and its number, what do you see? There should be just a single dot.

pimpzter
02-27-2013, 05:52 AM
If you click on the ¶ symbol on the Ribbon's Home tab, that toggles Word's formatting display on/off. Click on it so you can see the ¶ symbol at the end of every paragraph. Now, between the problem 'Revision' and its number, what do you see? There should be just a single dot.

9605

pimpzter
02-27-2013, 06:53 AM
Would you like me to attach the word.doc leaving only 2 pages.. 1 that the code is working for and 1 that it is not ?

macropod
02-27-2013, 01:26 PM
Would you like me to attach the word.doc leaving only 2 pages.. 1 that the code is working for and 1 that it is not ?
That might be useful

pimpzter
02-27-2013, 01:39 PM
That might be useful

9606

as you will see in this one:

"Revision 008" goes to "Revision 9" in a majority of the pages AND
in the last couple it remains unchanged. The ones that dont change at all are in the footer. To clarify.. I am trying to get them all to go to "Revision 009"

macropod
02-27-2013, 02:08 PM
The code works fine.

Without placing too fine a point on it, you asked for code to replace content in headers. On page 1 you have it in the document body and on pages 12 & 13 it's in the footer.

Furthermore, you could manage the whole thing without recourse to macros at all. Simply apply a unique Style to the refernce on page 1, then use StyleRef fields in the headers & footers to refer to that Style. From then on, immediately you update the # in the body of the document, the update will be reflected in the headers & footers.

pimpzter
02-28-2013, 07:28 AM
The code works fine.

Without placing too fine a point on it, you asked for code to replace content in headers. On page 1 you have it in the document body and on pages 12 & 13 it's in the footer.

We have already established the fact that there are Revisions's in footers if you refer to #5 and has been restated numerous times. I dont know what you were getting at but if you are, in your own way, asking me to mark this thread as resolved and start a new one just say so and I will be more than happy to do that for you.




Furthermore, you could manage the whole thing without recourse to macros at all. Simply apply a unique Style to the refernce on page 1, then use StyleRef fields in the headers & footers to refer to that Style. From then on, immediately you update the # in the body of the document, the update will be reflected in the headers & footers.

Your suggestion does sound like a more logical way to do thing, I completely agree. The issue is that we have 1,000's of documents and as they get revised I would have to do this to every one, therefore I just assumed a macro would be the easiest or quickest way to go about doing this. Is there not a minor adjustment that could be made to have the code provided make the changes in the footers? Additionally, I was still looking for a way, In the Header, to have the change not omit the 00 if it is a Revision less than 10.

Again, I do appreciate the help you have provided and definitely your time that im sure you could have spent doing numerous other things.

update: Under the code you provided I pasted a duplicate copy and revised 'headers' to 'footers'. Now it makes the changes to both the headers and footers. The only issue im still having is when it changes "Revision 008" to "Revision 9".

macropod
02-28-2013, 08:02 AM
We have already established the fact that there are Revisions's in footers if you refer to #5 and has been restated numerous times.
And, as the code I posted clearly shows, it processes headers only. What, for example, do you suppose 'Sctn.Headers' and StoryTypes 6, 7 & 10 refer to?

The issue is that we have 1,000's of documents and as they get revised I would have to do this to every one, therefore I just assumed a macro would be the easiest or quickest way to go about doing this.
It is dangerous to assume anything about something you haven't actually tried. The StyleRef approach is simpler and more reliable by far than having to rely on someone running a macro (once and once only) for every revision. Furthermore, if your other documents have the same layout as the one you attached, applying the StyleRef approach to all of them could be done programmatically as a once-off exercise.

pimpzter
02-28-2013, 08:33 AM
And, as the code I posted clearly shows, it processes headers only. What, for example, do you suppose 'Sctn.Headers' and StoryTypes 6, 7 & 10 refer to?

Ok since we are stuck on the Header topic... Do you have any suggestions for '008' to become '009' rather than '9' ?

If not I will mark this thread as Solved

macropod
02-28-2013, 08:38 AM
.Text = Format(Replace(.Duplicate.Text, Split(.Duplicate.Text, " ")(1), Split(.Duplicate.Text, " ")(1) + 1),"000")

pimpzter
02-28-2013, 09:26 AM
.Text = Format(Replace(.Duplicate.Text, Split(.Duplicate.Text, " ")(1), Split(.Duplicate.Text, " ")(1) + 1),"000")


Thank you for the quick response but I still get the same result.

macropod
02-28-2013, 05:41 PM
Try:
.Text = Replace(.Duplicate.Text, Split(.Duplicate.Text, " ")(1), Format(Split(.Duplicate.Text, " ")(1) + 1, "000"))

pimpzter
03-04-2013, 04:46 AM
Thank you again I will go try that right now.

same result. it still takes 'revision 008' and makes it 'revision 9'

ive tried altering what you sent but nothing i tried works not that is an educated effort.. more of guessing and trial and error. Maybe this part cant be done.. would it be easier for me to use the code you sent then copy/paste it 9 times replacing any 'revision 1' with 'revision 001' then 'revision 2' with 'revision 002' and so on ?

macropod
03-04-2013, 05:03 AM
Works fine for me ...

pimpzter
03-04-2013, 05:31 AM
hmmm... think its a reference I need to add or something?

macropod
03-04-2013, 05:33 AM
Maybe you should check you're actually using the code from the last update.

pimpzter
03-04-2013, 06:47 AM
Maybe you should check you're actually using the code from the last update.


I figured it out. I had your code duplicated.. one for headers and one for footers and I had only updated the headers part of it. It works like a champ now!! you are the man. thank you for your time, skill, and patience!!

macropod
03-04-2013, 09:48 PM
If you'd used the Select Case approach, that wouldn't have been an issue. For the Case condition, you'd use 'Case 6 - 11'.