Reputation: 113
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.
Upvotes: 6
Views: 2445
Reputation: 14373
TypeText
is a method of the Selection
object. Since you don't specify the object, TypeText
fails in your code. Since you also don't use the Selection
object you can't use TypeText
at all. I suggest a structure like this one.
With Para.Range
.Collapse wdCollapseEnd
.Move wdCharacter, -1
.Text = ","
End With
This code would add a comma at the end of the paragraph, which brings you to the question where you want the comma to be. You want it in the place of the tab which is quite a different animal from the TabStop
. A tab is a Chr(9) which you can replace easily enough. However, how this Chr(9) will be interpreted depends upon the type of TabStop
set for it which would depend upon where on the page the Chr(9) takes effect. You can't tell that by examining the text. Therefore you can't differentiate between types of TabStops
by looking at the document's text.
To be clear: It is not possible to replace a TabStop
with a comma because a TabStop
doesn't exist. It is possible to replace tabs with commas.
It is not possible to determine the leader of a particular tab without knowing its precise location in the text. Example: If a TabStop
exists at 100pts from the left margin and a tab becomes effective in the space ending 100pts from the left margin, let's say 50pts, and there is no other TabStop
set in the region between 50pts and 100pts from the left margin, then the tab in the document can be associated with the TabStop
set at 100pts, its leader determined, and a replacement of the tab (not the TabStop
!!!) enacted or omitted.
With knowledge of the precise location of the tab in the document it isn't possible to create an association between it and any of the tab stops in the paragraph. Since it isn't possible to determine which TabStop
might become effective it is also impossible to make an educated guess as to any of its properties. Without precise knowledge of the tab's position in the document the only decision that can be made with its regard is whether to execute it or replace it - unconditionally.
Upvotes: 1
Reputation: 22866
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
Upvotes: 3