Results 1 to 9 of 9
  1. #1
    Star Lounger
    Join Date
    Dec 2001
    Location
    Texas
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Tab Stops in VBA (Word 97 SR-2)

    How do I use VBA to return the position of the currently selected tab? Or for that matter any special character if I need to know it's position from the left-hand margin?

    I'm thinking something along these lines

    Variable=PointsToInches(Selection.Paragraphs.TabSt op(CURRENT_TAB).Position)

    Thanks,

  2. #2
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Tab Stops in VBA (Word 97 SR-2)

    <pre>MsgBox PointsToInches(Selection.Range.ParagraphFormat.Tab Stops(1).Position)
    </pre>


    If you want to assign it to a variable, it returns a Single.

    HTH!

  3. #3
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Tab Stops in VBA (Word 97 SR-2)

    And for the position in general, use the Information property:

    <pre>MsgBox PointsToInches(Selection.Information(wdHorizontalP ositionRelativeToPage))
    </pre>


  4. #4
    Star Lounger
    Join Date
    Dec 2001
    Location
    Texas
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Tab Stops in VBA (Word 97 SR-2)

    Thanks for your speedy response!

    However, while the tab stops may be indexed, I need to know the position of the CURRENTLY selected tab, not the tab in the first position. I want to use this in conjunction with a find/execute method or MoveUntil method.

    Thanks in advance for any assistance. And as always, you guys are awesome.

    Best regards,

  5. #5
    Star Lounger
    Join Date
    Dec 2001
    Location
    Texas
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Tab Stops in VBA (Word 97 SR-2)

    Andrew,

    Your second post gave me hope! However, I noticed that position returned is not the same as the tab position. For instance, it looks like using:

    MsgBox PointsToInches(Selection.Information(wdHorizontalP ositionRelativeToPage))

    Finds the beginning of the selection, whereas I will need to know where the tab is set.

    Am I wearing out my welcome?LOL

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 16 Times in 16 Posts

    Re: Tab Stops in VBA (Word 97 SR-2)

    What IS the current tab position, actually? Say you have the following situation:
    <pre>----T----T----T----
    a bc def</pre>

    The tab between 'bc' and 'def' is selected. Do you want to return the position of the first tab, or of the second?

  7. #7
    Star Lounger
    Join Date
    Dec 2001
    Location
    Texas
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Tab Stops in VBA (Word 97 SR-2)

    Hi Hans,

    Good to hear from you. Eventually I would need to know both. You can get that information from Format Menu/Tabs. Let me explain what I'm trying to do.

    We have a Word document that looks like it is in tab-delimited format, but not quite. This file was arranged using tabs, but it didn't include a tab where a null value occurred, it just moved the tab over to the next column! Hence, if you try to copy/paste and convert text to table and so on, the information doesn't align properly. Where a null value occurs, the data shifts to the left.

    The only solution I have right now is to create a table and place the information in the corresponding columns. I would do this by first building an array that has all the tab stop positions from the very first row, since no null values exist. Then loop through the data and copy information between the tabs and paste in the appropriate cells. I would have the macro compare the current tab stop position to the position that I'm expecting in the array, which would be incremented by the loop. If the current tab position is greater than the tab position in the array, I know I have a null value. I would then move over one cell within the table and increment the array by one. At least, it sounds plausible.

    So to take your example, let's pretend that I'm showing marks on the page, an left tab on the ruler is the first row "L", a "T" represents a tab, and a "-" represents a space the tab is extending. (There are no spaces) . And the words in parentheses is the information to be copied. The information would like this:

    L L L L
    -T(store)--T(Name)--T(IG)--T(AW)
    -T(18054)-T(Bob)-------------T(Y)

    What you would get is this:

    Store Name IG AW
    18054 Bob Y

    Which is incorrect!

    I've included a sample. If you can figure out a different solution or how to find the actual tab position of the currently selected tab, that would be fantastic!

    Thanks,
    Attached Files Attached Files

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 16 Times in 16 Posts

    Re: Tab Stops in VBA (Word 97 SR-2)

    Perhaps these functions may help:

    Function GetNextTabPosition() As Single
    Dim i As Integer
    Dim p As Single
    p = Selection.Information(wdHorizontalPositionRelative ToTextBoundary)
    For i = 1 To Selection.Paragraphs(1).TabStops.Count
    If Selection.Paragraphs(1).TabStops(i).Position >= p Then
    Exit For
    End If
    Next i
    If i > 0 Then
    GetNextTabPosition = PointToInches(Selection.Paragraphs(1).TabStops(i). Position)
    Else
    GetNextTabPosition = 0
    End If
    End Function

    Function GetPrevTabPosition() As Single
    Dim i As Integer
    Dim p As Single
    p = Selection.Information(wdHorizontalPositionRelative ToTextBoundary)
    For i = 1 To Selection.Paragraphs(1).TabStops.Count
    If Selection.Paragraphs(1).TabStops(i).Position >= p Then
    Exit For
    End If
    Next i
    If i > 1 Then
    GetPrevTabPosition = PointsToInches(Selection.Paragraphs(1).TabStops(i - 1).Position)
    Else
    GetPrevTabPosition = 0
    End If
    End Function

  9. #9
    Star Lounger
    Join Date
    Dec 2001
    Location
    Texas
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Tab Stops in VBA (Word 97 SR-2)

    Thanks, Hans. I'll take look.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •