Changing Paragarphs in a Range

E

Edd

I am interested in defining a range of paragraphs in the main story of a
document based upon a selection and then chaging the first few characters in
each paragraph in that range. When I try to update a paragraph using a "for
each" loop the range is redefined apparently due to the change and I canonot
effect a change in more than one selected paragraph.
 
J

Jonathan West

Hi edd

Edd said:
I am interested in defining a range of paragraphs in the main story of a
document based upon a selection and then chaging the first few characters
in
each paragraph in that range. When I try to update a paragraph using a
"for
each" loop the range is redefined apparently due to the change and I
canonot
effect a change in more than one selected paragraph.

You would need code something like this

Dim oPara as Paragraph
For Each oPara in Selection.Paragraphs
oPara.Range.InsertBefore "abcd"
Next oPara
 
E

Edd

Yes I think this would work and is similar to what I was trying to do, but I
was trying to remove a few charachters on each paragraph and then replace the
entire paragraph. This was necessary to replace charachters to a point
defined by a tab. I'll try this variation and see how it goes.
 
E

Edd

I need to remove characters to a tab point and replacement with new
characters. If I try and replace the paragraphs the selection and subsequent
range is lost so this method wil not work for me.
 
J

Jean-Guy Marcil

Edd was telling us:
Edd nous racontait que :
I need to remove characters to a tab point and replacement with new
characters. If I try and replace the paragraphs the selection and
subsequent range is lost so this method wil not work for me.

Why don't you post the code you have so far?

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
E

Edd

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
 
J

Jean-Guy Marcil

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
 
E

Edd

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.
 
J

Jean-Guy Marcil

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!
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
K

Klaus Linke

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top