Setting the length of a line

  • Thread starter Carlos Chalhoub
  • Start date
C

Carlos Chalhoub

Hi there,

I have thousands of lines of error messages that always end with a straight
quote ("). When I make a change to the message, the line stretches or
shrinks and I have to bring the quote sign back to its original position
(Col 208).

I want to be able to run a macro once in a while in the document so that all
the quotes are automatically put back in the right spot. I think I need to
use Len, but I don't know how to go about doing it.

Thanks for any help!

Carlos
 
C

Carlos Chalhoub

Exactly as you've described it.

Cindy Meister -WordMVP- said:
Hi Carlos,

It's not quite clear exactly what you have. You have a Word document, I take
it. And each "error message" constitutes a single paragraph in that document?
And each paragraph must have exactly 208 characters (excluding the paragraph
mark), with the " being the last character?

So, you'd need to lop of excess characters or pad each string with spaces?


Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jan 24 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
C

Cindy Meister -WordMVP-

Hi Carlos,

<<You have a Word document, I take it. And each "error message" constitutes a
single paragraph in that document?
And each paragraph must have exactly 208 characters (excluding the
paragraph mark), with the " being the last character?
So, you'd need to lop of excess characters or pad each string with spaces>>

Well, to start with, you need to loop through all the paragraphs in the
document, testing the Len (length). Then, you cut off all the extra character
or add spaces, as necessary. At the end, you add a double-quote and the
paragraph mark. Something like this:

Sub ExactParaLengths()
Dim para As Word.Paragraph, rng As Word.Range
Dim lNrParas As Long, lLen As Long, i As Long

lNrParas = ActiveDocument.Paragraphs.Count
For i = 1 To lNrParas
Set para = ActiveDocument.Paragraphs(i)
Set rng = para.Range
rng.MoveEnd wdCharacter, -1 'Cut off last char, the para mark
lLen = Len(rng.Text)
If lLen > 280 Then
para.Range.Text = Left(rng.Text, 279) & Chr(34) & vbCr
ElseIf lLen < 280 Then
para.Range.Text = rng.Text & Space(279 - lLen) & Chr(34) & vbCr
End If
Next i
End Sub


Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jan 24 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
C

Carlos Chalhoub

Hi Cindy,

Thank you for your effort. Really appreciated. I only made some corrections
to the position of the characters.

There is one snag in the macro. If the paragraph is shorter than 207
characters, the macro places a quote mark at the right position, but it
keeps the old quote mark too. I end up having 2 of them. I thought that the
MoveEnd method was supposed to chop off the quote mark. Any idea what is
happening? Thanks.

Sub ExactParaLengths()
Dim para As Word.Paragraph, rng As Word.Range
Dim lNrParas As Long, lLen As Long, i As Long

lNrParas = ActiveDocument.Paragraphs.Count
For i = 1 To lNrParas
Set para = ActiveDocument.Paragraphs(i)
Set rng = para.Range
rng.MoveEnd wdCharacter, -1 'Cut off last char, the para mark
lLen = Len(rng.Text)
If lLen > 207 Then
para.Range.Text = Left(rng.Text, 206) & Chr(34) & vbCr
ElseIf lLen < 207 Then
para.Range.Text = rng.Text & Space(206 - lLen) & Chr(34) & vbCr
End If
Next i
End Sub

Best regards
Carlos
 
C

Cindy Meister -WordMVP-

Hi Carlos,
There is one snag in the macro. If the paragraph is shorter than 207
characters, the macro places a quote mark at the right position, but it
keeps the old quote mark too. I end up having 2 of them. I thought that the
MoveEnd method was supposed to chop off the quote mark.
The MoveEnd is chopping off the paragraph mark.

I suggest building in a line that checks whether the last character is
already a quote, and if it is, delete it and add another space. This is how
you check, I leave building it into your code as an exercise :)

If Right(rng.Text, 1) = Chr$(34) Then 'it's a quote

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jan 24 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
C

Carlos Chalhoub

Hi Cindy,

Thanks so much for helping me out.

My code may not be pretty, but it works.

Sub ExactParaLengths()
Dim para As Word.Paragraph, rng As Word.Range
Dim lNrParas As Long, lLen As Long, i As Long

lNrParas = ActiveDocument.Paragraphs.Count
For i = 1 To lNrParas
Set para = ActiveDocument.Paragraphs(i)
Set rng = para.Range
rng.MoveEnd wdCharacter, -1 'Cut off last char, the para mark
If Right(rng.Text, 1) = Chr$(34) Then
rng.MoveEnd wdCharacter, -1
End If
lLen = Len(rng.Text)
If lLen > 208 Then
para.Range.Text = Left(rng.Text, 206) & Chr(34) & vbCr
ElseIf lLen < 208 Then
para.Range.Text = rng.Text & Space(206 - lLen) & Chr(34) & vbCr
End If
Next i
End Sub
 

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