Search for text options enclosed by brackets and replace with correct option

  • Thread starter msnews.microsoft.com
  • Start date
M

msnews.microsoft.com

I'd like to write a macro that would search through a word document and
search for an instance of text enclosed with square brackets "[.....]" and
parse out each option that immediately follows.

Example text from document would be as follows:
If changes are required, notify [Departmental Representative] [Engineer]
[Consultant] of these changes before they are made.

I'd like to essentially parse out the following options where I'd place them
on a form for the user to select the correct option:
Departmental Representative
Engineer
Consultant

So if the user selected Engineer then I'd like to update the original
document to read:
If changes are required, notify Engineer of these changes before they are
made.

At this point I'd run the macro again looking for the next set of brackets.

I know how to search for the start point with the following code:

Selection.Find.ClearFormatting
With Selection.Find
.Text = "\[*\]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute

This code will find [Departmental Representative] but from there I'm not
sure how to parse immediately after the close bracket to see if there is
another option (space followed by a open bracket.

Does anyone know how I would go about doing this?

Thanks
Dustin Wilson
 
R

Russ

Dustin,
Take a look at this page/site before going any further. Sometimes people try
to reinvent a process that is already included in Word.
I'd like to write a macro that would search through a word document and
search for an instance of text enclosed with square brackets "[.....]" and
parse out each option that immediately follows.

Example text from document would be as follows:
If changes are required, notify [Departmental Representative] [Engineer]
[Consultant] of these changes before they are made.

I'd like to essentially parse out the following options where I'd place them
on a form for the user to select the correct option:
Departmental Representative
Engineer
Consultant

So if the user selected Engineer then I'd like to update the original
document to read:
If changes are required, notify Engineer of these changes before they are
made.

At this point I'd run the macro again looking for the next set of brackets.

I know how to search for the start point with the following code:

Selection.Find.ClearFormatting
With Selection.Find
.Text = "\[*\]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute

This code will find [Departmental Representative] but from there I'm not
sure how to parse immediately after the close bracket to see if there is
another option (space followed by a open bracket.

Does anyone know how I would go about doing this?

Thanks
Dustin Wilson
 
D

Dustin Wilson

Russ

No this will not work because the template documents I use are developed by
the NMS (National Master Specifications) organization so I'm stuck with
using their templates. I'm trying to figure out how I can simplify editing
these documents as opposed to doing it manually all the time.

Your link is good for future reference if I have to create the templates
myself.

Regards
Dustin

Russ said:
Dustin,
Take a look at this page/site before going any further. Sometimes people
try
to reinvent a process that is already included in Word.
I'd like to write a macro that would search through a word document and
search for an instance of text enclosed with square brackets "[.....]"
and
parse out each option that immediately follows.

Example text from document would be as follows:
If changes are required, notify [Departmental Representative] [Engineer]
[Consultant] of these changes before they are made.

I'd like to essentially parse out the following options where I'd place
them
on a form for the user to select the correct option:
Departmental Representative
Engineer
Consultant

So if the user selected Engineer then I'd like to update the original
document to read:
If changes are required, notify Engineer of these changes before they are
made.

At this point I'd run the macro again looking for the next set of
brackets.

I know how to search for the start point with the following code:

Selection.Find.ClearFormatting
With Selection.Find
.Text = "\[*\]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute

This code will find [Departmental Representative] but from there I'm not
sure how to parse immediately after the close bracket to see if there is
another option (space followed by a open bracket.

Does anyone know how I would go about doing this?

Thanks
Dustin Wilson
 
H

Helmut Weber

Hi Dustin,

taking as testing material your first posting,
the following code highlights here and now all
sequences of strings in brackets seperated by a space.
Highlighting is just there to prove that those
sequences can be defined.
The highlighted text is ready for further processing.

There is for sure quite some redundancy in the code,
e.g. every select-method is there only for testing.

It is only for proving, that it can be done.

Refinement and further processing is up to you.

Sub Test8()
Dim rDcm As Range
Dim rTmp As Range
Dim Pos1 As Long
Dim Pos2 As Long
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "\[*\]"
.MatchWildcards = True
While .Execute
Set rTmp = rDcm.Duplicate
rTmp.Select
If rTmp.Characters.Last.Next = " " And _
rTmp.Characters.Last.Next.Next = "[" Then
Pos1 = rTmp.Start
End If
While rTmp.Characters.Last.Next = " " And _
rTmp.Characters.Last.Next.Next = "["
rTmp.Start = rTmp.End
rTmp.End = ActiveDocument.Range.End
rTmp.Select
With rTmp.Find
.Text = "]"
.MatchWildcards = False
.Execute
rTmp.Select
Pos2 = rTmp.End
End With
rTmp.Start = Pos1
Wend

If InStr(rTmp.Text, "] [") Then
rTmp.Select
rTmp.HighlightColorIndex = wdYellow
End If
rDcm.Start = rTmp.End
rDcm.End = ActiveDocument.Range.End
rDcm.Select
Wend
End With
End Sub

' -------------------

If there are typos in the text,
brackets scattered everywhere,
then nothing will work.

"If everything is possible, then nothing is possible."
Quoting my old professor Hans Günter Tillmann

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
D

Doug Robbins - Word MVP

The following macro will replace each group from which you want to select an
item with a dropdown formfield from which you can select the desired item:

Dim myrange As Range
Dim myoptions As Variant
Dim ffield As FormField

Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="] [", Forward:=True, MatchWildcards:=False,
Wrap:=wdFindStop) = True
Set myrange = Selection.Paragraphs(1).Range
myrange.start = myrange.start + InStr(myrange, "[")
myrange.End = myrange.start + InStrRev(myrange, "]") - 1
myoptions = Split(myrange, "] [")
myrange.start = myrange.start - 1
myrange.End = myrange.End + 1
Set ffield = ActiveDocument.FormFields.Add(Range:=myrange,
Type:=wdFieldFormDropDown)
With ffield
For i = LBound(myoptions) To UBound(myoptions)
.DropDown.ListEntries.Add myoptions(i)
Next i
.Range.InsertBefore " "
End With
Loop
End With
ActiveDocument.Protect wdAllowOnlyFormFields


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

Dustin Wilson said:
Russ

No this will not work because the template documents I use are developed
by the NMS (National Master Specifications) organization so I'm stuck with
using their templates. I'm trying to figure out how I can simplify
editing these documents as opposed to doing it manually all the time.

Your link is good for future reference if I have to create the templates
myself.

Regards
Dustin

Russ said:
Dustin,
Take a look at this page/site before going any further. Sometimes people
try
to reinvent a process that is already included in Word.
I'd like to write a macro that would search through a word document and
search for an instance of text enclosed with square brackets "[.....]"
and
parse out each option that immediately follows.

Example text from document would be as follows:
If changes are required, notify [Departmental Representative] [Engineer]
[Consultant] of these changes before they are made.

I'd like to essentially parse out the following options where I'd place
them
on a form for the user to select the correct option:
Departmental Representative
Engineer
Consultant

So if the user selected Engineer then I'd like to update the original
document to read:
If changes are required, notify Engineer of these changes before they
are
made.

At this point I'd run the macro again looking for the next set of
brackets.

I know how to search for the start point with the following code:

Selection.Find.ClearFormatting
With Selection.Find
.Text = "\[*\]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute

This code will find [Departmental Representative] but from there I'm not
sure how to parse immediately after the close bracket to see if there is
another option (space followed by a open bracket.

Does anyone know how I would go about doing this?

Thanks
Dustin Wilson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top