Oops meant header styles.
Hi
I think you did mean heading styles. For what it's worth, a header is
content that is repeated at the top of every page. And Word has a built-in
Header style that it uses by default for this purpose. A heading is a short
paragraph that announces the beginning of a portion of the text. Word has 9
built-in styles for this purpose: Heading 1, Heading 2, .... Heading 9.
So, as I understand it, you have a document with a style that is, say, L1.
And you want to search-and-replace it with style Heading 1. But when you do
that, the paragraphs don't display all the characteristics of Heading 1.
The likely cause is that the paragraphs are not only in style L1, they also
have direct formatting applied to them. You can test this out. To remove all
direct formatting, triple-click one of the L1 paragraphs to select it, and
then do ctrl-q and ctrl-spacebar. That removes all the paragraph-level and
font-level direct formatting. Did it change its look? If so, it had direct
formatting applied to it. Try this both before and after the
search-and-replace. Does that solve the problem?
If so, something like the following might help:
Option Explicit
Sub ReplaceL1WithHeading1()
Dim oDoc As Word.Document
Dim rng As Word.Range
Dim sStyleToFind As String
Dim sStyleToReplace As String
'Change these two lines to suit your needs
sStyleToFind = "L1"
sStyleToReplace = "Heading 1"
Set oDoc = ActiveDocument
Set rng = oDoc.Range.Duplicate
With rng.Find
.ClearAllFuzzyOptions
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Style = sStyleToFind
.Format = True
.Wrap = wdFindContinue
.Format = True
With .Replacement
.Text = ""
.Style = sStyleToReplace
End With
Do While .Execute(Replace:=wdReplaceOne)
'Reset the paragraph format
'(equivalent of ctrl-q)
rng.ParagraphFormat.Reset
'Reset the font format
'(equivalent of ctrl-spacebar)
rng.Font.Reset
Loop
End With
End Sub
Hope this helps.
Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
> Oops meant header styles.
>
[quoted text clipped - 39 lines]
>> can globally replace it neatly and cleanly. Anyone got any ideas?
>> Appreciate any help I can get.
jerem - 18 Aug 2007 18:42 GMT
Oops, meant heading styles - got it right in the subject matter, wrong in the
body of text. Is that like mixing metaphors???
I shall try this Monday and see if all goes well. I'm not sure if direct
formatting is the culprit since it seems that every heading level reacts the
same way to the global replacement and I can't imagine every paragraph in
that level being directly formatted since, generally speaking - I would
think, one would only directly format an individual heading level or an
isolated group of headings so that they maintain most of the heading
paragraph styling, but with something a little different about them specified
in the direct format (font setting, paragraph after, etc.).
I have one of those pain in the neck jobs sitting on my desk waiting for me.
I'll try your solution and let you know how it turned out. Thanks for your
quick reply.
jerem
> Hi
>
[quoted text clipped - 111 lines]
> >> can globally replace it neatly and cleanly. Anyone got any ideas?
> >> Appreciate any help I can get.
Shauna Kelly - 19 Aug 2007 00:27 GMT
Hi
I hope it goes well.
I forgot to point out in my previous reply that the code will zap all direct
formatting, even direct formatting you might want to keep. So a heading "The
sinking of the _Titanic_" would lose its italics.
Hope this helps.
Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
> Oops, meant heading styles - got it right in the subject matter, wrong in
> the
[quoted text clipped - 155 lines]
>> >> can globally replace it neatly and cleanly. Anyone got any ideas?
>> >> Appreciate any help I can get.
jerem - 22 Aug 2007 16:52 GMT
Hi Shauna,
Did what you suggested - that is, I globally found each style, highlighted
all of them in the find then ctrlq(ed) and ctlspace(d) them and then globally
replaced them. That worked well. Apparently you were right about the direct
formatting. Other document I was working on must have had direct formatting
all over the place. Have not used the code yet since I wanted to see what
went on step by step first but will try that with my next problem job.
Thanks for your help.
By the way, I was looking for a book that addresses VBA in a Word
environment. The only one that I know of is Writing Word Macros by Steven
Roman, however, that book gets mixed reviews. I don't like buying books for
$35 and then finding out that it really wasn't that helpful. Are you
familiar with this book? I want something that covers the elemental but
moves into more intermediate and advanced topics - and something that is user
friendly. Any suggestions?
> Hi
>
[quoted text clipped - 168 lines]
> >> >> can globally replace it neatly and cleanly. Anyone got any ideas?
> >> >> Appreciate any help I can get.
Shauna Kelly - 23 Aug 2007 05:01 GMT
Hi jerem
I don't personally know Steven Roman's book, but it is often recommended by
others.
Hope this helps.
Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
> Hi Shauna,
>
[quoted text clipped - 214 lines]
>> >> >> can globally replace it neatly and cleanly. Anyone got any ideas?
>> >> >> Appreciate any help I can get.