Word VBA does no recognise the star (anything) symbol?

CaptainCsaba

Board Regular
Joined
Dec 8, 2017
Messages
78
Hi!
This actually appeared in Word VBA. I have a code that changed text tot other text. The problem is the text is almost always the same, there is one word inside it that always differs so I tried to use the * symbol and it does not work.

Code:
Sub ChangeText()

With Selection.Find
.ClearFormatting
.Text = "Mary had two little *^p in her basket"
.Replacement.ClearFormatting
.Replacement.Text = "Johnny had three little *^p in his basket"
.Execute Replace:=wdReplaceAll, Forward:=True, _
Wrap:=wdFindContinue
End With


End Sub

(^p is a paragraph end symbol)

If i put something like "apples" in the place of * it works fine but like this it does not. No matter what is inside the place of "*" it should work and change the beginning of the sentence. Even Microsoft says this on their official site:

"For example, a midsummer * dream will return results that contain the exact phrase, a midsummer night’s dream."

Does anybody has an idea what I should use instead of * to indicate to VBA that anything could be there?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
You cannot use things like ^P when using wildcards
 
Upvote 0
This actually appeared in Word VBA. I have a code that changed text tot other text. The problem is the text is almost always the same, there is one word inside it that always differs so I tried to use the * symbol and it does not work.
Try:
Code:
Sub ChangeText()
With Selection.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Text = "Mary had two little (*^13) in her basket"
  .Replacement.Text = "Johnny had three little \1 in his basket"
  .Forward = True
  .Wrap = wdFindContinue
  .MatchWildcards = True
  .Execute Replace:=wdReplaceAll
End With
End Sub
You cannot use things like ^P when using wildcards
Not entirely true. One can't use ^p as a Find expression, but it can be used as a Replace expression. Numerous other special 'Find' expressions and 'Replace' expressions also exist for wildcard Find/Replace operations.
 
Upvote 0
Try:
Code:
Sub ChangeText()
With Selection.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Text = "Mary had two little (*^13) in her basket"
  .Replacement.Text = "Johnny had three little \1 in his basket"
  .Forward = True
  .Wrap = wdFindContinue
  .MatchWildcards = True
  .Execute Replace:=wdReplaceAll
End With
End Sub

Not entirely true. One can't use ^p as a Find expression, but it can be used as a Replace expression. Numerous other special 'Find' expressions and 'Replace' expressions also exist for wildcard Find/Replace operations.

Hey!

I tried using it but did not work unfortunately. It runs without an error but noting happens.
 
Upvote 0
The code will work just fine for the content you described in post 1. Do note that wildcard finds are case-sensitive and, of course, if your find text has non-breaking spaces, tabs, etc., you have to provide for that in the code, too.
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top