|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
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 |
#2
|
||||
|
||||
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 [Fmr MS MVP - Word] |
#3
|
|||
|
|||
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. |
#4
|
||||
|
||||
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 [Fmr MS MVP - Word] |
#5
|
|||
|
|||
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. |
Tags |
macro |
|
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 |
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 |
How to find and select text in a document? | mkhuebner | Word VBA | 8 | 02-04-2014 08:04 PM |
Need help on Macro 03- Find text - if text is blank then remove line | simpleonline1234 | Word VBA | 1 | 02-25-2011 02:28 AM |