AlignParagraphLeft Macro Not Working

R

Raul

I recorded the following macro to select the whole document, align center,
and then align left. The objective is to remove all "leading" tabs (tabs
that preceed anything in that paragraph). The problem is the macro does not
work as intended. It does select the whole story, it does align center, and
then it aligns left but retains all the leftmost tabs.

Sub RemoveLeftmostTabs()
'
Selection.WholeStory
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft

End Sub

When I manually use CNTRL+A to select the whole story, use CNTRL+E to align
center, and CNTRL+L to align left, Word does what I want.

What am I missing?

Thanks in advance,
Raul
 
G

Graham Mayor

As you have found that approach will not work. Sendkeys will do the job
(though I hear there may be some issues with Sendkeys and Vista)

Selection.WholeStory
SendKeys "^e"
SendKeys "^l"

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
R

Raul

SendKeys didn't work (i.e., nothing happened while stepping through the
macro). I am on XP and 2003. Do I need to set some options?

Thanks,
Raul
 
G

Graham Mayor

The posted code works for me in both 2003/XP and 2007/XP?
It doesn't work with the E and L in Upper Case as this turns on tracking
changes (CTRL+SHIFT+e) and Apply ListBullet (CTRL+SHIFT+l)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
R

Raul

The sendkeys didn't work, so I took some code that Greg ("macro for
debolding" 13-Jun-2005) had posted and modified it to meet my needs. I hope
it can help others.

I do appreciate the help Graham; it helped me work through some other VBA
issues as well.

Thanks,
Raul

Sub RemoveLeftTabsInDocument()
Dim oPara As Paragraph

'Example of removing ascii character = 9 for the first line
'in every paragraph of a documnet
' tab character = ascii(9)


For Each oPara In ActiveDocument.Paragraphs
oPara.Range.Select
With Selection
'x = Asc(.Characters(1)) ' so I could see the ascii numbers
'y = .Characters(1).Fields.Count ' so I could see what was going on
While Asc(.Characters(1)) = 9 Or _
.Characters(1).Fields.Count = 1
If .Characters(1).Fields.Count = 1 Then
.Characters(1).Fields(1).Delete
Else
.Characters(1).Delete
End If
Wend
End With
Next

End Sub
 
K

Klaus Linke

Calling the built-in commands directly in some way would have worked too,
say:

Selection.WholeStory
WordBasic.CenterPara
WordBasic.LeftPara

The code is exploiting the fact that these built-in commands really do more
work than just centering/ left-aligning the selection, i.e., they remove
white space from both ends.

Regards,
Klaus
 

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