Add tab after finding word

SmallFry
StarLounger
Posts: 91
Joined: 02 Sep 2018, 23:12

Add tab after finding word

Post by SmallFry »

I need to add a right (6.5) tab after a specific word.

The sentence looks like this "Approval Date 12 November 2019"

Right after the word "Approval Date", I would like to add a tab in place of the space and then the code adds a 6.5 right tab stop.

Code: Select all

Sub FixTab()
    With ActiveDocument.Range
        With .Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Text = "Approval Date"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindStop
            .Format = False
            .MatchWildcards = True
            .Execute
        End With
        If .Find.Found = True Then
            .ParagraphFormat.TabStops.Add Position:=InchesToPoints(6.5), Alignment:=WdTabAlignment.wdAlignTabRight
        End If
    End With
End Sub

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Add tab after finding word

Post by HansV »

What exactly do you mean by a "right (6.5) tab"?
- What is the unit? Inches, cm, …?
- 6.5 units from the right margin, or …?
Best wishes,
Hans

SmallFry
StarLounger
Posts: 91
Joined: 02 Sep 2018, 23:12

Re: Add tab after finding word

Post by SmallFry »

My document is in inches.
Width: 8.5"
Height: 11"
1" margins all around

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Add tab after finding word

Post by HansV »

Thanks, but where exactly should the tab position be? At 6.5 inches from the left margin?
Best wishes,
Hans

SmallFry
StarLounger
Posts: 91
Joined: 02 Sep 2018, 23:12

Re: Add tab after finding word

Post by SmallFry »

Hi Hans,

The code already sets the tab and it works.

Code: Select all

.ParagraphFormat.TabStops.Add Position:=InchesToPoints(6.5), Alignment:=WdTabAlignment.wdAlignTabRight
I just need to find the word "Approval Date" and then place a tab right after the word. Currently, there is a space following the word "Approval Date" and then a date.

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Add tab after finding word

Post by HansV »

Like this:

Code: Select all

Sub FixTab()
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
        .ClearFormatting
        .Text = "Approval Date "
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchWildcards = False
        Do While .Execute
            Selection.Text = "Approval Date" & vbTab
            Selection.Collapse Direction:=wdCollapseEnd
            Selection.ParagraphFormat.TabStops.Add Position:=InchesToPoints(6.5), Alignment:=wdAlignTabRight
        Loop
    End With
End Sub
Best wishes,
Hans

SmallFry
StarLounger
Posts: 91
Joined: 02 Sep 2018, 23:12

Re: Add tab after finding word

Post by SmallFry »

Thank you Hans. That works.

Here is what I had come up with which seems to work also, but much longer than your suggestion.

Code: Select all

Sub FixTab()
Selection.HomeKey Unit:=wdStory
Dim myRange As Range
    With ActiveDocument.Range
        With .Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Text = "Approval Date"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindStop
            .Format = False
            .MatchWildcards = True
            .Execute
        End With
        If .Find.Found = True Then
            .ParagraphFormat.TabStops.Add Position:=InchesToPoints(6.5), Alignment:=WdTabAlignment.wdAlignTabRight
            Set myRange = ActiveDocument.Words(2)
            With myRange
                .MoveStart Unit:=wdWord, Count:=1
                .Select
            End With
            Selection.TypeText Text:=vbTab
        End If
    End With
End Sub