View Single Post
 
Old 10-29-2019, 11:22 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,138
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The original code was lifted from another process that puts the lines at the start and end of the paragraph for emphasising headings. As Andrew suggests the column width will do the job without the need to calculate the tab position, and can be adapted for use in tables. I would however suggest adding a trap for empty paragraphs, and of course the positioning variables are superfluous thus
Code:
Sub TabToEndOfLine()
Dim oRng As Range, lngColWidth As Long
Dim oPara As Paragraph
    On Error Resume Next
    For Each oPara In ActiveDocument.Paragraphs
        Set oRng = oPara.Range
        If oPara.Range.Information(wdWithInTable) = False Then
            lngColWidth = oRng.Sections(1).PageSetup.TextColumns.Width
        Else    ' in a table
            lngColWidth = oRng.Cells(1).Width
        End If
        If Len(oRng) > 1 Then
            With oRng.ParagraphFormat.TabStops
                .ClearAll
                .Add Position:=lngColWidth, Alignment:=wdAlignTabRight, Leader:=2
            End With
            oRng.End = oRng.End - 1
            oRng.InsertAfter Chr(32) & vbTab
            DoEvents
        End If
    Next oPara
lbl_Exit:
    Set oRng = Nothing
    Set oPara = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote