ms-wordms-office

How do I place a tab stop exactly at cursor?


Is there a built-in or VBA method to place a tab stop at the exact location of the text cursor?

I've tried digging through documentation, but I can't figure out how to get the text cursor location.

I originally needed this to replace the exact distance of two standard spaces, but I discovered 1/4 Em Spaces are the same size, but don't stretch when aligned to justified.


Solution

  • I stumbled upon the answer right after posting. 😅 Text cursor is called "Selection" in VBA.

    Here is the Procedure Sub, and a Sub to bind it to Ctrl+Shift+Tab. Add this as VBA to your Normal.dotm to use in all your documents. 😊

    Public Sub InsertTabStop()
      Selection.Paragraphs.TabStops.Add (Selection.Information(wdHorizontalPositionRelativeToTextBoundary))
    End Sub
     
    Sub AddKeyBind()
      Dim KeyCode As Long
      'Change the keys listed in "BuildKeyCode" to change the shortcut.
      KeyCode = BuildKeyCode(wdKeyControl, wdKeyShift, wdKeyTab)
     
      CustomizationContext = NormalTemplate
      If FindKey(KeyCode).Command = "" Then
        KeyBindings.Add wdKeyCategoryMacro, "InsertTabStop", KeyCode
      Else
        MsgBox "Error: Key combination is already in use!" & vbNewLine & vbNewLine & "Key binding not set.", vbOKOnly + vbCritical, "Key binding failed"
      End If
    End Sub