Tab stop weirdness

R

Rick Charnes

In my macro I want to clear all tabs, then set two tabs which should be
active for any text that the macro pumps out in the rest of the
document. I use this code from a 'Record Macro':

'set tab stops only at 1.25" and 3.0"
Selection.ParagraphFormat.TabStops.ClearAll
ActiveDocument.DefaultTabStop = InchesToPoints(0.5)
Selection.ParagraphFormat.TabStops.Add Position:=InchesToPoints(1.25), _
Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
Selection.ParagraphFormat.TabStops.Add Position:=InchesToPoints(3), _
Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces

But for some reason after this code is executed these two tabs only hold
for the first paragraph of text that is pumped out afterwards. After
that, the tab setting reverts to one tab every 0.5". What am I doing
wrong? Thanks.
 
R

Rick Charnes

Thanks, I'll take a look at this. In the meantime, can you think of why
my code would not be working? Thanks much.
 
D

Doug Robbins - Word MVP

You would have to reveal how your macro is "pumping out the rest of the
document."

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
R

Rick Charnes

You would have to reveal how your macro is "pumping out the rest of the
document."

Yup, I realized that my text is already below where I am. So I need to:

(1) Select text from current position to end-of-document
(2) Set new tab stops
(3) Turn off selection and return to original position before (1)

I assume I do the step (1) with:
Selection.EndKey Unit:=wdStory, Extend:=wdExtend

But I'm not sure how to turn off the selection and return to my original
position. Any thoughts? Thanks much.
 
E

Ed

You might try setting a Range and doing everything in that.
Dim rngWork As Range
Set rngWork = ActiveDocument.Range(Selection.Range.Start,
ActiveDocument.Content.End)
With rngWork.ParagraphFormat.TabStops
.ClearAll
.Add Position:=InchesToPoints(1.25), _
Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
.Add Position:=InchesToPoints(3), _
Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
End With

With a range, the actual selection (insertion point) never moves. You can
click in the doc, run this macro, and then pick up where you clicked.

HTH
Ed
 
R

Rick Charnes

Right, I had a feeling that using a range was the answer. But when I
paste this in to my VBA editor, it doesn't like the below line which
gets split over two lines with "Start," ending line 1 -- both lines
appear in red. Am I typing something wrong? Thanks.
 
E

Ed

Sorry - should have warned you about the line stuff. Lines of code can
often get pretty long - too long for the NG reader to handle without
breaking. And of course they will always break in bad spots! That all
should be on one line.

Alternatively, you can use a "line break" (I don't know what the proper VBA
name is), which is a space and an underscore before you hit Enter. So your
lines could be
Set rngWork = ActiveDocument.Range(Selection.Range.Start, _
ActiveDocument.Content.End)
or
Set rngWork = ActiveDocument.Range _
(Selection.Range.Start, ActiveDocument.Content.End)
whichever suits your purposes.

HTH
Ed
 

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