Aligning text

N

Nowhere

I have found many examples of aligning text to the left or right of a
document using code such as the following

wrdSelection.ParagraphFormat.Alignment := wdAlignParagraphLeft;

But I want to align 2 elements of text on the SAME line one to the left
of the document and the other to the right of the document

Thanks in advance

Mike

--
 
J

Jay Freedman

Word (unlike WordPerfect) doesn't do that with paragraph alignment. Set the
paragraph to left alignment, and insert a right-aligned tab stop at the
right margin. Enter the left element, followed by a tab character, followed
by the right element. Here's a sample of code:

Sub demoLeftRight()
Dim rightMarginLocation As Single
With Selection.Sections(1).PageSetup
rightMarginLocation = .PageWidth - .LeftMargin - .RightMargin
End With

With Selection.Paragraphs(1)
.Alignment = wdAlignParagraphLeft
.TabStops.ClearAll
.TabStops.Add Position:=rightMarginLocation, _
Alignment:=wdAlignTabRight
.Range.Text = "left stuff" & vbTab & "right stuff"
End With
End Sub

Note that if you're trying to do this in a header or footer, the default
styles for those areas (named Header and Footer) already have the tab stop
defined this way, so you don't have to do it in code.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
J

Jay Freedman

Slight amendment: the Header and Footer styles also include a center-aligned
tab stop at the 3-inch location. If you don't need anything centered, you
can either include two vbTab characters in the middle of the string or have
the code remove the center tab stop.
 

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