Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-26-2019, 12:16 AM
Dzib Dzib is offline Macro to format all occurrences of a text in a table Windows 10 Macro to format all occurrences of a text in a table Office 2019
Novice
Macro to format all occurrences of a text in a table
 
Join Date: Jul 2019
Posts: 29
Dzib is on a distinguished road
Default 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 😉
Reply With Quote
  #2  
Old 07-26-2019, 02:05 AM
gmayor's Avatar
gmayor gmayor is offline Macro to format all occurrences of a text in a table Windows 10 Macro to format all occurrences of a text in a table Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #3  
Old 07-26-2019, 06:26 AM
Dzib Dzib is offline Macro to format all occurrences of a text in a table Windows 10 Macro to format all occurrences of a text in a table Office 2019
Novice
Macro to format all occurrences of a text in a table
 
Join Date: Jul 2019
Posts: 29
Dzib is on a distinguished road
Default

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...
Reply With Quote
  #4  
Old 07-26-2019, 07:46 AM
kilroy kilroy is offline Macro to format all occurrences of a text in a table Windows 10 Macro to format all occurrences of a text in a table Office 2016
Competent Performer
 
Join Date: Sep 2016
Location: Southern Ontario
Posts: 118
kilroy is on a distinguished road
Default

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
Reply With Quote
  #5  
Old 07-26-2019, 08:00 AM
Dzib Dzib is offline Macro to format all occurrences of a text in a table Windows 10 Macro to format all occurrences of a text in a table Office 2019
Novice
Macro to format all occurrences of a text in a table
 
Join Date: Jul 2019
Posts: 29
Dzib is on a distinguished road
Default

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
Reply With Quote
  #6  
Old 07-26-2019, 08:28 AM
kilroy kilroy is offline Macro to format all occurrences of a text in a table Windows 10 Macro to format all occurrences of a text in a table Office 2016
Competent Performer
 
Join Date: Sep 2016
Location: Southern Ontario
Posts: 118
kilroy is on a distinguished road
Default

That's weird. I tested it on a document that has 20 tables and it worked. Let me know what you find.
Reply With Quote
  #7  
Old 07-26-2019, 09:32 AM
Dzib Dzib is offline Macro to format all occurrences of a text in a table Windows 10 Macro to format all occurrences of a text in a table Office 2019
Novice
Macro to format all occurrences of a text in a table
 
Join Date: Jul 2019
Posts: 29
Dzib is on a distinguished road
Default

Could be related to the fact that my document is a letter doc after a mail merge...
I'll check that
Reply With Quote
  #8  
Old 07-27-2019, 02:44 AM
eduzs eduzs is offline Macro to format all occurrences of a text in a table Windows 10 Macro to format all occurrences of a text in a table Office 2010 32bit
Expert
 
Join Date: May 2017
Posts: 262
eduzs is on a distinguished road
Default

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.
Reply With Quote
  #9  
Old 07-27-2019, 04:12 AM
Guessed's Avatar
Guessed Guessed is offline Macro to format all occurrences of a text in a table Windows 10 Macro to format all occurrences of a text in a table Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,967
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #10  
Old 07-28-2019, 05:53 AM
Dzib Dzib is offline Macro to format all occurrences of a text in a table Windows 10 Macro to format all occurrences of a text in a table Office 2019
Novice
Macro to format all occurrences of a text in a table
 
Join Date: Jul 2019
Posts: 29
Dzib is on a distinguished road
Default

Weird, advancing step by step through the macro, everything works fine but when I run the macro it keeps stopping after the first occurence...
Reply With Quote
  #11  
Old 07-30-2019, 05:33 AM
kilroy kilroy is offline Macro to format all occurrences of a text in a table Windows 10 Macro to format all occurrences of a text in a table Office 2016
Competent Performer
 
Join Date: Sep 2016
Location: Southern Ontario
Posts: 118
kilroy is on a distinguished road
Default

Is it possible to attach a sample document?
Reply With Quote
  #12  
Old 08-05-2019, 11:24 PM
Dzib Dzib is offline Macro to format all occurrences of a text in a table Windows 10 Macro to format all occurrences of a text in a table Office 2019
Novice
Macro to format all occurrences of a text in a table
 
Join Date: Jul 2019
Posts: 29
Dzib is on a distinguished road
Default

Sorry, finally got it to work and forgot to answer here...

Thanks for the help
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to format all occurrences of a text in a table 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 format all occurrences of a text in a table Macro to delete text from cells with specific format Soenke Word VBA 4 09-01-2016 08:55 AM
Macro to format all occurrences of a text in a table 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
Macro to format all occurrences of a text in a table Replace all occurrences of one merge field with plain text blankenthorst Mail Merge 5 07-01-2011 05:18 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:36 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft