JFerro
JFerro

Reputation: 3433

VBA Microsoft for MSword Macro using a wildcard including a string variable

word VBA programmers,

Problem: Working in MS word the user would select a string (one or several words), just asume "word1 word2" is selected. The macro would look in the whole document if there is such a string followed by references in parentheses i.e. "word1 word2 (234, 21)". From the point in which the user selected the text the references would be added. I have the code to get the selected text: Dim Sel As Selection Dim feature As String

Set Sel = Application.Selection
If Sel.Type <> wdSelectionIP Then
    MsgBox Sel.text
End If 
feature=sel.text

(feature being the text to look for followed by the references)

The wild card for the text into brackets would be (*) which means a parenthese followed by any change and followed by a close parenthese.

my problem is that the following code does not work:

With ActiveDocument.Content.Find
 .ClearFormatting
 .text = feature & \(*\)
  With .Replacement
    .ClearFormatting
    .text = feature & \(*\)
    .Font.Color = wdColorRed
  End with 
 .Forward = True
 .Wrap = wdFindContinue
 .Format = True
 .MatchCase = False
 .MatchWholeWord = False
 .MatchWildcards = true
 .MatchSoundsLike = False
 .MatchAllWordForms = False
 .Execute Replace:=wdReplaceAll
End With

I can not even run it because the line .text = feature & (*)

gets red and its not understood.

the problem here is how to combine the content of a variable (text selected by the user) with a wild card that would give me instances of that selected text followed by parentheses including numeral references.

Thanks a lot.

Upvotes: 0

Views: 1566

Answers (3)

JFerro
JFerro

Reputation: 3433

xidgel,

Thanks. This is very powerfull. It works. I have the impression Wildcards are like another language. So difficult to start using them.

In any case I realise I need yet on top of that a way to say to MS-WORD that from a particular point in text it is neccesary to substitute the FEATURES NOT BEING FOLLOWED BY REFERENCES IN PARENTHESES by the references in parentheses, in other words, to add the references to the features which dont have them, and only from the text that I selected on.

Xidgel codes works perfectly in the sense that really finds the features followed by the references.

when looking for the text with:

.text = feature & "\((*)\)"

I need to load in a variable the string corresponding to

"\((*)\)"

Just call this variable dim REF as String in order to be able to substitute then

.text = feature & "[A-z]"

=feature followed by a character i.e NOT followed by PARENTHESES

by

feature & Ref

Upvotes: 0

xidgel
xidgel

Reputation: 3145

For the search text use:

.text = feature & "\((*)\)"

Here, \( matches the opening paren; (*) does a "group" match (allows you to use put what the wildcard match in the replacement text); and \) matches the closing paren.

For the replace text use:

.text = feature & "(\1)"

Here, ( matches the opening paren; \1 returns what the first match "group" (what the wildcard picked up); and ) matches the closing paren.

Hope that helps.

Upvotes: 2

SierraOscar
SierraOscar

Reputation: 17627

.text = feature & "(*)"

You're trying to concatenate a string, you need to put that string in parentheses - no need to escape the brackets here as it isn't a regular expression.

Upvotes: 0

Related Questions