Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2004

Tip: Looking for answers? Try searching our database.

Changing Paragarphs in a Range

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Edd - 11 Nov 2004 17:38 GMT
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.
Jonathan West - 11 Nov 2004 17:41 GMT
Hi 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
[quoted text clipped - 4 lines]
> 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

Signature

Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup

Edd - 11 Nov 2004 18:35 GMT
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.

> Hi edd
>
[quoted text clipped - 13 lines]
>     oPara.Range.InsertBefore "abcd"
> Next oPara
Edd - 11 Nov 2004 18:39 GMT
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.

> Hi edd
>
[quoted text clipped - 13 lines]
>     oPara.Range.InsertBefore "abcd"
> Next oPara
Jean-Guy Marcil - 11 Nov 2004 18:46 GMT
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?

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Edd - 11 Nov 2004 19:04 GMT
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

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.