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 / February 2008

Tip: Looking for answers? Try searching our database.

For Each...Next is not advancing to next paragraph

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
toth.usenet@gmail.com - 04 Feb 2008 16:59 GMT
hi,

i'm trying to write a program which will select several paragraphs,
based on a key word, and then turn them into a table. most of it seems
to work but i can't get the For Each...Next to advance to the next
paragraph and i'm not sure why.

wherever the text "Step name :" is found in my document i would like
to select the current (n=1)+ the following 6 (n=7) paragraphs and turn
them into a table. then i would like to continue with the next
paragraph (n=8) and repeat the same procedure (search, select,
convert).

here is my code:

Dim SearchPara As Paragraph
Dim SelPara As Paragraph
Dim SelRange As Range

For Each SearchPara In ActiveDocument.Paragraphs
   Set SelRange = SearchPara.Range
   With SelRange.Find
       .Text = "Step name :"
       .Forward = False
       If .Execute = True Then
           If SelRange.Start = SearchPara.Range.Start Then
               SelRange.MoveEnd Unit:=wdParagraph, Count:=7
               SelRange.Select
               Selection.ConvertToTable .......[snip]

           End If
       End If
   End With
Next

so far this is working well and i can find the text, i can select the
next paragraphs and i can turn them into a table. however, i can not
get the program to advance to the next paragraph after the table, the
For Each...Next loop keeps going on the same selected paragraphs. i
assume it is related to the selection covering several paragraphs.

does anyone have an idea why this is happening, and how i could
resolve it?

any help is appreciated.

cheers,
thomas
Doug Robbins - Word MVP - 05 Feb 2008 01:56 GMT
You need to do it as follows:

   Dim myrange As Range
   Selection.HomeKey wdStory
   Selection.Find.ClearFormatting
   With Selection.Find
       Do While .Execute(findText:="Step name :", Forward:=True, _
           MatchWildcards:=False, MatchCase:=True, Wrap:=wdFindStop) = True
           Set myrange = Selection.Paragraphs(1).Range
           myrange.MoveEnd wdParagraph, 6
           myrange.ConvertToTable [Additional parameters required]
           myrange.Collapse wdCollapseEnd
       Loop
   End With

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> hi,
>
[quoted text clipped - 44 lines]
> cheers,
> thomas
toth.usenet@gmail.com - 05 Feb 2008 08:46 GMT
hi doug,

thanks for your answer.

i'm not sure whether this is only a part of or the full solution, but
in itself it is not working. maybe this is because not every line has
the "Step name :" in it. it seems to me that the program is trying to
operate on a selection which doesn't exist yet.

any ideas what i'm doing wrong?

cheers,
thomas

On Feb 5, 2:56 am, "Doug Robbins - Word MVP" <d...@REMOVECAPSmvps.org>
wrote:
> You need to do it as follows:
>
[quoted text clipped - 71 lines]
> > cheers,
> > thomas
toth.usenet@gmail.com - 05 Feb 2008 09:19 GMT
hi doug,

i got it to work now and it properly converts the paragraphs into a
table. however, it will then go through the same paragraph (Step name)
3 times, creating two more tables inside the table before moving to
the next "Step name" item.

any ideas why that is?

sorry for causing the mixup. i didn't pay attention to the case of the
search string. while my version of find seems to be case insensitive,
yours only works with the correct case.

cheers,
thomas

On Feb 5, 9:46 am, toth.use...@gmail.com wrote:
> hi doug,
>
[quoted text clipped - 88 lines]
> > > cheers,
> > > thomas
Doug Robbins - Word MVP - 05 Feb 2008 10:15 GMT
Show us the full code that you are now using.  It should not do what you
says it does because after the range is expanded to include the next 6
paragraphs and the range is converted into a table, the range is collapsed
by the statement

myrange.Collapse wdCollapseEnd

