#1
|
|||
|
|||
Macro to format all occurrences of a text in a table
Hi,
I'd like to find a macro that search a table, find all occurrences of a given text, select the whole row and format it in bold and different font size. Then it would have to convert the row to text, move it one line down and center it. I've searched but cant find how to convert to text then move down... Thanks 😉 |
#2
|
||||
|
||||
Probably something like
Code:
Sub Macro1() Dim oTable As Table Dim oRng As Range Dim oRow As Row Const strText As String = "Text to find" Set oTable = Selection.Tables(1) For Each oRow In oTable.Rows If InStr(1, oRow.Range, strText) > 0 Then Set oRng = oRow.Range oRow.ConvertToText Separator:=Chr(9) oRng.Font.Bold = True oRng.Font.Size = 14 oRng.ParagraphFormat.Alignment = wdAlignParagraphCenter oRng.InsertParagraphBefore End If Next oRow lbl_Exit: Set oTable = Nothing Set oRng = Nothing Set oRow = Nothing Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Thanks, it works fine but not if I have more than one table in the document.
I've tried with "For each oTable in Active Document.Tables" but with no success... |
#4
|
|||
|
|||
This is a variation of Grahams macro. (Code tags not working)
Sub BoldCenterText() With ActiveDocument ChangeFont ActiveDocument End With End Sub Function ChangeFont(wdDoc) Dim oTable As Table Dim oRng As Range Dim oRow As Row Dim strText As String strText = InputBox("Search text?") For Each oTable In wdDoc.Tables For Each oRow In oTable.Rows If InStr(1, oRow.Range, strText) > 0 Then Set oRng = oRow.Range oRow.ConvertToText Separator:=Chr(9) oRng.Font.Bold = True oRng.Font.Size = 14 oRng.ParagraphFormat.Alignment = wdAlignParagraphCenter oRng.InsertParagraphBefore End If Next oRow Next lbl_Exit: Set oTable = Nothing Set oRng = Nothing Set oRow = Nothing Exit Function End Function |
#5
|
|||
|
|||
Thanks a lot but just like Grahams macro it stops after finding the first occurence on some documents while on others it works fine...
I'll investigate that this week end. Thanks to both of you |
#6
|
|||
|
|||
That's weird. I tested it on a document that has 20 tables and it worked. Let me know what you find.
|
#7
|
|||
|
|||
Could be related to the fact that my document is a letter doc after a mail merge...
I'll check that |
#8
|
|||
|
|||
If you want to do this in a lot of documents first you need to say if those documents are opened or you want to open and edit one by one.
__________________
Backup your original file before doing any modification. |
#9
|
||||
|
||||
If you are having problems with some documents it could be due to something like...
Tables with merged cells can be prone to breaking on commands which call for a row like "For Each oRow In oTable.Rows". If you step through a macro, you can probably identify when the issue arises and diagnose it from there.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#10
|
|||
|
|||
Weird, advancing step by step through the macro, everything works fine but when I run the macro it keeps stopping after the first occurence...
|
#11
|
|||
|
|||
Is it possible to attach a sample document?
|
#12
|
|||
|
|||
Sorry, finally got it to work and forgot to answer here...
Thanks for the help |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
How to I create a macro that dynamically creates a table, to enter questions, in the below format? | MathiasFC | Word VBA | 11 | 01-14-2019 04:50 PM |
Macro to delete text from cells with specific format | Soenke | Word VBA | 4 | 09-01-2016 08:55 AM |
Variable text field code based on occurrences on each page | Cosmo | Word | 2 | 12-29-2015 11:54 AM |
Macro to find text in between two characters and then format selected text? | qcom | Word | 5 | 02-19-2015 11:23 PM |
Replace all occurrences of one merge field with plain text | blankenthorst | Mail Merge | 5 | 07-01-2011 05:18 AM |