Solved

Word (2003): How to automatically space texts on a line?

Posted on 2006-11-29
26
413 Views
Last Modified: 2008-01-09

Hi all,
 
Anybody know of a way of telling Word (2003) to automatically space texts on ONE fully justified line? (I am talking about regular text on a line - not text strings sitting inside text boxes.)
 
For example if I have three texts:
 
"United Federation" "USS Enterprise" "Operation Guide"
 
I would like to make the left string -> left justified (trivial),
the right string -> right justified,
and the center string placed somewhere in the center such that:
 
the spacing between the left string and center string is equal to
the spacing between the center string and right string.
 
I.e., just as in graphics apps you can space objects evenly in the hor. or vert. direction. E.g., in Macromedia Freehand this is called "Distribute Widths" and in Corel Draw this is called "Distribute Spacing" .
 

Anyway to do this, or should I should I just carry on dreaming...?

<-------------------- Thinking while typing -- I had an idea.   ----------------->

Maybe there is a special space character that makes adjacent words "stick" to each other. I.e., that special single space indicates to Word to never increase it to more than one space. That would solve the problem.

- Trying it out
-
-

OK! That did it! Just put non-breaking space between the words that go together, and regular spaces between the terms.
 
Now the problem I have is that Word doesn't justify a paragraph containing just a single line. *** How do I force it to justify a single line, i.e., justify a paragraph containing just a single line? ***

A dirty solution is to add a manual line break after the right-most text - that forces it to justify. But I don't want the extra line. I can see from VBA that there are further Justify commands:
 
wdAlignParagraphDistribute
wdAlignParagraphJustify
wdAlignParagraphJustifyHi
wdAlignParagraphJustifyLow
wdAlignParagraphJustifyMed

 
but they don't seem to help.
 
Any ideas?
 
tia
 
avraham

 


0
Comment
Question by:progtw
  • 14
  • 11
26 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 18040723
Set left, center and right tab stops in the paragraph - use the ruler.
And separate the text with tabs in the line.
0
 

Author Comment

by:progtw
ID: 18041180

To GrahamSkan :

Thanks for the response.

I think I tried that. Maybe I am not understanding you. Where exactly do I put the tab stops? I don't know in advance where the center of the middle string is. That's my whole problem.

- avi
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 18041269
Actually if your paragraph is left justified, then you may not need the left tab.
With the paragraph (line) selected, put the centre tab where you want the middle of the second phrase to be, and the right tab where you want the end of the third phrase to reach.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 18041324
This will do it in VBA

Sub SetStops()
    With ActiveDocument.Paragraphs(1)
        With .TabStops
            .ClearAll
            .Add 200, wdAlignTabCenter
            .Add 400, wdAlignTabRight
        End With
    End With
End Sub
0
 

Author Comment

by:progtw
ID: 18043586
To GrahamSkan :

Thanks for the response.

>> Actually if your paragraph is left justified, then you may not need the left tab.
Sure thing: only two tabs needed. And the second tab goes at the end of the line...
That leaves us with only the middle tab to worry about...That's the whole problem.

>> This will do it in VBA
>> Sub SetStops()
>> <snip>
>>             .Add 200, wdAlignTabCenter
>>             .Add 400, wdAlignTabRight
>> <snip>

Why should I want to set tabs at 200 and 400.

I don't think you are understanding the problem. Here is a response I sent somebody else about the tabs idea:

------------------------------------------------

Thanks for the response.
 
I tried the tab method already. It doesn't work. It doesn't work for the simple reason that you don't know exactly where in the middle of the ruler to put the central tab. You could put the central tab bang in the middle (by me that's at 8.25 cm on a 16.5 cm wide page) but only in a very unusual situation will you get even spacing between the three texts: when the left text and the right text are of identical length. If, say, the left text is a lot longer than the right text, then you will need to put the middle tab mark a lot further to the right to get identical spacing.
 
***** In fact, you could say that the whole definition of this problem is:  "how to automatically calculate where exactly to put the center tab mark". *****
 
Try it yourself with three texts of unequal length to achieve equal spacing.
 
Unless I am not understanding you...
 
Thanks,
------------------------------



