PDA

View Full Version : add tag to end of line



nutts4life
06-08-2007, 08:40 AM
Hi there,

I have a wiki engine which needs text to have a {BR} tag at the end of the line. But no tag if it is an empty line:

e.g.
This 1 line
This 2 line

This 3 line
This 4 line

Should be:

This 1 line{BR}
This 2 line{BR}

This 3 line{BR}
This 4 line{BR}

I want to use word and a vba macro to do this.
I am a vba beginner so am struggling making this code.

Bascially i thought of just going down each line, line by line. If text exists on that line, then add {BR} to the end of the line above (except for the first line}

How would i do this? Is this best idea?

Any help is great.

O

Ebrow
06-08-2007, 12:27 PM
Can you explain the example that you have more. I.e what are you reading from - a spreadsheet, a text file or a string variable(s)

What is your output a text file, or excel or a string.

Jay Freedman
06-10-2007, 03:28 PM
Strictly speaking, your original document shouldn't contain any blank lines -- that's a basic no-no in Word, you should use the Space After paragraph formatting in the style to create that space, not a second paragraph mark.

Still, lots of documents do have stuff like that, and if you have to deal with it in a macro, here's how. DO NOT step line by line through any document, as that's the slowest and most mistake-prone method. Instead, you can do everything quickly and easily with Find/Replace. Sometimes you should use wildcards, and sometimes that's overkill. Here's a macro that uses both to deal with your specific example:
Sub demo()
Dim oRg As Range

' change multiple paragraph marks
' to double-ampersands
Set oRg = ActiveDocument.Range
With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "[^13]{2,}"
.Replacement.Text = "&&"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
' insert tags before remaining
' paragraph marks
Set oRg = ActiveDocument.Range
With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^p"
.Replacement.Text = "{BR}^&"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = False
.Execute Replace:=wdReplaceAll
End With
' replace double-ampersands with
' tag and two paragraph marks
Set oRg = ActiveDocument.Range
With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "&&"
.Replacement.Text = "{BR}^p^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = False
.Execute Replace:=wdReplaceAll
End With
End Sub

--
Regards,
Jay

fumei
06-10-2007, 06:04 PM
Here is a possible alternative.Sub PutBR_End()
Dim oPara As Paragraph
Dim r As Range

For Each oPara In ActiveDocument.Paragraphs
If oPara.Range.Text <> Chr(13) Then
Set r = oPara.Range
r.MoveEnd Unit:=wdCharacter, Count:=-1
r.InsertAfter ("{BR}")
End If
Next
End SubJay is of course absolutely right in stating you really should have NO "empty" paragraphs.

Proper use of Styles (with SpaceAfter handling...well the space after) is THE way to go with Word.

However, if those "empty" are indeed "empty" - that is, they are just paragraph marks, then the above code will leave them alone, and put {BR} at the end of all the other paragraphs.

But NOTE!!! Paragraphs. Not, technically speaking, "lines", but paragraphs. This assumes that each of your lines is terminated by a paragraph mark.

fumei
06-10-2007, 06:14 PM
Oh, and you do not mention if you have tables in your document. If you do, some extra care must be taken, and the code adjusted. Each cell in a table is considered a paragraph.

nutts4life
06-11-2007, 01:14 AM
Guys,

This worked a treat. I understand your concerns with the use of double lines. But the concept is that a user just rights a dirty document in word to make it easy to pass to a wiki. I have a feeling users will use double spaces to save time.

It's people like you that keep the world going round.

Thanks again.

O

fumei
06-12-2007, 12:57 PM
users will use double spaces to save time.Sigh, yes they will....and they will be wrong.

It does NOT save time. Proper use of well-designed styles saves time.