VBA Needed for Insertion of Hard CRs

A

a8736d53

I need an MS Word 2002 VBA Macro which automatically inserts a hard
carriage return at the end of every line with the following exceptions:

1. Lines which already end in a hard carriage return.

2. Lines which are the last line of a paragraph.

Another requirement is that it automatically stop when it gets to the
end of the text.
 
G

Greg Maxey

This might meet your requirements:

Sub ScratchMacro()
ActiveDocument.Range(0, 0).Select
Do
ActiveDocument.Bookmarks("\Line").Select
If Selection.Range.End + 1 = ActiveDocument.Range.End Then Exit Do
If Selection.Characters.Last <> vbCr Then
Selection.InsertAfter vbCr
Selection.Collapse wdCollapseStart
Selection.MoveDown wdLine, 1
Else
Selection.Collapse wdCollapseStart
Selection.MoveDown wdLine, 1
End If
Loop
End Sub
 
J

Jean-Guy Marcil

(e-mail address removed) was telling us:
(e-mail address removed) nous racontait que :
I need an MS Word 2002 VBA Macro which automatically inserts a hard
carriage return at the end of every line with the following
exceptions:

1. Lines which already end in a hard carriage return.

2. Lines which are the last line of a paragraph.

Another requirement is that it automatically stop when it gets to the
end of the text.

Sorry, I might be missing something here, but what is the difference between
1 and 2?
In Word, hard carriage returns (¶) are used to signify end of paragraphs...

BTW, this must be a first! Usually, people ask how to delete all the extra
¶!

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

James Reid

Greg said:
This might meet your requirements:

Sub ScratchMacro()
ActiveDocument.Range(0, 0).Select
Do
ActiveDocument.Bookmarks("\Line").Select
If Selection.Range.End + 1 = ActiveDocument.Range.End Then Exit Do
If Selection.Characters.Last <> vbCr Then
Selection.InsertAfter vbCr
Selection.Collapse wdCollapseStart
Selection.MoveDown wdLine, 1
Else
Selection.Collapse wdCollapseStart
Selection.MoveDown wdLine, 1
End If
Loop
End Sub

The above macro works very well, but it inserts "end of paragraph
marks" (¶ - the same thing that you get when you hit the "Enter" key),
instead of the required "hard carriage returns". Also, it disregards
the fact that some lines already end in "hard carriage returns".

To see what was going on, I went to "Tools/Options.../View", and under
"Formatting marks" I checked "Paragraph marks", and then clicked "OK".
The desired "hard carriage returns" then displayed as down arrows which
bend to the left, whereas the paragraph marks displayed as ¶.

When the text is saved using Save As... with "Save as type" set to "Web
Page, Filtered (*.htm; *.html)", the hard carriage returns save as
"<br>", but the "end of paragraph marks" save as "</p>...<p...>".
 
J

James Reid

Jean-Guy Marcil said:
(e-mail address removed) was telling us:
(e-mail address removed) nous racontait que :


Sorry, I might be missing something here, but what is the difference between
1 and 2?
In Word, hard carriage returns (¶) are used to signify end of paragraphs...

If you copy/paste a web page containing <br>s into Word, you will see
what I am calling "hard carriage returns". Go to
"Tools/Options.../View", and under "Formatting marks" check "Paragraph
marks", and then click "OK". The desired "hard carriage returns" then
display as down arrows which bend to the left.
 
H

Helmut Weber

Hi James,

try this further development of Greg's code:

Sub ScratchMacro()
ActiveDocument.Range(0, 0).Select
Do
ActiveDocument.Bookmarks("\Line").Select
If selection.Range.End + 1 = ActiveDocument.Range.End Then Exit Do
If selection.Characters.Last <> vbCr And _
selection.Characters.Last <> Chr(11) Then
selection.InsertAfter Chr(11)
selection.Collapse wdCollapseStart
selection.MoveDown wdLine, 1
Else
selection.Collapse wdCollapseStart
selection.MoveDown wdLine, 1
End If
Loop
End Sub


--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Jean-Guy Marcil

James Reid was telling us:
James Reid nous racontait que :
If you copy/paste a web page containing <br>s into Word, you will see
what I am calling "hard carriage returns". Go to
"Tools/Options.../View", and under "Formatting marks" check "Paragraph
marks", and then click "OK". The desired "hard carriage returns" then
display as down arrows which bend to the left.

