Macro to Replace alle line breaks with <br>

C

Chris

Hi,

i hope someone can help me.

I need a macro to find all line breaks in my document and replace it with
html-tag <br> and back (from <br> back to line break. Also i want to find
every word which font size is bigger than 12 and enclose it with html-tag
<font size ='font size'>text</font>.

Some idea how to solve this?

Many thanks in advice

Chris
 
H

Helmut Weber

Hi Chris,

from your posting in the German groups
I see, that you're not aware of the difference
between a linebreak and a paragraph mark.
Not to speak of automatic linebreaks.
There are differences even between paragraph
marks and chr(13), but I don't think that matters here.

If you got a paragraph formatted, lets say, as "Heading 3",
preceded by a paragraph formatted as "normal",
then, when you remove the paragraph mark from
the "normal" paragraph the following paragraph
takes over the style of the previous paragraph.

See my signature for the Word-version i'm talking about.

It may happen, if you remove all paragraph marks
from your doc, except the last one, which can't
be removed, starting with paragraph 1,
that the above mentioned principle
spills over to all paragraphs successively,
unitl there is only one paragraph left,
formatted in the style of the first paragraph.

I darkly remember, this might have been the other
way round in earlier versions of Word,
means, the paragraph, the paragraph mark of
which was deleted, took over the formatting
of the next paragraph.

For the second question, I don't think
you can search for font-size > 12,
only for a well defined size.

You may check however,
beware, pseudocode

for each word in range.words
if font-size > 12 then

slow, admittedly, but for large documents
there are ways to speed it up.

I'll wait with a further explanation,
until there is a problem.

Cheers

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Chris

Hi Helmut,

thanks for your explanation.

As you see i'm still a "greenhorn" in vba word programming.
What is the difference between word paragraph mark an line break (chr(13))?
So i search for paragraph marks "^p" and replace it with my tag <br>, right?
So the problem that my whole document will be formated as the last
headerstyle i
assigned is that i remove all paragraph marks first??
It may happen, if you remove all paragraph marks
from your doc, except the last one, which can't
be removed, starting with paragraph 1,
that the above mentioned principle
spills over to all paragraphs successively,
unitl there is only one paragraph left,
formatted in the style of the first paragraph.

Ok, but how can this be solved?

Greetings Chris
 
H

Helmut Weber

Hi Chris,
What is the difference between word paragraph mark
and line break (chr(13))?

A line break is chr(11) and looks like the arrow
on an enter key. You type it with [shift return].
A paragraph mark looks like Chr(182) ¶.
What you see here, though, are not paragraph marks,
they characters only look that way.

Sample Text:

This is paragraph one.¶
This is paragraph two.¶

If you got two paragraphs and replace
the first paragraph mark by anything
but a paragaph mark, the two paragraphs will become one.
So i search for paragraph marks "^p" and replace it with my tag <br>, right?
So the problem that my whole document will be formated as the last
headerstyle i
assigned is that i remove all paragraph marks first??

Yes.

Use a character style instead of a paragraph style.
Or apply formatting directly, like this:

Sub Macro11()
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = True
.MatchWildcards = True
.Text = "(\<U1\>)(*)(\</U1\>)"
.Replacement.Text = "\2"
.Replacement.Font.Color = wdColorRed
.Execute Replace:=wdReplaceAll
End With
End Sub

Still better:

Sub Macro11a()
Dim rTmp As Range
Set rTmp = ActiveDocument.Range
With rTmp.Find
.Text = "(\<U1\>)(*)(\</U1\>)"
.MatchWildcards = True
.Replacement.Text = "\2"
.Replacement.Font.Color = wdColorBlue
.Execute Replace:=wdReplaceAll
End With
End Sub

As when creating a new range-object to search
you don't have to worry about clearformatting etc.

Keep on.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Chris

Hi Helmut,

thank you! I will try...

Greetings Chris
Helmut Weber said:
Hi Chris,
What is the difference between word paragraph mark
and line break (chr(13))?

A line break is chr(11) and looks like the arrow
on an enter key. You type it with [shift return].
A paragraph mark looks like Chr(182) ¶.
What you see here, though, are not paragraph marks,
they characters only look that way.

Sample Text:

This is paragraph one.¶
This is paragraph two.¶

If you got two paragraphs and replace
the first paragraph mark by anything
but a paragaph mark, the two paragraphs will become one.
So i search for paragraph marks "^p" and replace it with my tag <br>, right?
So the problem that my whole document will be formated as the last
headerstyle i
assigned is that i remove all paragraph marks first??

Yes.

Use a character style instead of a paragraph style.
Or apply formatting directly, like this:

Sub Macro11()
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = True
.MatchWildcards = True
.Text = "(\<U1\>)(*)(\</U1\>)"
.Replacement.Text = "\2"
.Replacement.Font.Color = wdColorRed
.Execute Replace:=wdReplaceAll
End With
End Sub

Still better:

Sub Macro11a()
Dim rTmp As Range
Set rTmp = ActiveDocument.Range
With rTmp.Find
.Text = "(\<U1\>)(*)(\</U1\>)"
.MatchWildcards = True
.Replacement.Text = "\2"
.Replacement.Font.Color = wdColorBlue
.Execute Replace:=wdReplaceAll
End With
End Sub

As when creating a new range-object to search
you don't have to worry about clearformatting etc.

Keep on.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

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