Find underlined text in a cell and select the line

K

krini_pop

I need to find underlined text in a table cell. When I find it, I need
to select the line it is on and change the font of the whole line to
bold. Here is the code snippet I have so far.

wd_tbl.Cell(9, 7).Select
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Font.Underline = wdUnderlineSingle
Do While .Execute(Forward:=True, Wrap:=wdFindStop,
Format:=True) = True
Set cell_range = Selection.Bookmarks("\line").Range
cell_range.Font.Bold *****fails on this statement, invalid
use of property*****
Loop
End With

Please help.

Thanks,
Trina
 
G

Greg Maxey

This may work for you:

Sub Scratchmacro()
Dim oRng As Word.Range
Dim i As Long
Dim j As Long
Set oRng = Selection.Tables(1).Range
With oRng.Find
.Font.Underline = wdUnderlineSingle
While .Execute
i = oRng.Cells(1).RowIndex
For j = 1 To oRng.Tables(1).Columns.Count
oRng.Tables(1).Cell(i, j).Range.Font.Underline =
wdUnderlineSingle
Next j
Wend
End With
End Sub
 
G

Greg Maxey

My last won't work because you wanted the whole line bold, not
underlined. Try:

Sub Scratchmacro()
Dim oRng As Word.Range
Dim i As Long
Dim j As Long
Set oRng = Selection.Tables(1).Range
With oRng.Find
.Font.Underline = wdUnderlineSingle
While .Execute
i = oRng.Cells(1).RowIndex
For j = 1 To oRng.Tables(1).Columns.Count
oRng.Tables(1).Cell(i, j).Range.Font.Bold = True
Next j
Wend
End With
End Sub
 
H

Helmut Weber

Hi Trina,

have a look at this one:

Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Font.Underline = wdUnderlineSingle
While .Execute
If rDcm.Information(wdWithInTable) Then
rDcm.Select
rDcm.Bookmarks("\line").Range.Font.Bold = True
End If
Wend
End With
End Sub

Depending on whether there is more underlined
text outside of tables or inside of tables,
the speed will vary.

I didn't want to make it more complicated than necessary.

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
K

krini_pop

Thanks Guys. I'm able to change the font to bold now, however, it's not
just doing one cell, it's doing the entire table. I just wanted to
select one cell and change the font in that one cell. Could you help?

Set cell_range = wd_tbl.Cell(9, 7).Range
cell_range.Find.ClearFormatting
With cell_range.Find
.Font.Underline = wdUnderlineSingle
While .Execute
cell_range.Select
Selection.Bookmarks("\line").Range.Font.Bold = True
Wend
End With
 
H

Helmut Weber

hmm...,

there were some unexpected complications.

How about this one:

Sub Test99()
Dim rDcm As Range
Set rDcm = ActiveDocument.Tables(1).Cell(5, 2).Range
With rDcm.Find
.Text = ""
.Font.Underline = wdUnderlineSingle
While .Execute
If rDcm.Information(wdWithInTable) Then
rDcm.Select
rDcm.Bookmarks("\line").Range.Font.Bold = True
End If
Wend
End With
End Sub
 
K

krini_pop

That still does the whole file. When I did this......
With cell_range.Find
.Text = ""
.Font.Underline = wdUnderlineSingle
.Execute
If .Found Then
cell_range.Select
Selection.Bookmarks("\line").Range.Font.Bold = True
End If
End With

It only did that cell, but it only found one instance of the underlined
text. Is there a parameter or something for .Execute that would allow
to continue just through the cell, not the whole document?

Thanks
 
H

Helmut Weber

Hi,

hmm...

look closer at the sample I gave you.

Don't you like "while .execute wend"?

"If found" processes the very first, and only, found match.
Quite as you coded it.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
K

krini_pop

The while loop works, but a little too well. It does the whole table
instead of just the cell.
I'm trying to think of a way that it would just do the cell. If I
can't, then at the end of all of my processing, I would just select the
whole table and make the font not to be bold. I have to take the bold
off anyway after I do some comparisons with the text.

Trina
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top