PDA

View Full Version : [SOLVED:] Macro search text BOLD UPPERCASE if within table row and apply style Word



jec1
02-06-2022, 08:04 PM
Hi, attach document.

Would like to search document using VBA to change BOLD UPPERCASE heading that only appears "within a table row (always shaded)" and apply heading style Heading UCPR.

The doc downloads in Normal usually.

Many thanks for any assistance.

Janine

gmayor
02-06-2022, 11:47 PM
The following will work in your document.

Sub Macro1()
Const sPwd As String = "" 'the form password if any
Dim oTable As Table
Dim oCell As Cell
Dim oRng As Range
Dim lProtect As Long
lProtect = ActiveDocument.ProtectionType
If Not ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Unprotect Password:=sPwd
End If
For Each oTable In ActiveDocument.Tables
For Each oCell In oTable.Range.Cells
Set oRng = oCell.Range
oRng.End = oRng.End - 1
If oRng.Case = wdUpperCase And oRng.Font.Bold = True Then
oRng.Style = "Heading_UCPR"
End If
Next oCell
Next oTable
ActiveDocument.Protect Type:=lProtect, NoReset:=True, Password:=sPwd
Set oTable = Nothing
Set oCell = Nothing
End Sub

jec1
02-07-2022, 02:08 PM
Many thanks Graham - perfect.:)