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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find text in table and assign the found text to another table

Status
Not open for further replies.

PStrongs

Instructor
Oct 30, 2007
30
0
0
GB
Hi,

I am trying to loop through a table in a Word document to find certain text in column 1. If the text is found, the selection is extended into column 2 and both cells selected.

The resultant found text is destined for another table in the same document. The destination table is just a one row two column table ready for the found text. There will usually be more than one instance of the found text, so the destination table has to add rows as new text is found.

the following code works on the main table to find the required text.
Could anyone tell me how to do the above and extend the code for this purpose.

Regards,

'start the find process
For Each tCell In Selection.Cells

'search for x.x.x text
With Selection.Find
.MatchWildcards = True
.Format = True
.Forward = True
.MatchCase = False
.MatchWholeWord = False
.MatchSoundsLike = False
.Execute FindText:=" [0-9]@.[0-9]@.[0-9]@ "
End With

'if the selection is found then assign to table(2)
If Selection.Find.Found = True Then
Selection.Select
Selection.MoveRight wdCharacter, 2, wdExtend
tCellText = LTrim(Replace(Left(Selection.Range.Text, Len(Selection.Range.Text) - 2), Chr(13), ""))
'Do something to assign to the destination table
End If
'move down to next row and continue search
Selection.Move wdRow, 1
Next
End With

 
Hi,

Found my own solution to this issue:

Merge the cells of the destination table
convert Chr(13) to Chr(9) (tab) in found text
insert bookmark into destination table and then run the following code:

With gTable.Cell(2, 1)
.Select
Selection.SelectColumn
'exclude first header
Selection.SetRange Start:=Selection.Cells(2).Range.Start, End:=Selection.End

'start the find process
For Each tCell In Selection.Cells

'search for x.x.x text
With Selection.Find
.MatchWildcards = True
.Format = True
.Forward = True
.MatchCase = False
.MatchWholeWord = False
.MatchSoundsLike = False
.Execute FindText:=" [0-9]@.[0-9]@.[0-9]@ "
End With

'if the selection is found then assign to table(2)
If Selection.Find.Found = True Then
Selection.Select
Selection.MoveRight wdCharacter, 2, wdExtend
tCellText = LTrim(Replace(Left(Selection.Range.Text, Len(Selection.Range.Text) - 2), Chr(13), Chr(9)))
rng.InsertAfter tCellText
rng.Paragraphs.Add
End If
'move down to next row and continue search
Selection.Move wdRow, 1
Next
End With

'Remove extra carriage return from table
With pTable
.Cell(1, 1).Select
Selection.EndKey
Selection.TypeBackspace
End With

'check if EO cell contains text. If not, warn the user.
'If the user chooses to continue, enter default text into cell.
With pTable
If .Cell(3, 1).Range.Text = Chr(13) & Chr(7) Then
noEOs = MsgBox("Blah.", vbExclamation + vbYesNo)
If noEOs = vbYes Then
.AutoFitBehavior wdAutoFitWindow
.Cell(3, 1).Range.Text = "Intentionally Blank"
ElseIf noEOs = vbNo Then
targetDoc.Close wdDoNotSaveChanges
End If
End If
End With

'convert KLP table to two column table
Set tempTable = pTable
Set tempRge = tempTable.ConvertToText(Separator:=wdSeparateByParagraphs)
tempRge.ConvertToTable

'Re-initialize pTable and set borders to default value
Set pTable = targetDoc.Tables(7)
With pTable
.Select
.Style = "Table Grid"
.TopPadding = CentimetersToPoints(0.1)
.BottomPadding = CentimetersToPoints(0.1)
.LeftPadding = CentimetersToPoints(0.19)
.RightPadding = CentimetersToPoints(0.19)
End With

Thanks if you were looking into a solution.

regards,
 
PStrongs,

Actually, I did do a little fiddling with it, and discovered that I could not do it based on your code without having to do more coding than seemed reasonable; I like writing small code. However, I did come up with a fairly short solution based on placing the contents of each cell in
separte variables. It came out this way:

Code:
Sub CellMove()
Selection.ExtendMode = True
Selection.MoveRight (wdWord)
PS1 = Selection
Selection.MoveRight wdCell
PS2 = Selection
ActiveDocument.Bookmarks("DestTable").Select
Selection.Rows.Last.Select
Selection.InsertRowsAbove
Selection = PS1
Selection.MoveRight wdCell
Selection = PS2
End Sub

As you can see, I was suggesting you set the second table as a Bookmark; for convience I named mine "DestTable". This works every time, but as noted it does require that you use two variables for your data. You can still strip anything you want before setting the variable; since you had a handle on that I didn't fuss with it figuring what you really needed was what you didn't know.

Anyway, since this was already done before I got back here and found your second post, I figured I might as well go ahead and send it along.

