Edd was telling us:
Edd nous racontait que :
I continued working on it and ended up with:
Sub Format()
Dim oPara As Object
Here oPara should be a range as you set it as one below.
Dim TabPos As Long
Dim TempVal As String
Dim x As Long
Dim Rstart As Long
Dim Rend As Long
Rstart = ActiveDocument.Range(0,
Selection.Paragraphs(1).Range.End).Paragraphs.count
Rend = ActiveDocument.Range(start:=Selection.start,
End:=Selection.End).Paragraphs.count - 1
Set oPara = ActiveDocument.Range
For x = Rstart To Rstart + Rend
TabPos = InStr(1, oPara.Paragraphs(x), Chr$(9))
TempVal = Right$(oPara.Paragraphs(x), Len(oPara.Paragraphs(x))
- TabPos)
TempVal = "3" & Chr$(9) & TempVal
oPara.Paragraphs(x) = TempVal
Next x
This is supposed to search each paragraph in a selection for a tab
remove everything before it and then replace it with a number 3 and a
tab. This is in preparation for another process. I don't know if
this is the best way to handle it but it works.
End Sub
Wow, it looks very complicated to me!
Here is, in my opinion, a simpler version using the Range object:
Try to use the Range object as much as possible, much sturdier and less
prone to generating errors.
(It looks like a lot because of my comments...)
'_______________________________________
Sub Format()
Dim SelRange As Range
Dim oPara As Paragraph
Dim ParaRge As Range
'save current selection
Set SelRange = Selection.Range
'Iterate thorugh each para in the current selection
For Each oPara In SelRange.Paragraphs
Set ParaRge = oPara.Range
With ParaRge
'check if para as a tab in it
If InStr(1, .Text, vbTab) <> 0 Then
'If so, delete everything before including the tab
.Collapse
.MoveEndUntil vbTab, wdForward
.MoveEnd wdCharacter, 1
.Delete
End If
'Insert the 3 and the tab at the beginning of the para
.InsertBefore "3" & vbTab
'Rest the 3 and tab font attribute in case
'they pick up attributes from the text following
.Font.Reset
End With
Next
'reset the selection
SelRange.Select
End Sub
'_______________________________________
If you want to skip the paragraphs that do not have tabs, the code would be
even simpler.
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site:
http://www.word.mvps.org