0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 18043688
No. It was I who didn't understand. Your example:
"United Federation" "USS Enterprise" "Operation Guide"
looked OK.
as
"United Federation"                      "USS Enterprise"                        "Operation Guide"
but
"United Federation of craftsmen"   "USS Enterprise"                                         "Ops"
is what the centre tab achieves, but isn't what you want.
0
 

Author Comment

by:progtw
ID: 18043791
Yep, you got the problem now. Congrats.

What we need now is a VBA function to calculate the length in cm of a piece of text, given its number of characters, font size, and all the rest of the parameters. Then we can calc where the center tab should go.

The problem then would be that it would not give one click automation given my application for all this. Namely: an autotext, e.g., My_Footer, is declared (in a template) as the line of three text properties placed where their values are drawn from the Doc Custom Properties. Then, in a document based on the template I press F9 on the footer to take the values of the Doc Props from the doc's actual values. I can't think how in one operation I could pass my whole footer (string) to a VBA function, calc where to put the tab, put it there, and then update my doc's footer. On the other hand...

Hey, wait a minute... actually, now I come to think of it, I can't find in Word how to embed a function in a document (using a field). In Excel you can embed a function in a cell, that's the whole idea there. Do YOU know if there is a function field. I can't find one. I am using Word 2003.

avraham
0
 

Author Comment

by:progtw
ID: 18043830
I would have do it in two stages:

Have an embedded DocVariable.
1. Run the macro function. Assign result footer string + tabs to the DocVar.
2. Refresh the DocVar.

Could maybe get the macero function to do the refresh of the DocVar.

avraham
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 18043840
You could do all those steps with one VBA call. Do you know how to code them?
F9 updates the field, i.e. causes the field to do its calculation. It's not clear what you mean by 'embed a function'. A macrobutton field will call a macro. You can also create a keyboard shortcut to start a macro.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 18043852
Cross posted. I posted before getting your comment sent a t 16 minute past the hour.
0
 

Author Comment

by:progtw
ID: 18043865
Main issue is how to calculate the length of a piece of text in cm (or inches or whatever) ...

:-)

A
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 18043871
I am unclear where document variables and/or custom document properties would come in.

You will need to have three portions of text, and to insert and format them according to the curent font, margins, printer driver etc. Would they not already be in the footer? What would need to be saved to a variable?
0
 

Author Comment

by:progtw
ID: 18043884
Again, main issue is how to calculate the length of a piece of text in cm (or inches or whatever) ...

:-)

A
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:progtw
ID: 18043907
>> No. It was I who didn't understand. Your example:
"United Federation" "USS Enterprise" "Operation Guide"
looked OK.

You are right there. My example was not a good one. Your's is better.

A
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 18043938
I'll have a go at that.
0
 

Author Comment

by:progtw
ID: 18043970
>> >> Again, main issue is how to calculate the length of a piece of text in cm (or inches or whatever) ...

>> I'll have a go at that.

I couldn't find a Word API function.
Searched EE and also googled, but couldn't find.

A
0
 

Author Comment

by:progtw
ID: 18043975
>> >> Again, main issue is how to calculate the length of a piece of text in cm (or inches or whatever) ...

>> I'll have a go at that.

if you can do that you deserve the 500 points , and  a few more
:)

A
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 18043995
More than 500 is against EE policy, but thanks for the thought.
0
 

Author Comment

by:progtw
ID: 18044036
really, EE shoudl find a way of sharing some income with the experts who are working so hard - whether it be for enjoyment or not

like, pay half of my monthly subscription out to the answer gievrs in proportion to all teh points I awarded that month.

maybe they should rasie teh price a few $ even just to pay for it

A
0
 

Author Comment

by:progtw
ID: 18044098
According to this:

http://groups.google.com/group/microsoft.public.vb.general.discussion/browse_frm/thread/df43f00eeb7d1ea7/169855ce5bfbd2ae?tvc=1#169855ce5bfbd2ae

the length of a text (selection) was available in teh Information prop in WOrd 97.
can't find it now in WOrd 2003.

A
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 480 total points
ID: 18044203
This macro assumes that you already have the paragraph with the relevant tabs and tabstopes in the primary footer of the first section of your document.

