Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-28-2013, 10:58 PM
Adriano Adriano is offline Find Replace Windows 7 64bit Find Replace Office 2010 32bit
Novice
Find Replace
 
Join Date: Feb 2013
Posts: 10
Adriano is on a distinguished road
Default 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
My question is:

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
Reply With Quote
  #2  
Old 03-01-2013, 12:49 AM
macropod's Avatar
macropod macropod is offline Find Replace Windows 7 64bit Find Replace Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
Note that, as coded, you can only search for one string at a time, though you could change the Find parameters to allow wildcard searches, which can be more flexible. Also, with the way I've coded the macro, you could omit the text to find and just have Word find the Style - or you could omit the Style and have Word find just the text; it's up to you.

PS: When posting code, please use the code tags. They're on the 'Go Advanced' tab.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 03-01-2013, 03:56 AM
Adriano Adriano is offline Find Replace Windows 7 64bit Find Replace Office 2010 32bit
Novice
Find Replace
 
Join Date: Feb 2013
Posts: 10
Adriano is on a distinguished road
Default

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
Reply With Quote
  #4  
Old 03-01-2013, 03:59 AM
macropod's Avatar
macropod macropod is offline Find Replace Windows 7 64bit Find Replace Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #5  
Old 03-01-2013, 04:03 AM
Adriano Adriano is offline Find Replace Windows 7 64bit Find Replace Office 2010 32bit
Novice
Find Replace
 
Join Date: Feb 2013
Posts: 10
Adriano is on a distinguished road
Default

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
Reply With Quote
  #6  
Old 03-01-2013, 04:09 AM
macropod's Avatar
macropod macropod is offline Find Replace Windows 7 64bit Find Replace Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
Reply

Tags
find, replace, tables

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Find/Replace or VBA davidku Word 4 04-27-2012 04:39 PM
Find Replace Bad view when using Find and Find & Replace - Word places found string on top line paulkaye Word 4 12-06-2011 11:05 PM
Find Replace Is there a way to use "find/replace" to find italics words? slayda Word 3 09-14-2011 02:16 PM
Find Replace Find and Replace kjxavier Excel 3 08-12-2011 10:49 PM
Find Replace Help with find and replace or query and replace shabbaranks Excel 4 03-19-2011 08:38 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:55 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