and the code is then looking for the next instance of  "Step name :" in the
balance of the document.
-
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> hi,
>
[quoted text clipped - 44 lines]
> cheers,
> thomas
toth.usenet@gmail.com - 05 Feb 2008 10:37 GMT
hi doug,

here is the full code i'm using

Sub TestingTable()

Dim myrange As Range
   Selection.HomeKey wdStory
   Selection.Find.ClearFormatting
   With Selection.Find
       Do While .Execute(findText:="Step Name :", Forward:=True, _
               MatchWildcards:=False, MatchCase:=True,
Wrap:=wdFindStop) = True
           Set myrange = Selection.Paragraphs(1).Range
           myrange.MoveEnd wdParagraph, 6
           myrange.ConvertToTable Separator:=wdSeparateByParagraphs,
NumColumns:=7, _
               NumRows:=1,
InitialColumnWidth:=CentimetersToPoints(2), Format:= _
               wdTableFormatNone, ApplyBorders:=True,
ApplyShading:=True, ApplyFont:= _
               True, ApplyColor:=True, ApplyHeadingRows:=True,
ApplyLastRow:=False, _
               ApplyFirstColumn:=True, ApplyLastColumn:=False,
AutoFit:=True, _
               AutoFitBehavior:=wdAutoFitFixed
           myrange.Collapse wdCollapseEnd
       Loop
   End With

End Sub

i've tried to debug the code a bit and noticed the following. the
range is correctly collapsed to the end of the range but executing the
find will actually find the same instance of "Step Name :".

here's some sample text from my word document, including the empty
paragraphs:

Step Name : Step 3
Description : Search for John Doe and press submit

Expected Result : John Doe page is displayed

Step Name : Step 4
Description : Select General Services and click on submit

Expected Result : Services page is displayed

Step Name : Step 5
......

thanks for your help.

cheers,
thomas

On Feb 5, 11:15 am, "Doug Robbins - Word MVP"
<d...@REMOVECAPSmvps.org> wrote:
> Show us the full code that you are now using.  It should not do what you
> says it does because after the range is expanded to include the next 6
[quoted text clipped - 65 lines]
> > cheers,
> > thomas
toth.usenet@gmail.com - 05 Feb 2008 11:01 GMT
hi doug,

i now have a working solution. i've modified the collapsing section to
also include collapsing the selection. this way the selection is
guaranteed to continue after the created table.

here's the working code:

Sub TestingTable()

Dim myrange As Range
   Selection.HomeKey wdStory
   Selection.Find.ClearFormatting
   With Selection.Find
       Do While .Execute(findText:="Step Name :", Forward:=True, _
               MatchWildcards:=False, MatchCase:=True,
Wrap:=wdFindStop) = True
           Set myrange = Selection.Paragraphs(1).Range
           myrange.MoveEnd wdParagraph, 6
           'myrange.Select
           myrange.ConvertToTable Separator:=wdSeparateByParagraphs,
NumColumns:=7, _
               NumRows:=1,
InitialColumnWidth:=CentimetersToPoints(2), Format:= _
               wdTableFormatNone, ApplyBorders:=True,
ApplyShading:=True, ApplyFont:= _
               True, ApplyColor:=True, ApplyHeadingRows:=True,
ApplyLastRow:=False, _
               ApplyFirstColumn:=True, ApplyLastColumn:=False,
AutoFit:=True, _
               AutoFitBehavior:=wdAutoFitFixed
           'myrange.Select
           'myrange.Collapse wdCollapseEnd
           Selection.End = myrange.End
           Selection.Collapse wdCollapseEnd
       Loop
   End With

End Sub

thanks again for your help and time,

thomas

On Feb 5, 11:15 am, "Doug Robbins - Word MVP"
<d...@REMOVECAPSmvps.org> wrote:
> Show us the full code that you are now using.  It should not do what you
> says it does because after the range is expanded to include the next 6
[quoted text clipped - 65 lines]
> > cheers,
> > thomas
 
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.