[thumbsup2]



----------------------------------------------------------------------------------
[small]How did the newspaper describe the swindler who fell off the prison roof during an escape attempt?[/small]
[spineyes]They used a condescending remark.[bugeyed]
 
Hi WalkerEvans,

Your code is far more elegant than mine. I will try it out over the weekend and let you know how I get on.

Thanks for taking the time to post your solution. Much appreciated.

Regards,
 
Hi WalkerEvans,

Just had 5 minutes to tinker with your code. Couldn't try it on the document that the code will be working on because i am not back in the office until Thursday, but it works great!

Some Microsoft related sites recommend creating tables from text rather than assigning text strings to existing tables as this will slow down the process considerably.

I see no difference in performance from your code and creating a table from the text, although i need to try it on the main document as that will contain considerably more text.

Thanks for the code. Much appreciated.

Regards,
 
PStrongs,

Glad to hear that its working well for you. If you have any questions/problems once you get back to the office repost and we'll see if we can solve them.

I can only check back here at odd moments during the workday so if you post something and don't get a real quick response it doesn't mean I'm ignoring you - just taking a break to actually do what they pay me for!

[thumbsup2}

----------------------------------------------------------------------------------
[small]How did the newspaper describe the swindler who fell off the prison roof during an escape attempt?[/small]
[spineyes]They used a condescending remark.[bugeyed]
 
Know the problem!

Just one thing I noticed last night...

When removing the end of cell marker etc:

PS1=Left(PS1, Len(PS1)-2)
PS2=Left(PS2, Len(PS2)-2)

PS1 does just that, removes the end of cell marker and paragraph mark.

However, PS2 removes the last two characters of the found text.
I would have thought that the cell PS2 was working from would also have the end of cell marker and paragaraph mark.

Perhaps I am doing something wrong.

Regards,
 

Have you tried running it just as it was written? When this was put together and tested, it only pulled the data out of the table cells - there was no need for me to fuss with length-of-string at all.

Just a thought, but it might be worth giving it a try. Let me know if this doesn't work and we can debug my code; no one ever accused me of being mysteak freee.

[glasses]

----------------------------------------------------------------------------------
[small]How did the newspaper describe the swindler who fell off the prison roof during an escape attempt?[/small]
[spineyes]They used a condescending remark.[bugeyed]
 
Hi WalkerEvans,

Tried the code on the actual document and all is ok!
Thanks once again for your help.
 

Glad I could be of service. This forum has helped me out many times - being able to help someone else occasionally makes me feel less like a freeloader.

People helping people is what this site is all about - and, it feels good, as I'm sure you'll discover (if you haven't already). Welcome aboard!

[thumbsup2]

----------------------------------------------------------------------------------
[small]How did the newspaper describe the swindler who fell off the prison roof during an escape attempt?[/small]
[spineyes]They used a condescending remark.[bugeyed]
 
Thanks WalkerEvans,

Perhaps when I gain more experience, I too will be able to offer help to others.

Regards and keep well,
 
Gee, <sniff> that is so nice to hear. </sniff>

BTW, and just to be perfectly clear, no...I am not being sarcastic.

It IS nice see people writing like that. Bravo.

faq219-2884

Gerry
My paintings and sculpture
 

Gerry,

Thank you sir! If not for the assistance given so freely by you, Skip, PHV, and others, I would have been far up Defication Inlet without means of propulsion many times. I feel there is a debt owed, and the only way to pay back something like this is to pass it forward to others whenever possible. It seems that every now and then I actually succeed.

Sappy? Perhaps, but that is the way I feel.

[jester2]

----------------------------------------------------------------------------------
"A committee is a life form with six or more legs and no brain." -- L. Long
 
I can only concur with WalkerEvans, because i too have been up said defacation inlet without means of propulsion as well. It has only been through the dedication of Fumei, yourself WalkerEvans and others that have helped me out. It is also a learning curve for me and hopefully, when I feel confident to assist others i wll be there to help too.

Thanks to you all.

Regards,
 
Keep going people. That is what Tek-Tips is about.

PStrongs, the first part of confidence itself is the courage to use it.

The second part is resiliance, the ability to emotionally survive when you make mistakes.

The third part is the knowledge.

I have gained knowledge (the third part, by surviving mistakes (the second part) when trying teach it (the first part).

I am still very much learning. I hope it never stops.

I can not think of the number of times someone has posted a question that I did not know the answer to. In fact, often I would go: "Huh? I am lost on the concept." Then spent hours trying.

First, to clearly understand the issue/problem. You may have noticed that I am a bit of a bear regarding that.

Second, to see if I could figure it out. For three reasons. It was fun to try and figure it out. It was fun to actually do so (sometimes), and it was (and is) fun to help.

faq219-2884

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

Part and Inventory Search

Sponsor

Back
Top