Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I select specific paragraphs without knowing how many there are?

Status
Not open for further replies.

clhare

Technical User
May 29, 2003
118
US
I have a generated document that needs to have the text converted to tables. Is there a way to put a bookmark at the start and end of each "table" (or search for specific keywords) to select all the paragraphs to be converted to a single table? The number of rows needed may vary, but many of them start with the same text in the first "row" and end with the same text in the last "row".

Any help is greatly appreciated!

Cheryl
 
Hi Cheryl,

"Is there a way to put a bookmark at the start and end..."
==>Yes.

Here is a simple piece of code, that should help you to get on your way, if you adapt the starter and ender string accordignly:
Code:
Sub TableIt()
Dim starter As String, ender As String
Dim RanStart As Long, RanEnd As Long
'...

starter = "Hello"
ender = "Good bye"
Selection.HomeKey unit:=wdStory
With Selection.Find
    .ClearFormatting
    .Text = starter
    .MatchWildcards = False
    .MatchCase = False
    .Execute
End With

If Selection.Find.Found Then
    RanStart = Selection.Range.Start
    Selection.Collapse wdCollapseEnd
    With Selection.Find
        .Text = ender
        .Execute
    End With
    RanEnd = Selection.Range.End
    Selection.SetRange RanStart, RanEnd
    Selection.ConvertToTable vbTab
End If

End Sub

And I didn#t even need a bookmark... :)
;-)

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
You are wonderful! Thank you so much! I had no idea where to start!

Any help is greatly appreciated!

Cheryl
 
'Twas my pleasure.
[orientalbow]

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
One more question for you...

How do I make it loop through the rest of the document and convert all similar tables?

Any help is greatly appreciated!

Cheryl
 
That's not too difficult. Just wrap a loop around the Find algorithm like this (I've bolded the changes):
Code:
Sub TableIt()
Dim starter As String, ender As String
Dim RanStart As Long, RanEnd As Long
'...

starter = "Hello"
ender = "Good bye"
Selection.HomeKey unit:=wdStory
With Selection.Find
    .ClearFormatting
    .Text = starter
    .MatchWildcards = False
    .MatchCase = False
    .Execute
End With
[b]
Do While Selection.Find.Found[/b]
    RanStart = Selection.Range.Start
    Selection.Collapse wdCollapseEnd
    With Selection.Find
        .Text = ender
        .Execute
    End With
    RanEnd = Selection.Range.End
    Selection.SetRange RanStart, RanEnd
    Selection.ConvertToTable vbTab
    [b]
    Selection.MoveRight unit:=wdCharacter, Count:=1
    
    With Selection.Find
        .Text = starter
        .Execute
    End With
Loop[/b]

End Sub
:)

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
I put text for three tables in the document (same text three times), but the macro only converts the first one to a table even tho I've added the loop.

Any help is greatly appreciated!

Cheryl
 
Oopsy, my mistake!

I forgot one line.
Add this line:
Code:
Selection.Collapse wdCollapseEnd
right before this one
Code:
Selection.MoveRight unit:=wdCharacter, Count:=1
so that it looks like this:
Code:
...
Selection.SetRange RanStart, RanEnd
    Selection.ConvertToTable vbTab
    
    Selection.Collapse wdCollapseEnd
    Selection.MoveRight unit:=wdCharacter, Count:=1
    
    With Selection.Find
...


