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 / July 2005

Tip: Looking for answers? Try searching our database.

copying paragraphs containing specific text

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Judi - 12 Jul 2005 15:54 GMT
Hi

I am hoping someone can help me out.

I am trying to write some code that loops through all the text in a word
document and when it finds the text "[R1]" it copies the entire paragraph
where that text is contained to another location.

I’ve amended some code from the MS knowledgebase. I started out simply just
trying to get the text selected and formatted but the code isn’t selecting
the right text.

Sub EditFindLoopExampleA()
    With ActiveDocument.Content.Find
        .ClearFormatting
        .Text = "[R1]"
     
        Do While .Execute(Forward:=True, Format:=True) = True
                With .Parent
                    'If the found text is the last
                    ' paragraph in the document...
                    If .End = ActiveDocument.Content.End Then
                       With Selection
                             'I would expect this code to select the
current paragraph where
                             'the text had been found so i could format or
copy it
                            .StartOf Unit:=wdParagraph, Extend:=wdMove
                            .MoveDown Unit:=wdParagraph, Count:=1,
Extend:=wdExtend
                            .Font.Color = wdColorRed
                       End With
                       Exit Do
                    'If the found text is *not* the last
                    ' paragraph in the document...
                    Else
                       With Selection
                             'I would expect this code to select the
current paragraph where
                             'the text had been found so i could format or
copy it
                            .StartOf Unit:=wdParagraph, Extend:=wdMove
                            .MoveDown Unit:=wdParagraph, Count:=1,
Extend:=wdExtend
                            .Font.Color = wdColorRed
                       End With
                                         
                    End If
                End With
        'Goes back to the beginning of the Do...Loop statement.
        Loop
    End With
End Sub

Has any one got any suggestions?

Thanks

Judi
Dave Lett - 12 Jul 2005 17:54 GMT
Hi Judi,

You didn't specify in your post WHERE the other location is that you want to
copy the current paragraph to. Instead of using the selection object to
select the entire paragraph, you probably want to use ranges so that 1) you
don't have to make sure that you move to the end of the currently selected
R1 paragraph and 2) your routine will run faster. Once I know where you want
to copy the existing material, then I can show you a sample.

HTH,
Dave

> Hi
>
[quoted text clipped - 56 lines]
>
> Judi
Judi - 12 Jul 2005 23:06 GMT
Hi Dave

Thanks for the rapid response.  I want to create a summary of the tagged
data and I intend to copy it to a bookmarked location at the beginning of the
document and every time i append a paragraph i will change the bookmark so
everything gets appended to the end.  I hope thats enough information.  

Thanks

Judi

> Hi Judi,
>
[quoted text clipped - 68 lines]
> >
> > Judi
Dave Lett - 13 Jul 2005 15:08 GMT
Hi Judi,

I've experimented in my environment (Word 2003 and NT) and it seems to work.
With this method, there was really no need to test if the found item was in
the last paragraph or not. Hope this helps.

Dim oRngToCopy As Range
Dim oRngBookmark As Range

Set oRngBookmark = ActiveDocument.Bookmarks("CopyTo").Range
oRngBookmark.Start = oRngBookmark.End

With Selection
   .HomeKey Unit:=wdStory
   With .Find
       .Text = "[R1]"
       .Forward = True
       Do While .Execute
           Set oRngToCopy = Selection.Paragraphs(1).Range
           oRngBookmark.FormattedText = oRngToCopy.FormattedText
           oRngBookmark.Start = oRngBookmark.End
       Loop
   End With
End With

HTH,
Dave

> Hi Dave
>
[quoted text clipped - 89 lines]
>> >
>> > Judi
Judi - 14 Jul 2005 17:19 GMT
Hi Dave

Thank you , that's great exactly what i wanted to do.

Best,

Judith

> Hi Judi,
>
[quoted text clipped - 117 lines]
> >> >
> >> > Judi
 
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.