My Flush Right Macro Broken

D

DWTSG

Word XP
This macro flushes right selected text
Sub FlushRight()

If Selection.Range.Text = "" Then

MsgBox ("You must select the text you want to flush right")

Else

'Selection.ParagraphFormat.TabStops.Add Position:=InchesToPoints(6), _
' Alignment:=wdAlignTabRight, Leader:=wdTabLeaderSpaces
Dim iTabStop As Integer
With ActiveDocument.PageSetup
iTabStop = .PageWidth - .LeftMargin - .RightMargin
End With

Selection.ParagraphFormat.TabStops.Add Position:=iTabStop, _
Alignment:=wdAlignTabRight, Leader:=wdTabLeaderSpaces

Selection.Copy

Selection.TypeText Text:=vbTab

Selection.Paste

Selection.TypeParagraph

End If

End Sub

all of a sudden it does not work in headers or footers, it functions
flawlessly every where else. Could you please take a look at the code and
see if you notice anything strange. The error I get is at the
selection.copy portion, I get an error telling me that this method is not
available because no text is selected, I can assure you that I do indeed
have text selected. If I run the macro in a header and footer without
selecting any text I get my msgbox warning. If I select the text I get the
vba error. Thanks in advance for the help.

Jim
 
J

JGM

Hi there DWTSG,

There are many problems with your macro:

What if you select a few words in the middle of a paragraph?
It will totally destroy the paragraph formating to have only the middle part
aligned right, and not the words preceding the selection, no? Try it...

What if the selected text is more than one paragraph?
Your macro will replace anything over two paragraphs with one tab stop, and
when you paste the selection back in, only the first pargraph is flushed
right.

What if the selected text already has tab stops in it?
When you paste the text back in, it will align according to the first stop
that was in the existing text prior to replacing it with a single tab stop.
This is because
Selection.TypeText Text:=vbTab
will move the cursor to the first tab stop on the line, not necessarily to
the stop you added with
MyRange.ParagraphFormat.TabStops.Add Position:=iTabStop, _
Alignment:=wdAlignTabRight, Leader:=wdTabLeaderSpaces

You cannot use
..PageWidth - .LeftMargin - .RightMargin
while text is selected in a header or footer, in such cases, the text
becomes unselected, generating your problem. The PageSetUp object and its
properties (such as LeftMargin) can be used only in the document main
window, not in the header or footer. If you have selected text in a header
when calling these properties, the text becomes unselected.

Why use
Selection.TypeParagraph
at the end?
It is dangerous to add a paragraph to a document when you intend only to
format it. A new paragraph may generate an unwanted new page, it will
increase the top or bottom margins of a header or footer, etc.

Finally, why all the code when all you want to do is flush right? Why not
use the Formatting toolbar button "Align right"?
If you really need code for that, then why not use:
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
With this code you do not have to worry about text being selected or not. If
nothing is selected, only the current paragraph will be aligned right.

But, if you insist on using your code, try this modified version:
(It will work even if text is selected in the header or footer.
_________________________________________________________
Sub FlushRight()

If Selection.Range.Text = "" Then

MsgBox ("You must select the text you want to flush right")

Else

Dim iTabStop As Integer
Dim MyRange As Range

Set MyRange = Selection.Range
With ActiveDocument.PageSetup
iTabStop = .PageWidth - .LeftMargin - .RightMargin
End With

MyRange.Select

MyRange.ParagraphFormat.TabStops.Add Position:=iTabStop, _
Alignment:=wdAlignTabRight, Leader:=wdTabLeaderSpaces

MyRange.Copy
Selection.TypeText Text:=vbTab
Selection.Paste

End If

End Sub

__________________________________________________________

Good luck!
Cheers!
 
D

DWTSG

Best explanation ever. Thanks tons.
JGM said:
Hi there DWTSG,

There are many problems with your macro:

What if you select a few words in the middle of a paragraph?
It will totally destroy the paragraph formating to have only the middle part
aligned right, and not the words preceding the selection, no? Try it...

What if the selected text is more than one paragraph?
Your macro will replace anything over two paragraphs with one tab stop, and
when you paste the selection back in, only the first pargraph is flushed
right.

What if the selected text already has tab stops in it?
When you paste the text back in, it will align according to the first stop
that was in the existing text prior to replacing it with a single tab stop.
This is because
Selection.TypeText Text:=vbTab
will move the cursor to the first tab stop on the line, not necessarily to
the stop you added with
MyRange.ParagraphFormat.TabStops.Add Position:=iTabStop, _
Alignment:=wdAlignTabRight, Leader:=wdTabLeaderSpaces

You cannot use
.PageWidth - .LeftMargin - .RightMargin
while text is selected in a header or footer, in such cases, the text
becomes unselected, generating your problem. The PageSetUp object and its
properties (such as LeftMargin) can be used only in the document main
window, not in the header or footer. If you have selected text in a header
when calling these properties, the text becomes unselected.

Why use
Selection.TypeParagraph
at the end?
It is dangerous to add a paragraph to a document when you intend only to
format it. A new paragraph may generate an unwanted new page, it will
increase the top or bottom margins of a header or footer, etc.

Finally, why all the code when all you want to do is flush right? Why not
use the Formatting toolbar button "Align right"?
If you really need code for that, then why not use:
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
With this code you do not have to worry about text being selected or not. If
nothing is selected, only the current paragraph will be aligned right.

But, if you insist on using your code, try this modified version:
(It will work even if text is selected in the header or footer.
_________________________________________________________
Sub FlushRight()

If Selection.Range.Text = "" Then

MsgBox ("You must select the text you want to flush right")

Else

Dim iTabStop As Integer
Dim MyRange As Range

Set MyRange = Selection.Range
With ActiveDocument.PageSetup
iTabStop = .PageWidth - .LeftMargin - .RightMargin
End With

MyRange.Select

MyRange.ParagraphFormat.TabStops.Add Position:=iTabStop, _
Alignment:=wdAlignTabRight, Leader:=wdTabLeaderSpaces

MyRange.Copy
Selection.TypeText Text:=vbTab
Selection.Paste

End If

End Sub

__________________________________________________________

Good luck!
Cheers!
--
_________________________________________

Jean-Guy Marcil
(e-mail address removed)
 
D

DWTSG

This macro is only intended to be used when the user wants multiple
alignments on a single line. So if the name was left aligned the date would
be right aligned.
 
J

JGM

Haaaaa!

So make sure to tell the user to select exactly from the the beginning of
the date (as in your example) to the last character on the line just before
the paragraph mark, but not the paragraph mark itself. Also, the user has to
make sure that there are no tab stop from the starting point of the
selection to the end of the line.

Otherwise there will be trouble!

Cheers!
 

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