Word VBA: Find.Text ="##*##" only selects "#"

  • Thread starter Georgios Liakopoulos
  • Start date
G

Georgios Liakopoulos

Hello,
I am using VBA (in an MS Access procedure) to locate and read text parts
from a Word document. Text parts are like ##*## where * is a variable
digit number (goes from 37 to 6000 or more).
So what I want is to locate ##*##, read it and place it in an access
table, then replace the text with something else, lets say ##READ##.

Here is the VBA code:

******************************************************
With objWord.ActiveDocument.Content.Find
.ClearFormatting
.MatchWildcards = True
.Text = "##*##"
If .Execute = True Then
Debug.Print Selection.Text 'this is like reading the number
.. . .
******************************************************

Example of the text in the Word document:
Bla, bla, bla ##45## bla bla bla ##2843## bla, bla, . . . .

Running the code should at first select ##45##, print ##45## in the
Access Immediate window and so on. . .

The problem is that it only prints: # like it only selects the first
character from the ##45##.
Can anyone help with this?
I am grateful to anyone who will read my question
 
D

Doug Robbins - Word MVP on news.microsoft.com

Use .Text="##[0-9]{1,}##"

--
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, originally posted via msnews.microsoft.com
 
G

Georgios Liakopoulos

Doug said:
Use .Text="##[0-9]{1,}##"
Hi Doug,
The pattern was not a problem. It was the command activedocument.content
that was the problem. I copy from Tony Jollans who posted a relevant
message:

Your Find, using ActiveDocument.Content, will not change the Selection.
Either use Selection.Find (although, interestingly, VBA seems to work
differently from the UI), or reference .Parent.Text (or rearrange your
Withs to access it differently).

Thank you
 

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