Macro Find and Replace with styles

C

Chris

Hi,

i have a macro which formats the text between Html-Tags like <U1>Text</U1>
with standard header styles. The problem is that my whole document will be
replaced with this format style. But i only want to format the text between
the HTML-Tags.
Her is my snippet:

With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = True
.MatchWildcards = True
.Text = "(\<U1\>)(*)(\</U1\>)"
.Replacement.Text = "\2"
.Replacement.Style =
ActiveDocument.Styles(WdBuiltinStyle.wdStyleHeading1)
.Execute Replace:=wdReplaceAll
End With

Many thanks for any advice...

Chris
 
E

Edward Thrashcort

I remember having a similar problem which I had to kludge my way around.
The trouble is that the (*) part of the search text can be ANYTHING
including the whole document!


Something like this

- find first tag
Do while found
- delete selection
- get the range end position of the cursor "a"
- find closing tag
- get the range start "b"
- delete the selection
- select the range between (a+1) abd (b-1)
- reformat
- find first tag
loop

Eddie
 
C

Chris

Hi Edward,

thanks for your fast reply.

Can u give me a code example for this workaround. Im very new in vba macros,
so it would be very helpful....

Thanks in advance.

Chris
 
H

Helmut Weber

Hi Chris,

to me, the basic mistake is to apply a paragraph
style instead of a character style.
Or use direct formatting.

By the way, sure, there are more people here
(that is why I'm here as well)
with excellent knowledge than in the German groups,
according to the number of speakers of english,
some equal to but none better than Christian Freßdorf MVP.

Schönen Tag noch.
(Have a nice day)

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
E

Edward Thrashcort

Hi Edward,

"EDDIE" WILL BE FINE THANKS ;-)

Sub ReformatBetweenTags()
Dim a, b As Long
Dim oRange As Range

Selection.HomeKey Unit:=wdStory

With Selection.Find
.ClearFormatting
.Text = "<U1>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False

While .Execute
Selection.Delete
a = Selection.Range.End
.Text = "</U1>"
If .Execute Then
Selection.Delete
b = Selection.Range.Start
Set oRange = ActiveDocument.Range(a, b)
oRange.Style = ActiveDocument.Styles("Heading 1")
End If
.Text = "<U1>"
Wend
End With

Set oRange = Nothing
End Sub


Eddie
 
K

Klaus Linke

Hi Chris,

A safer wildcard replacement would be .Text = "(\<U1\>)([!^13]@)(\</U1\>)".
Though if your replacement formats the whole document, either there's an end
tag missing, or you started the search in the middle of the document, or you
don't really have paragraphs (... think you replaced them with <br/> tags in
the German groups?).

Just BTW, for the other direction I replace ([!^13]@)(^13) with
<U1>\1</U1>\2
I often use that kind of tagging to clean up docs:
-- Mark up things I want to keep (styles, italic, bold...),
-- then turn the rest into pure text (Ctrl+A, Ctrl+N, Ctrl+Q,
Ctrl+Spacebar),
-- then reapply the styles and formatting.

Klaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top