Sub DistributeText()
    Dim para As Paragraph
    Dim rng As Range
    Dim point1 As Single
    Dim point2 As Single
    Dim point3 As Single
    Dim point4 As Single
    Dim gap1 As Single
    Dim gap2 As Single
   
    Set para = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.Paragraphs(1)
    Set rng = para.Range
    If rng.Find.Execute("^t") Then
        point1 = rng.Information(wdHorizontalPositionRelativeToPage)
        rng.Move wdCharacter, 1
        point2 = rng.Information(wdHorizontalPositionRelativeToPage)
    End If
    rng.End = para.Range.End
    If rng.Find.Execute("^t") Then
        point3 = rng.Information(wdHorizontalPositionRelativeToPage)
        rng.Move wdCharacter, 1
        point4 = rng.Information(wdHorizontalPositionRelativeToPage)
    End If
    gap1 = point2 - point1
    gap2 = point4 - point3
   
    para.TabStops(1).Position = para.TabStops(1).Position + (gap2 - gap1) / 2
End Sub
0
 

Author Comment

by:progtw
ID: 18044357
Thanks!

I am busy right now, but I will give it my full attention asap.

>> This macro assumes that you already have the paragraph with the relevant tabs and tabstopes in the primary footer of the first section of your document.


Just checking:

I assume that the tab stops are one left tab stop (to mark the start of the center text) and a right tab stop (to put at the end of the line to make the right end of the 3rd text jump to be flush with the end of the line

and  

I see that you search for two tab chars successively one each at the start of text2 and text3.
?

- avi
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 18044940
I tested with a centre and a right tab stop, though I guess that it would work with a left and a right. As I mentioned before, the left stop is unnecessary, so there would only be need for two tab characters in the line.

0
 
LVL 21

Assisted Solution

by:EricFletcher
EricFletcher earned 20 total points
ID: 18046163
You can do this without tabs if you are willing to use fixed spaces within the different groups of words -- and can live with a Shift-Return at the end of the line.

Consider the examples cited earlier:
"United Federation" "USS Enterprise" "Operation Guide"
and
"United Federation of craftsmen" "USS Enterprise" "Ops"

Use fixed spaces (Shift-Ctrl-Spacebar) within the quoted sets and a single space between them. Set the paragraph justified (flush left & right; Ctrl-J) and end it with Shift-Enter to force it to justify the line. The only ption Word will have will be to expand the spaces between the word sets, and you will end up with the appearance I think you want. Note however, that you will have an extra line due to the line break.
0
 

Author Comment

by:progtw
ID: 18046195
to EricFletcher:
yes, all we need really is for MS to allow you to force justify a single line.

tnx

- avi
0
 

Author Comment

by:progtw
ID: 18063095
To GrahamSkan:

Yep, your routine basically works. I couldn't get it to find my footer using your first line, so I just used this:  

    Set para = Selection.Paragraphs(1)

and manually selected the footer. That's good enough.

So I raised the points to 500.

( I give EricFletcher some points also cos he said something and agreed me about MS.  :-)  )

HOWEVER, somewhere along the way, it started to dawn on me that Word purists are not going to like this (of which of course I am one). By changing the tab positions, we are of course applying local formatting. The other side of teh same coin would be to update the template for each document because of where it needs it tab points. For me, it's even 'worse' because I keep all my header and footer variations defined as autotexts in my template. So I would have to go into my template document update my sample footer and then re-apply it to the template. So I am updating the template for each new document.

I then decided to take a new route. I made my footer into a table of 5 columns, where cols #1, 3, and 5 contain the texts, and cols #2 and 4 contain are the gaps. Then use a method like yours for calculating the gap width. It works apart from one
s-m-a-l-l   problemo...it only works when I do F8 (Step) within the the IDE. If I do F5 or run the macro from Word, it does some strange things. Anyway, I have a friend at work looking at it. If we can't fix it , I will post it as a new question. I have tried using Sleeps and DoEvents. Don't help.

Thanks,

- avi
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Spacing when using MS Word. 18 45
Re-indent word doc 5 33
Printing A1 on A4 Printer 8 58
How to open a ZIP file. 6 25
I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now