Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert / Replace tab stops to comma

Tags:

ms-word

vba

I have Word documents which require me to convert or replace tab stops to commas.

The text may look like this:

Hello........world (dot)
Hello------------World (dash)
Hello____World (line)
Hello          world (none)

Word provides 4 choices for tab stops leader: dot, dash, line and none.
I would like to replace only the first 3 leader types into comma.

The last one won't be changed like this:

Hello,world
Hello,World
Hello,World
Hello          world

Find and Replace Tabs function in Word doesn't work. So I tried to "copy and paste" from various sources into a simple macro to do this task:

Sub Macro1()
    For Each para In ActiveDocument.Content.Paragraphs
        For Each aTab In para.TabStops
            If aTab.Leader = wdTabLeaderDots _
              Or aTab.Leader = wdTabLeaderDashes _
              Or aTab.Leader = wdTabLeaderLines Then
                TypeText = ","
                aTab.Clear
            End If
        Next aTab
    Next para
End Sub

Reference: https://msdn.microsoft.com/en-us/library/office/ff192806.aspx

Word says there is an error in the line: TypeText = ",".


Slai has a solution for this question. I copied the second code into a macro, modified a little bit to make it work:

Sub Macro1()
    Dim p As Paragraph, t As TabStop
        
    For Each p In ActiveDocument.Paragraphs
        For Each t In p.TabStops
            If t.Leader Then p.Range.Find.Execute "^t", , , , , , , , , ",", wdReplaceAll
        Next
    Next
End Sub

If you need the macro to work only with paragraphs in your selection, replace ActiveDocument into Selection.

like image 1000
doquan0 Avatar asked Apr 22 '17 16:04

doquan0


People also ask

How do you replace a tab with a comma?

You can use the replaceText processor to replace tabs with commas in a text/plain input file. The Search Value is set to a tab. The Replacement Value is set to a comma.

How do I replace a tab with a comma in Excel?

In the Find and Replace dialog box, under the Replace tab, please: (1) In the Find What box press Ctrl + J keys to enter alt-enter character; (2) In the Replace with box type space or comma as you need; (3) Click the Replace All button.

How do I replace a tab with a comma in notepad?

In the Notepad menu bar click on Edit > Replace.... Right click on the Find what field and choose Paste from the context menu, or press CTRL+V. This inserts the tab character you copied into the 'Find what' box. In the 'Replace with' field, enter a comma and click on Replace All.

How do you replace a tab with a comma in Python?

The most straightforward way to convert a tab-delimited (TSV) to a comma-separated (CSV) file in Python is to replace each tabular character '\t' with a comma ',' character using the string. replace() method. This works if two values are separated by exactly one tabular character.


1 Answers

Find and Replace Format seems to work only if the Tab stop positions are known:

With ThisDocument.Range.Find
    For L = wdTabLeaderDots To wdTabLeaderLines
        .ParagraphFormat.TabStops.Add Position:=36, Leader:=L
        .Execute "^t", , , , , , , , , ",", wdReplaceAll
    Next
End With

Alternative can be to check the tab stops of each paragraph ( BTW WdTabLeader has 6 values ) :

Dim p As Paragraph, t As TabStop

For Each p In ThisDocument.Paragraphs
    For Each t In p.TabStops
        If t.Leader Then p.Range.Find.Execute "^t", , , , , , , , , ",", wdReplaceAll
    Next
Next
like image 73
Slai Avatar answered Oct 12 '22 20:10

Slai