Microsoft Office Forums Microsoft Word macro to find text, select all text between brackets, and delete

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-05-2015, 12:10 PM
helal1990 helal1990 is offline Microsoft Word macro to find text, select all text between brackets, and delete Windows 7 64bit Microsoft Word macro to find text, select all text between brackets, and delete Office 2010 64bit
Novice
Microsoft Word macro to find text, select all text between brackets, and delete
 
Join Date: Feb 2015
Posts: 3
helal1990 is on a distinguished road
Default Microsoft Word macro to find text, select all text between brackets, and delete

Hello,



I'm trying to write a word macro that will find text that will be enclosed between brackets, and select everything between the brackets, then delete.


The text will need to be input from the user as a full model number like this:


MVAV-A1-B3-C2 ...etc, where A1, B3, and C2 are different configurations.


The word file is written like this (for example): The unit will use [A1 - chilled water] [A2 - compressors] for cooling.


Based on a model # MVAV-A1, I want the macro to search for all text "A2" (the negative of A1) in the word doc, select/highlight everything between the brackets "[A1 - compressors]", and delete it.



So far I'm just trying to get it to find a single instance of "A1", and then find the nearest [ bracket to the left, and ] bracket to the right, and select everything in between. Then eventually put it in a loop.


With the research that I've done, I've got this, which obviously does not do the job:
Sub EditFindLoopExample()
With Selection.find .ClearFormatting .Text = "1A" .Forward = True .MatchWildcards = False .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Extend Selection.find.ClearFormatting With Selection.find .Text = "]" .Replacement.Text = "" .Forward = True .Wrap = wdFindAsk .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With With Selection.find
.Text = "]"
.Forward = True
.Execute
.Text = ""
End With End Sub

I was wondering if anyone here can help me fill the blanks on how to get this to work. Any help would be greatly appreciated.



Thanks for reading.

Admin Note: Deleted multiple embedded links to post at http://stackoverflow.com/questions/28351992/microsoft-word-macro-to-find-text-select-all-text-between-brackets-and-delete#

Last edited by macropod; 02-05-2015 at 02:14 PM. Reason: Deleted multiple links to stackoverflow.com
Reply With Quote
  #2  
Old 02-05-2015, 02:18 PM
macropod's Avatar
macropod macropod is offline Microsoft Word macro to find text, select all text between brackets, and delete Windows 7 64bit Microsoft Word macro to find text, select all text between brackets, and delete Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,577
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

You don't need a macro for this - you can use a wildcard Find/Replace, where:
Find = \[A[!1]*\]
Replace = nothing
This will delete all of the [A# text] where # is any number other than 1.

Cross-posted at: http://stackoverflow.com/questions/2...ets-and-delete#
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #3  
Old 02-05-2015, 03:15 PM
helal1990 helal1990 is offline Microsoft Word macro to find text, select all text between brackets, and delete Windows 7 64bit Microsoft Word macro to find text, select all text between brackets, and delete Office 2010 64bit
Novice
Microsoft Word macro to find text, select all text between brackets, and delete
 
Join Date: Feb 2015
Posts: 3
helal1990 is on a distinguished road
Default

Thanks for the reply macropad. I want to use a macro because I want to automate a process at work, and I need to make it easy for the older guys to use. Pretty much I want the final version to only need a model number as the input.

Thanks again.
Reply With Quote
  #4  
Old 02-05-2015, 03:22 PM
macropod's Avatar
macropod macropod is offline Microsoft Word macro to find text, select all text between brackets, and delete Windows 7 64bit Microsoft Word macro to find text, select all text between brackets, and delete Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,577
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

You could, of course, record the Find/Replace as a macro, though I'm not sure what such a macro would achieve - you'd still have to teach your people how to use one. You might do better to set the document up as a Word form, using formfields and use field coding to handle the conditional text, or use content controls in conjunction with a ContentControlOnExit macro to output the conditional text (or update fields containing it).
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #5  
Old 02-05-2015, 03:52 PM
helal1990 helal1990 is offline Microsoft Word macro to find text, select all text between brackets, and delete Windows 7 64bit Microsoft Word macro to find text, select all text between brackets, and delete Office 2010 64bit
Novice
Microsoft Word macro to find text, select all text between brackets, and delete
 
Join Date: Feb 2015
Posts: 3
helal1990 is on a distinguished road
Default

I envisioned it as having the user enter the model # and then just pressing a macro button that will create the doc fully formatted. The Word form may be a better approach though. I'll update the page after I investigate it a bit.

PS. just read the notes about cross-posting. Thanks for letting me know.
Reply With Quote
Reply

Tags
macro

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to find text in between two characters and then format selected text? qcom Word 5 02-19-2015 11:23 PM
Microsoft Word macro to find text, select all text between brackets, and delete VBA code for Microsoft Word macro select text and insert footnote ndnd Word VBA 10 01-06-2015 01:47 PM
Macro to find coloured text and replace with form-field/formtext containing that text tarktran Word VBA 1 11-26-2014 08:12 AM
Microsoft Word macro to find text, select all text between brackets, and delete How to find and select text in a document? mkhuebner Word VBA 8 02-04-2014 08:04 PM
Microsoft Word macro to find text, select all text between brackets, and delete Need help on Macro 03- Find text - if text is blank then remove line simpleonline1234 Word VBA 1 02-25-2011 02:28 AM


All times are GMT -7. The time now is 02:19 PM.


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