
Signature
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org
I continued working on it and ended up with:
Sub Format()
Dim oPara As Object
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
> Edd was telling us:
> Edd nous racontait que :
[quoted text clipped - 4 lines]
>
> Why don't you post the code you have so far?
Jean-Guy Marcil - 12 Nov 2004 05:59 GMT
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
[quoted text clipped - 23 lines]
>
> 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.

Signature
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org
Edd - 16 Nov 2004 00:47 GMT
Thanks for the help. Much more elegant. I am having a hard time learning to
work with ranges. I understand their benifit but they seem poorly documented
for the learn as you go programmer.
Thanks again.
> Edd was telling us:
> Edd nous racontait que :
[quoted text clipped - 79 lines]
> If you want to skip the paragraphs that do not have tabs, the code would be
> even simpler.
Jean-Guy Marcil - 16 Nov 2004 01:01 GMT
Edd was telling us:
Edd nous racontait que :
> Thanks for the help. Much more elegant. I am having a hard time
> learning to work with ranges. I understand their benifit but they
> seem poorly documented for the learn as you go programmer.
Don't give up! Once you get the concept of ranges (A stretch of text
delimited by a starting point and an ending point stashed in memory, sort
of...) the examples will start to make sense. Also, without the Range
object, you will always end up with highly complicated/unstable code,
especially when dealing with header/footers.
Keep at, you won't regret it.
One day, you will find some code you wrote before understanding the Range
object and you will want to fire yourself!
I have been there!

Signature
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org
Klaus Linke - 30 Nov 2004 18:09 GMT
> 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.
Hi Edd,
Another way to achieve that might be a wildcard replacement:
Edit > Replace, check "Match wildcards",
Find what: [!^13]@^t([!^13]@^13)
Replace with: 3^t\1
If you are sure that there's never more than one tab in a paragraph, this
would suffice:
Find what: [!^13]@^t
Replace with: 3^t
[!^13]@ matches any characters except paragraph marks (^13).
So [!^13]@^t matches any text up to and including the tab ^t.
If the paragraph doesn't contain a tab, the match fails, and the search
continues at the start of the next paragraph.
Regards,
Klaus