OK I see the confusion now.
Those down arrows are not hard carriage returns (well, at least, I have
never heard them being referred to as such), they are sometimes called
either "manual line breaks" or "soft carriage return". They are inserted
through the GUI by doing SHIFT-Enter instead of Enter (which inserts a "hard
carriage return"). In VBA you can insert them using "vbVerticalTab"as
opposed to vbCr or vbCrLf.

This is why Greg's code did no do what you wanted because he used hard
carriage returns (¶) as per your original request.
This is also why Helmut's code works... he read your reply to Greg and
understood what you meant by "hard carriage returns".

And you are absolutely correct that you usually get a tons of those when you
copy/paste from HTML or from these newsgroups.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

James Reid

Helmut said:
try this further development of Greg's code:

Sub ScratchMacro()
ActiveDocument.Range(0, 0).Select
Do
ActiveDocument.Bookmarks("\Line").Select
If selection.Range.End + 1 = ActiveDocument.Range.End Then Exit Do
If selection.Characters.Last <> vbCr And _
selection.Characters.Last <> Chr(11) Then
selection.InsertAfter Chr(11)
selection.Collapse wdCollapseStart
selection.MoveDown wdLine, 1
Else
selection.Collapse wdCollapseStart
selection.MoveDown wdLine, 1
End If
Loop
End Sub

It works perfectly! Thanks Helmut and Greg!

After playing around with it a bit, I came up with the following
slightly shorter version:

Sub Macro1()
ActiveDocument.Range(0, 0).Select
Do
ActiveDocument.Bookmarks("\Line").Select
If Selection.Range.End + 1 >= ActiveDocument.Range.End Then Exit Do
temp1 = Selection.Characters.Last
If temp1<>vbCr And temp1<>Chr(11) Then Selection.InsertAfter Chr(11)
Selection.Collapse wdCollapseStart
Selection.MoveDown
Loop
End Sub
 
J

James Reid

Jean-Guy Marcil said:
OK I see the confusion now.
Those down arrows are not hard carriage returns (well, at least, I have
never heard them being referred to as such), they are sometimes called
either "manual line breaks" or "soft carriage return".

"What we have here is a failure to communicate!". Sorry about that.
They are inserted
through the GUI by doing SHIFT-Enter instead of Enter (which inserts a "hard
carriage return").

Nice to know.
In VBA you can insert them using "vbVerticalTab"as
opposed to vbCr or vbCrLf.

OK. so, "vbVerticalTab" could be used instead of "Chr(11)".

Great! Thanks for your help, Jean-Guy.
 
H

Helmut Weber

"What we've got here is failure to communicate."
A fan of Cool Hand Luke. Superb flick!

I see.

Getting to grips with that remarks,
was the most difficult task of the day.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Helmut Weber

Hi,

IMHO, there is a great deal of confusion in all this.
If you want to shift the rest of a paragraph down one line,
you have to use chr(11) whithin a line.
I you want to preserve a line break,
then insert chr(11) at the end of the line,
unless it is the end of a paragraph anwy.

I've used chr(11) in Word a million times without a problem.

Regarding Word, I think the documentation is outright wrong,
whatever "not useful in Microsoft Windows" means.

Useful in Linux or Mac-OS?

I doubt it.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Jean-Guy Marcil

(e-mail address removed) was telling us:
(e-mail address removed) nous racontait que :
I'd never come across "vbVerticalTab" before, so of course I had to
look it up.

Interestingly, Microsoft refers to this character as "not useful in
Microsoft Windows." (see
http://msdn2.microsoft.com/en-us/library/f63200h0.aspx). I would have
used "vbLf" myself (aka Chr(10)). If nothing else, it's less typing!

I cannot remember right now, but there was one case I cam across where only
vbVerticvalTab did the job (otherwise I ended up with the dreaded square
character) . I think it had to do with manual line breaks in Excel cells
being transferred to Word or to a Userform...

But, maybe I had not tried vbLf... I cannot remember, it was about 2 years
ago...

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
H

Helmut Weber

Hi Jean-Guy,

a linefeed in Excel is chr(10) [alt return].
At least a linefeed becomes chr(10),
when saving an Excel file as CSV.

When opening the CSV-file in Word,
the Excel-linefeed displays as ¶.
Yet, Asc(selection.Text) returns 10.

However, Word regards it as paragraph mark,
according to selection.Paragraphs.Count.
And it behaves like a paragraph mark,
that is to say, [End] moves the insertion point
right before the pilcrow in the same line.

Yet, when inserting a chr(10) manually [num 010],
it is not counted as a paragraph mark,
and [End] moves the insertion point after the pilcrow
in the same line!

Do you like more confusion?
see above:
"Replace Web Returns By Proper Returns".


--

Cheers

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

James Reid

Using the macro (See:
http://groups.google.com/group/microsoft.public.word.vba.general/msg/463f97ee8f1466e9?hl=en&)
revealed the need for an additional requirement:

If a line ends in a space, then delete the space before inserting the
Chr(11).

Here's the complete macro:

Sub Macro1()
ActiveDocument.Range(0, 0).Select
Do
ActiveDocument.Bookmarks("\Line").Select
If Selection.Range.End + 1 >= ActiveDocument.Range.End Then Exit Do
tmp = Selection.Characters.Last
If tmp = " " Then Selection.Characters.Last = ""
If tmp <> vbCr And tmp <> Chr(11) Then Selection.InsertAfter Chr(11)
Selection.Collapse wdCollapseStart
Selection.MoveDown
Loop
End Sub
 
J

James Reid

Oops! Opening a web page ending in:

<p style=display:none>&nbsp;</p>
</body>
</html>

....as an WS Word document puts the following macro into an endless
loop:

Sub Macro3()
ActiveDocument.Range(0, 0).Select
Do
ActiveDocument.Bookmarks("\Line").Select
If Selection.Range.End + 1 >= ActiveDocument.Range.End Then Exit Do
tmp = Selection.Characters.Last
If tmp = " " Then Selection.Characters.Last = ""
If tmp <> vbCr And tmp <> Chr(11) Then Selection.InsertAfter Chr(11)
Selection.Collapse wdCollapseStart
Selection.MoveDown
Loop
End Sub

Of course, ending a web page with "<p
style=display:none;>&nbsp;</p></body></html>" doesn't make sense, but I
did come across one with something similar which did put the above
macro into a loop. The offending web page was actually created by MS
Word itself when I used another VBA macro to automatically convert a
large number of RTF files into "filtered" HTML files!

Therefore, to be safe, I recommend the following:

Sub Macro3()
ActiveDocument.Range(0, 0).Select
Do
ActiveDocument.Bookmarks("\Line").Select
If Selection.Range.End + 2 >= ActiveDocument.Range.End Then Exit Do
tmp = Selection.Characters.Last
If tmp = " " Then Selection.Characters.Last = ""
If tmp <> vbCr And tmp <> Chr(11) Then Selection.InsertAfter Chr(11)
Selection.Collapse wdCollapseStart
Selection.MoveDown
Loop
End Sub

Although it's true that the above macro will skip checking the last
line, that's okay because the last line always ends with ¶ (vbCr).
It's also true that a web page ending in:

<p style=display:none>&nbsp;</p>
<p style=display:none>&nbsp;</p>
</body>
</html>

....will still cause an endless loop, but let's hope that such a
ridiculous web page doesn't exist!
 
J

James Reid

Oops! Opening a web page ending in:

<p style=display:none>&amp;nbsp;</p>
</body>
</html>

....as an WS Word document puts the following macro into an endless
loop:

Sub Macro3()
ActiveDocument.Range(0, 0).Select
Do
ActiveDocument.Bookmarks("\Line").Select
If Selection.Range.End + 1 >= ActiveDocument.Range.End Then Exit Do
tmp = Selection.Characters.Last
If tmp = " " Then Selection.Characters.Last = ""
If tmp <> vbCr And tmp <> Chr(11) Then Selection.InsertAfter Chr(11)
Selection.Collapse wdCollapseStart
Selection.MoveDown
Loop
End Sub

Of course, ending a web page with "<p
style=display:none;>&nbsp;</p></body></html>" doesn't make sense, but I
did come across one with something similar which did put the above
macro into a loop. The offending web page was actually created by MS
Word itself when I used another VBA macro to automatically convert a
large number of RTF files into "filtered" HTML files!

Therefore, to be safe, I recommend the following:

Sub Macro3()
ActiveDocument.Range(0, 0).Select
Do
ActiveDocument.Bookmarks("\Line").Select
If Selection.Range.End + 2 >= ActiveDocument.Range.End Then Exit Do
tmp = Selection.Characters.Last
If tmp = " " Then Selection.Characters.Last = ""
If tmp <> vbCr And tmp <> Chr(11) Then Selection.InsertAfter Chr(11)
Selection.Collapse wdCollapseStart
Selection.MoveDown
Loop
End Sub

Although it's true that the above macro will skip checking the last
line, that's okay because the last line always ends with ¶ (vbCr).
It's also true that a web page ending in:

<p style=display:none>&amp;nbsp;</p>
<p style=display:none>&amp;nbsp;</p>
</body>
</html>

....will still cause an endless loop, but let's hope that such a
ridiculous web page doesn't exist!
 

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