#1
|
|||
|
|||
Find Replace
Hi,
I'm new to VBA, but I think I need one to my problem. The situation is: I have a document with text and tables like this b) text (the first cell is a), b), c) and so on... and the second is a text) I have another a) b) and c) in the document I don't want to change, so I've managed to format those cells with a special style like below: Code:
Sub format_tables() Dim tTable As Table For Each tTable In ActiveDocument.Tables tTable.Select Selection.Style = ActiveDocument.Styles("Table-1") Next End Sub I want to find those formated a) b) c) and so on and convert the table in which they are in regular text. Thank you, Adriano Last edited by macropod; 03-01-2013 at 12:13 AM. Reason: Added code tags & formatting |
#2
|
||||
|
||||
Hi Adriano,
You could use a macro like: Code:
Sub TableFindConvert() Application.ScreenUpdating = False Dim StrTxt As String, StrSty As String, Stl As Style, bSty As Boolean StrTxt = InputBox("What is the Text to Find", "Text Selection") StrSty = Trim(InputBox("What is the Style to Find", "Style Selection", "Table-1")) bSty = False If StrSty <> "" Then For Each Stl In ActiveDocument.Styles If Stl.NameLocal = StrSty Then bSty = True Exit For End If Next If bSty = False Then GoTo errExit End If With ActiveDocument.Range With .Find .ClearFormatting .Replacement.ClearFormatting .Text = StrTxt If StrSty <> "" Then .Style = StrSty .Replacement.Text = "" .Format = bSty .Forward = True .Wrap = wdFindStop .MatchCase = True .MatchWholeWord = True .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Execute End With Do While .Find.Found If .Information(wdWithInTable) = True Then .Duplicate.Tables(1).ConvertToText Separator:=vbTab End If .Collapse wdCollapseEnd .Find.Execute Loop End With Exit Sub errExit: Beep Application.ScreenUpdating = True End Sub PS: When posting code, please use the code tags. They're on the 'Go Advanced' tab.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Thank you, Paul.
How can I adapt this code for searching using this following wildcards ([a-z]\)) and the style named "tabela-1"? I ask that because I have about 6000 documents and wouldn't place a text for each of them. I'm sorry about the tags. Thank you again, Adriano |
#4
|
||||
|
||||
For the wildcards, simply replace these lines:
.MatchCase = True .MatchWholeWord = True .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False with: .MatchWildcards = True You can make the Style whatever you want, by overtyping the default displayed by the InputBox. To change the default, simple change 'Table-1' in the code to whatever you want.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Paul,
Thanks a lot! I've managed to solve the puzzle with your help. The code is: Code:
Sub remove_tables() Application.ScreenUpdating = False With ActiveDocument.Range With .Find .ClearFormatting .Replacement.ClearFormatting .Text = "([a-z]\))" .Style = "table-1" .Replacement.Text = "" .Format = bSty .Forward = True .Wrap = wdFindStop .MatchCase = True .MatchWholeWord = True .MatchWildcards = True .MatchSoundsLike = False .MatchAllWordForms = False .Execute End With Do While .Find.Found If .Information(wdWithInTable) = True Then .Duplicate.Tables(1).ConvertToText Separator:=vbTab End If .Collapse wdCollapseEnd .Find.Execute Loop End With Exit Sub errExit: Beep Application.ScreenUpdating = True End Sub Last edited by macropod; 02-08-2021 at 10:44 PM. Reason: Added code tags |
#6
|
||||
|
||||
Still not using code tags ...
Glad you got the coding sorted. Do bear in mind, though, that your modifications make the code less flexible. As I also said previously, you could omit the text parameter entirely and just search for the styles.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Tags |
find, replace, tables |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Using Find/Replace or VBA | davidku | Word | 4 | 04-27-2012 04:39 PM |
Bad view when using Find and Find & Replace - Word places found string on top line | paulkaye | Word | 4 | 12-06-2011 11:05 PM |
Is there a way to use "find/replace" to find italics words? | slayda | Word | 3 | 09-14-2011 02:16 PM |
Find and Replace | kjxavier | Excel | 3 | 08-12-2011 10:49 PM |
Help with find and replace or query and replace | shabbaranks | Excel | 4 | 03-19-2011 08:38 AM |