Share via

TabStops - macro to adjust alignment?

Anonymous
2013-11-26T03:24:17+00:00

Hi,

I have the following macro/VBA script that allows me to add or remove a 'bar' tab stop to and from selected paragraphs in a Word 2011 document. It adds a bar tab stop at 7cm (or removes it if it already exists). I use this for authoring and editing purposes to temporarily put a line through the middle of a paragraph (or several paragraphs or a whole document depending on my requirements).

The following script works well to add or remove this line (bar tab). However there appears to be a bug where if a paragraph contains tab stops after the position of this 7cm bar tab stop, the tab stop directly following the 7cm tab stop has its alignment changed to 'bar' i.e. the 'bar' alignment setting of the 7cm tab stop I'm deleting gets passed to the tab stop directly following this tab stop.

This appears to be a bug?

Does anyone have any ideas how I could modify the below VBA script to ensure the tab stop following the 7cm bar tab stop retains the correct alignment when I delete the 7cm bar tab stop?

Many thanks.

Pol

Sub BarTab()

Dim p As Word.Paragraph

Dim sngPosition As Single

Dim sngPositionLow As Single

Dim sngPositionHigh As Single

Dim objTabStop As Word.TabStop

Dim objTabStopNext As Word.TabStop

Dim NextTabStopAlign As String

' The value is not exactly as set,

' so look within a range between 7 and 8

sngPosition = CentimetersToPoints(7)

sngPositionLow = Int(sngPosition)

sngPositionHigh = sngPositionLow + 1

For Each p In Selection.Paragraphs

bFound = False

With p.Range.ParagraphFormat

For Each objTabStop In .TabStops

If (objTabStop.Position >= sngPositionLow) _

And (objTabStop.Position <= sngPositionHigh) _

And (objTabStop.Alignment = wdAlignTabBar) Then

bFound = True

objTabStop.Clear

Exit For

End If

Next

If Not bFound Then

.TabStops.Add Position:=sngPosition, _

Alignment:=wdAlignTabBar

End If

End With

Next

End Sub

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2013-11-27T09:58:10+00:00

Hi Pol:

Seriously: create some styles. The macro approach is not going to be stable or reliable, please trust me on this: adding and removing tab stops as direct formatting is likely to lead to document corruption.

If you have a BodyText style, create BodyTextT to be exactly the same but WITH the tab at 7 centimetres. Don't be afraid to change styles in a document, that's what Word expects, and it is built for it.

I don't believe for a moment that the code I suggested is out of your league at all: you are using correctly-typed variables, Arrays are just one step up the ladder from that.

Several of the people posting in here would write it for you if you offered to pay, I would run it up for you myself, except I am right out of time: it's that time of the year.

This is roughly how the store routine would look.

  Dim TabArray()

For Each p In Selection.Paragraphs

    ReDim TabArray(p.TabStops.Count, 1)

    For Each aTab In p.TabStops

        With aTab

            TabArray(aTab, 0) = .Position

            TabArray(aTab, 1) = .Alignment

        End With

    Next ' aTab

Next ' aParagraph

 That declares a "Dynamic" array named TabArray. We do not decide how many rows and columns yet because we don't know how many tabs we have.

 Then in ReDim TabArray(p.TabStops.Count, 1) we count the tab stops in the paragraph and set up as many rows in the array as there are tab stops, and two columns, 0 and 1.

Redimming the array has the side benefit of throwing out the previous data.

 Then it just walks along the paragraph, storing the position and alignment of each tab stop in a row of the array.

Having done that, you can use your existing routines to inspect the content of the array instead of the paragraph, and delete the tab at 7 centimetres if you find it, add it if you don't.

Then clear ALL of the tabs in the paragraph.

Then iterate through the array adding every tab you find in there to the paragraph.

Then do the next paragraph.

I am sorry, I don't know whether that works or not: and I do not have time to look.

Hope this helps

Was this answer helpful?

0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Anonymous
    2013-11-28T02:32:12+00:00

    If it's just a visual cue for yourself you can simply use the Highlight function. A Bookmark would also work. And if you used a macro you could place the bookmark or clear it similarly as you are with your current code.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2013-11-27T22:21:10+00:00

    Thanks for all your feedback John, that's been extremely helpful. I'll have a bit more of a think about it based on what you have said - use styles if I can, refine the script if I need to, or maybe think of a different way altogether to solve the problem that will have less impact from a document corruption perspective (basically I use a line down the middle of a document as an obvious visual cue as to whether I have the master version of a document, or whether I've sent the version to someone else to review and mine is only a copy - so I don't accidentally make changes to the copy rather than master).

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2013-11-27T01:37:44+00:00

    Hi John,

    Thanks very much for your response. Our documents are already fully styled with different paragraph and character styles so applying a style for this function unfortunately won't be an option.

    The code you have suggested is a bit out of my league, would you have suggestions as to where we might find someone who would be able to write the suggested code for us.

    Thanks,

    Pol.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2013-11-26T08:52:17+00:00

    Hi Pol:

    I observe the same bug you do: when  you remove the bar tab at 7 cm, any subsequent tab becomes a bar tab.

    I would not use code for this.  I would use a style.  Define the tab properties into the style.  When you want the tab, apply the style.  When you don't, apply a different style.

    Job's done: fast, stable, reliable.

    To work around the bug, you would need to load all of the tab positions and types in the paragraph into a two-dimension array, remove them all from the paragraph, then re-set the ones you want.  You may find that you need to set them "backwards" from furthest to nearest to the beginning of the paragraph to avoid any "entertainment" from Word's bugs.

    Looking at your code, I am sure you would have no difficulty doing that.  Personally I wouldn't cut that much code when I can do it easier with a style: but your mileage may be different.

    Cheers

    Was this answer helpful?

    0 comments No comments