align text left and align text right: on the same line

J

Jay Freedman

How to do it in MS Word?

Set a right-aligned tab stop at the right margin. After entering the
left-aligned text, press Tab and then enter the right-aligned text.

The styles named Header and Footer already have the desired tab stop,
as well as one at the center of the line. You can define your own
style with a right-aligned tab and apply it wherever you want.

BTW, this question is off-topic in a VBA newsgroup unless you're
asking how to do it in a macro. It would be more appropriate to post
it in the microsoft.public.word.pagelayout group.
 
T

Thirsty_4_knowledge

Jay -

I am trying to do this with VBA. What I really need is to access ONLY the
middle "section" (I know this is not the correct use of my vocab) in the
header and footer. For example, I'm trying to get this done:

##### (TAB) MyString (TAB) ####

Where the pound signs are something anybody can manually enter, the (TAB) is
the seperating tabstop (making the header/footer have a left alligned section
of text, a center alligned section of text, and a right allighned section of
text). MyString is a variable in the VBA code.

Currently, all I can think of is count the total characters in the header,
then divide it by two (hoping that will land me in the middle) and selecting
the word that's associated with that character location, and changing it.
This really is way too buggy, and I know it's not the proper way to do
things! If you know of a way that I can access only the center section of a
header or footer, I'd greatly appreciate it... thanks!

Dereck
 
J

Jay Freedman

Hi Dereck,

I'll assume you know how to assign a range to the correct header or footer,
dealing with the issues of different first page, different odd and even, and
multiple sections, so this example will just show the first-section main
footer being modified.

There are a couple of ways of dealing with this, but I think it's simplest
to just look for the tabs and adjust the range's ends to exclude the parts
you don't want to modify.

Sub Example()
Dim oRg As Range

Set oRg = ActiveDocument.Sections(1).Footers( _
wdHeaderFooterPrimary).Range

With oRg
If InStr(.Text, vbTab) > 0 Then
' There is at least one tab character, so
' move the range's start to the character
' after that tab.
.MoveStartUntil vbTab, wdForward
.MoveStart wdCharacter, 1

If InStr(.Text, vbTab) > 0 Then
' There is at least one more tab character,
' so move the range's end to the character
' before that tab.
.MoveEndUntil vbTab, wdBackward
.MoveEnd wdCharacter, -1
End If
End If

' Exclude the paragraph mark if range's end is
' at end of footer.
If .Characters.Last = vbCr Then
.MoveEnd wdCharacter, -1
End If

.Text = .Text & " altered"
.Font.Color = wdColorRed
End With

Set oRg = Nothing
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