[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
Nope, still only converting the first table.

Any help is greatly appreciated!

Cheryl
 
That's strange.
I've tested it, and it converted all 4 tables in my test document.
Are you sure, the start- and end-qualifying strings are the exact same for all tables?
Have you executed the code in single step mode to check what happens after the first table is converted?

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
I copied the same text three times. Can you attach your test document? I don't think I can attach as I don't belong to box.net.

Any help is greatly appreciated!

Cheryl
 
Unfortunately, I can't attach right now either.
I could, once I'm back at my stay - but that'll be tomorrow evening...

My test document is no big deal, it's just some tabbed gibberish I entered..
Hello [tab] jkgh [tab] b [tab] ijg [tab] ig
Aset
Re [tab]
[tab] [tab] Set bvwer
[tab] Se tve uzghrt
Recrcq2c [tab] [tab] Good bye
I copied that stuff three times and add a bit of tabbed gibberish here, deleted some there to check how well the code will swollow different column numbers too.
[pacman]
I have also checked, whether there will be a problem, if the tables follow each other directly, without blank line in between - it worked.
The code did NOT work though, after the first table was already converted to a table.
That means: I ran the code, but broke it off after the first table conversion, then re-ran it. THAT did not work.

Just to be on the safe side, here's the entire code with loop and selection.collapse on the right place, that worked with my 4-table-test doc:
Code:
Sub TableIt()
Dim starter As String, ender As String
Dim RanStart As Long, RanEnd As Long
'...

starter = "Hello"
ender = "Good bye"
Selection.HomeKey unit:=wdStory
With Selection.Find
    .ClearFormatting
    .Text = starter
    .MatchWildcards = False
    .MatchCase = False
    .Execute
End With

Do While Selection.Find.Found
    RanStart = Selection.Range.Start
    Selection.Collapse wdCollapseEnd
    With Selection.Find
        .Text = ender
        .Execute
    End With
    RanEnd = Selection.Range.End
    Selection.SetRange RanStart, RanEnd
    Selection.ConvertToTable vbTab
    
    Selection.Collapse wdCollapseEnd
    Selection.MoveRight unit:=wdCharacter, Count:=1
    
    With Selection.Find
        .Text = starter
        .Execute
    End With
Loop

End Sub
I wonder why it doesn't seem to work for you.
[ponder]

I have tested it on Word 2003, but the code does not use any fancy stuff, so it should also run on other versions.
Not sure about 2007 though.

Wait a sec, I DO have Word 2000.... - and it runs just fine there, too...
[3eyes]

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
That helped! I compared your code to mine and found that I had added a line as shown in bold below:

Code:
with Selection.Find
     .ClearFormatting
     .Text = starter
     [b].Font.Bold = False[/b]
     .MatchWildcards = False
     .MatchCase = False
     .Execute
End With
and

Code:
with Selection.Find
     .Text = ender
     [b].Font.Bold = False[/b]
     .Execute
End With

Once I removed that code, it converted all three tables.

Thanks so much for your help!!

Cheryl [bigsmile]
 
If only things were always that simple! [tongue]
Glad you got it sorted!

Andy

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
Here is the same thing essentially, but without using Selection.
Code:
Sub TableIt()
Dim starter As String, ender As String
Dim RanStart As Long
Dim r As Range
Dim MakeTable As Range

starter = "Hello"
ender = "Good bye"
Set r = ActiveDocument.Range

With r.Find
   .ClearFormatting
   Do While .Execute(FindText:=starter, _
                Forward:=True) = True
     RanStart = r.Start
     r.End = ActiveDocument.Range.End
     With r.Find
        .Text = ender
        .Execute
     End With
     Set MakeTable = ActiveDocument.Range(Start:=RanStart, _
           End:=r.End)
     MakeTable.ConvertToTable (vbTab)
     r.Collapse Direction:=wdCollapseEnd
   Loop
End With
End Sub
It makes a Range of the document, and then searches for "starter".

It sets a long variable for the starting value of the found instance. It resizes the running Range End to the end of the document, and searches for "ender". A Range is set for the values from "starter" Start, to "ender" End (the current searching range End value). That range is converted to a table.

The original searching range collapses to its end (the end of "ender") and starts to search for the next "starter".

It continues until there are no more "starter" instances found.

It is not much different from what MakeItSo posted. It just does not use Selection. Also, by using the Do While on the .Execute instruction - rather than MakeItSo's .Found instruction - there is no need to reset the Selection.Find text as "starter", which his code does require.

faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top