Search through a document and assign bookmarks

T

Tim

I would like to search through an entire word document (text, tables, etc)
and assign bookmarks to every number in it. These numbers could have
decimals, $ signs in front, etc. Is there a quick and easy way to do this?
Thank you.
 
O

old man

Hi,

This works except you have to work on the wildcard expression
"<[0-9][0-9.]{1,}>" so it matches the different type of numbers you listed:




Sub ss2()


Dim insbkmrks

Dim j As Integer
Dim r1 As Range
Dim newname As String

j = 0

Selection.HomeKey unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "<[0-9][0-9.]{1,}>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
End With
Do While Selection.Find.Execute = True
j = j + 1
newname = "bkNum" & Trim(Str(j))
ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:=newname
Loop

End Sub

old man
 
T

Tim

Thanks a lot, and I will have to work on the wildcard expression. Do you
know of any comprehensive reference on wildcards?

Thanks again,

Tim

old man said:
Hi,

This works except you have to work on the wildcard expression
"<[0-9][0-9.]{1,}>" so it matches the different type of numbers you listed:




Sub ss2()


Dim insbkmrks

Dim j As Integer
Dim r1 As Range
Dim newname As String

j = 0

Selection.HomeKey unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "<[0-9][0-9.]{1,}>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
End With
Do While Selection.Find.Execute = True
j = j + 1
newname = "bkNum" & Trim(Str(j))
ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:=newname
Loop

End Sub

old man

Tim said:
I would like to search through an entire word document (text, tables, etc)
and assign bookmarks to every number in it. These numbers could have
decimals, $ signs in front, etc. Is there a quick and easy way to do this?
Thank you.
 
G

Graham Mayor

No :)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
T

Tim

Hi Graham,

I was wondering if it would be possible to use a single wildcard statement
to find all the number "types" in the document below. I would not like to
include the $ signs nor the period that comes at the end of the
sentences/numbers in the table. Iif there is a more simple way to accomplish
this task, that would be great too. Furthermore, if it is too much of an
inconvenience altogether, don't waste your time.

Thanks again everybody,

Tim

I have finished 5 years of university, and have 2.7 more to go and I am
currently writing 34,67 tests and then 2,345.00 tests.
I have 2.
I have 2.7.
I have 34,67.
I have 2,345.00.
Hi, I have $4 in the bank, and $45.67 in my wallet and $54,78 in my piggy
bank, but I wish I had $46,765.00 in all.
Each test costs roughly $4.
Each test costs roughly $45.67.
Each test costs roughly $54,78.
Each test costs roughly $46,765.00.
 
G

Graham Mayor

I can't immediately see a single solution that would account for them all
[0-9,.]{2,} is the closest, but that adds the period at the ends.
You could perform several passes to really tie it down.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
T

Tim

I will take the several pass approach to get the result.

Thank you very much,

Tim

Graham Mayor said:
I can't immediately see a single solution that would account for them all
[0-9,.]{2,} is the closest, but that adds the period at the ends.
You could perform several passes to really tie it down.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Hi Graham,

I was wondering if it would be possible to use a single wildcard
statement to find all the number "types" in the document below. I
would not like to include the $ signs nor the period that comes at
the end of the sentences/numbers in the table. If there is a more
simple way to accomplish this task, that would be great too.
Furthermore, if it is too much of an inconvenience altogether, don't
waste your time.

Thanks again everybody,

Tim

I have finished 5 years of university, and have 2.7 more to go and I
am currently writing 34,67 tests and then 2,345.00 tests.
I have 2.
I have 2.7.
I have 34,67.
I have 2,345.00.
Hi, I have $4 in the bank, and $45.67 in my wallet and $54,78 in my
piggy bank, but I wish I had $46,765.00 in all.
Each test costs roughly $4.
Each test costs roughly $45.67.
Each test costs roughly $54,78.
Each test costs roughly $46,765.00.
 
T

Tim

Sorry to keep bugging you, but as a follow up to your last incredibly useful
comment, how would I use wildcards to just find the "integers" in the below
text. Here by integers, I just mean all numbers that do not have a decimal
or comma in them and are not followed by a period

Thank you very much,

Tim

Graham Mayor said:
I can't immediately see a single solution that would account for them all
[0-9,.]{2,} is the closest, but that adds the period at the ends.
You could perform several passes to really tie it down.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Hi Graham,

I was wondering if it would be possible to use a single wildcard
statement to find all the number "types" in the document below. I
would not like to include the $ signs nor the period that comes at
the end of the sentences/numbers in the table. If there is a more
simple way to accomplish this task, that would be great too.
Furthermore, if it is too much of an inconvenience altogether, don't
waste your time.

Thanks again everybody,

Tim

I have finished 5 years of university, and have 2.7 more to go and I
am currently writing 34,67 tests and then 2,345.00 tests.
I have 2.
I have 2.7.
I have 34,67.
I have 2,345.00.
Hi, I have $4 in the bank, and $45.67 in my wallet and $54,78 in my
piggy bank, but I wish I had $46,765.00 in all.
Each test costs roughly $4.
Each test costs roughly $45.67.
Each test costs roughly $54,78.
Each test costs roughly $46,765.00.
 
R

Russ

Tim,
You're apparently not doing the homework assignment Helmut gave you at
Graham's site.
[0-9]{1,}
Sorry to keep bugging you, but as a follow up to your last incredibly useful
comment, how would I use wildcards to just find the "integers" in the below
text. Here by integers, I just mean all numbers that do not have a decimal
or comma in them and are not followed by a period

Thank you very much,

Tim

Graham Mayor said:
I can't immediately see a single solution that would account for them all
[0-9,.]{2,} is the closest, but that adds the period at the ends.
You could perform several passes to really tie it down.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Hi Graham,

I was wondering if it would be possible to use a single wildcard
statement to find all the number "types" in the document below. I
would not like to include the $ signs nor the period that comes at
the end of the sentences/numbers in the table. If there is a more
simple way to accomplish this task, that would be great too.
Furthermore, if it is too much of an inconvenience altogether, don't
waste your time.

Thanks again everybody,

Tim

I have finished 5 years of university, and have 2.7 more to go and I
am currently writing 34,67 tests and then 2,345.00 tests.
I have 2.
I have 2.7.
I have 34,67.
I have 2,345.00.
Hi, I have $4 in the bank, and $45.67 in my wallet and $54,78 in my
piggy bank, but I wish I had $46,765.00 in all.
Each test costs roughly $4.
Each test costs roughly $45.67.
Each test costs roughly $54,78.
Each test costs roughly $46,765.00.


:

No :)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Helmut Weber wrote:
Hi Tim,

hi, Graham,

I hope you don't mind me possibly being faster. ;-)

http://www.gmayor.com/replace_using_wildcards.htm
 
H

Helmut Weber

Hi Tim,
I have ... $45.67 in my wallet and $54,78 in my piggy bank
Each test costs roughly $4.
Each test costs roughly $45.67.
Each test costs roughly $54,78.
Each test costs roughly $46,765.00.

Conditions!!!
Period "." should be the decimal separator
Comma "," should be the thousands separator.
If there are decimal digits, its always 2 digits. !!!

Then,
if a period is followed by 3 digits, its as thousands separator.
Change it to an otherwise not used character, like "þ".

if a period is followed by 2 digits, its as decimal separator.
Change it to an otherwise not used character, like "ý".

if a comma is followed by 3 digits, its as thousands separator.
Change it to an otherwise not used character, like "þ".

if a comma is followed by 2 digits, its as decimal separator.
Change it to an otherwise not used character, like "ý".

Change "þ" to ","
change "ý" to "."

If there are typos than nothing works.
If there are numbers like 1.000,123455, then it would be really hard.
--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Russ

Tim,
Sorry, I was a little too quick on the draw.
Try [!$]([0-9]{1,})[!.] in find box
And if you are going to do something with what you found:
\1
in replace box


Tim,
You're apparently not doing the homework assignment Helmut gave you at
Graham's site.
[0-9]{1,}
Sorry to keep bugging you, but as a follow up to your last incredibly useful
comment, how would I use wildcards to just find the "integers" in the below
text. Here by integers, I just mean all numbers that do not have a decimal
or comma in them and are not followed by a period

Thank you very much,

Tim

Graham Mayor said:
I can't immediately see a single solution that would account for them all
[0-9,.]{2,} is the closest, but that adds the period at the ends.
You could perform several passes to really tie it down.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Tim wrote:
Hi Graham,

I was wondering if it would be possible to use a single wildcard
statement to find all the number "types" in the document below. I
would not like to include the $ signs nor the period that comes at
the end of the sentences/numbers in the table. If there is a more
simple way to accomplish this task, that would be great too.
Furthermore, if it is too much of an inconvenience altogether, don't
waste your time.

Thanks again everybody,

Tim

I have finished 5 years of university, and have 2.7 more to go and I
am currently writing 34,67 tests and then 2,345.00 tests.
I have 2.
I have 2.7.
I have 34,67.
I have 2,345.00.
Hi, I have $4 in the bank, and $45.67 in my wallet and $54,78 in my
piggy bank, but I wish I had $46,765.00 in all.
Each test costs roughly $4.
Each test costs roughly $45.67.
Each test costs roughly $54,78.
Each test costs roughly $46,765.00.


:

No :)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Helmut Weber wrote:
Hi Tim,

hi, Graham,

I hope you don't mind me possibly being faster. ;-)

http://www.gmayor.com/replace_using_wildcards.htm
 
R

Russ

Tim,
Darn, this *is* harder than I remembered.
[!$]([0-9]{1,})[!.]
Will also select the character before and after the integer, It won't be a $
or .

But if you want to do something with the number, only, you have to use this
loop in your find and replace section.

With Selection.Find
'...
.MatchWildcards = True
.Text = "[!$]([0-9]{1,})[!.]"
Do While .Execute = True
'contract range or selection in found text in one of many ways
Selection.SetRange Start:=Selection.Range.Start + 1, _
End:=Selection.Range.End - 1
' Do stuff
Loop
End With
Tim,
Sorry, I was a little too quick on the draw.
Try [!$]([0-9]{1,})[!.] in find box
And if you are going to do something with what you found:
\1
in replace box


Tim,
You're apparently not doing the homework assignment Helmut gave you at
Graham's site.
[0-9]{1,}
Sorry to keep bugging you, but as a follow up to your last incredibly useful
comment, how would I use wildcards to just find the "integers" in the below
text. Here by integers, I just mean all numbers that do not have a decimal
or comma in them and are not followed by a period

Thank you very much,

Tim

:

I can't immediately see a single solution that would account for them all
[0-9,.]{2,} is the closest, but that adds the period at the ends.
You could perform several passes to really tie it down.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Tim wrote:
Hi Graham,

I was wondering if it would be possible to use a single wildcard
statement to find all the number "types" in the document below. I
would not like to include the $ signs nor the period that comes at
the end of the sentences/numbers in the table. If there is a more
simple way to accomplish this task, that would be great too.
Furthermore, if it is too much of an inconvenience altogether, don't
waste your time.

Thanks again everybody,

Tim

I have finished 5 years of university, and have 2.7 more to go and I
am currently writing 34,67 tests and then 2,345.00 tests.
I have 2.
I have 2.7.
I have 34,67.
I have 2,345.00.
Hi, I have $4 in the bank, and $45.67 in my wallet and $54,78 in my
piggy bank, but I wish I had $46,765.00 in all.
Each test costs roughly $4.
Each test costs roughly $45.67.
Each test costs roughly $54,78.
Each test costs roughly $46,765.00.


:

No :)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Helmut Weber wrote:
Hi Tim,

hi, Graham,

I hope you don't mind me possibly being faster. ;-)

http://www.gmayor.com/replace_using_wildcards.htm
 
R

Russ

Tim,
Also add the comma to the trailing-not-to-be-found list.
[!$]([0-9]{1,})[!.,]
But then it would not find these three numbers in test phrase:
3, 4, and 5.

So try instead
[!$][0-9]{1,}
You might do a test(s) within the aforementioned loop like:
If Not Selection.Range.Next.Next.Text Like "#" Then
Selection.SetRange Start:=Selection.Range.Start + 1, _
End:=Selection.Range.End
' Do stuff
End If
Tim,
Darn, this *is* harder than I remembered.
[!$]([0-9]{1,})[!.]
Will also select the character before and after the integer, It won't be a $
or .

But if you want to do something with the number, only, you have to use this
loop in your find and replace section.

With Selection.Find
'...
.MatchWildcards = True
.Text = "[!$]([0-9]{1,})[!.]"
Do While .Execute = True
'contract range or selection in found text in one of many ways
Selection.SetRange Start:=Selection.Range.Start + 1, _
End:=Selection.Range.End - 1
' Do stuff
Loop
End With
Tim,
Sorry, I was a little too quick on the draw.
Try [!$]([0-9]{1,})[!.] in find box
And if you are going to do something with what you found:
\1
in replace box


Tim,
You're apparently not doing the homework assignment Helmut gave you at
Graham's site.
[0-9]{1,}

Sorry to keep bugging you, but as a follow up to your last incredibly
useful
comment, how would I use wildcards to just find the "integers" in the below
text. Here by integers, I just mean all numbers that do not have a decimal
or comma in them and are not followed by a period

Thank you very much,

Tim

:

I can't immediately see a single solution that would account for them all
[0-9,.]{2,} is the closest, but that adds the period at the ends.
You could perform several passes to really tie it down.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Tim wrote:
Hi Graham,

I was wondering if it would be possible to use a single wildcard
statement to find all the number "types" in the document below. I
would not like to include the $ signs nor the period that comes at
the end of the sentences/numbers in the table. If there is a more
simple way to accomplish this task, that would be great too.
Furthermore, if it is too much of an inconvenience altogether, don't
waste your time.

Thanks again everybody,

Tim

I have finished 5 years of university, and have 2.7 more to go and I
am currently writing 34,67 tests and then 2,345.00 tests.
I have 2.
I have 2.7.
I have 34,67.
I have 2,345.00.
Hi, I have $4 in the bank, and $45.67 in my wallet and $54,78 in my
piggy bank, but I wish I had $46,765.00 in all.
Each test costs roughly $4.
Each test costs roughly $45.67.
Each test costs roughly $54,78.
Each test costs roughly $46,765.00.


:

No :)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Helmut Weber wrote:
Hi Tim,

hi, Graham,

I hope you don't mind me possibly being faster. ;-)

http://www.gmayor.com/replace_using_wildcards.htm
 
O

old man

Hi Tim,

Word regular expressions is a very limited one and using VSTO (Visual Studio
Tools for Office) or third party addins allow one to create a 'real' regex
expression that would do exactly what you want (which is to identify every
number in a document.) The only way to do it is to run the expression I
suggested and then you have to move left (to see if the character to the left
is a dollar sign and right three characters to see if there is a decimal
followed by two digits.

The expression suggested by Tim (while being very elegant) does not work any
better then the one I suggested.

When my program is run it finds every number, (skips dollar signs )and then
refinds numbers (including the decimal point) after the decimal point. It
does the same for commas.

You can also use the VBScript.RegExp in Word VBA which is (I don't want to
get flamed on this please) a fine regex parser. To see more on this see:

http://www.regular-expressions.info/vbscript.html

Finally you have discovered another MS Word black hole. The best way to
avoid getting into this is to do use a hack (something like I suggested where
you move right and left) and get on with your work.

old man
 
R

Russ

Tim,
More testing within the loop would be need to handle an integer divided by
an integer, etc.
1/3
5x6
Tim,
Also add the comma to the trailing-not-to-be-found list.
[!$]([0-9]{1,})[!.,]
But then it would not find these three numbers in test phrase:
3, 4, and 5.

So try instead
[!$][0-9]{1,}
You might do a test(s) within the aforementioned loop like:
If Not Selection.Range.Next.Next.Text Like "#" Then
Selection.SetRange Start:=Selection.Range.Start + 1, _
End:=Selection.Range.End
' Do stuff
End If
Tim,
Darn, this *is* harder than I remembered.
[!$]([0-9]{1,})[!.]
Will also select the character before and after the integer, It won't be a $
or .

But if you want to do something with the number, only, you have to use this
loop in your find and replace section.

With Selection.Find
'...
.MatchWildcards = True
.Text = "[!$]([0-9]{1,})[!.]"
Do While .Execute = True
'contract range or selection in found text in one of many ways
Selection.SetRange Start:=Selection.Range.Start + 1, _
End:=Selection.Range.End - 1
' Do stuff
Loop
End With
Tim,
Sorry, I was a little too quick on the draw.
Try [!$]([0-9]{1,})[!.] in find box
And if you are going to do something with what you found:
\1
in replace box



Tim,
You're apparently not doing the homework assignment Helmut gave you at
Graham's site.
[0-9]{1,}

Sorry to keep bugging you, but as a follow up to your last incredibly
useful
comment, how would I use wildcards to just find the "integers" in the
below
text. Here by integers, I just mean all numbers that do not have a
decimal
or comma in them and are not followed by a period

Thank you very much,

Tim

:

I can't immediately see a single solution that would account for them all
[0-9,.]{2,} is the closest, but that adds the period at the ends.
You could perform several passes to really tie it down.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Tim wrote:
Hi Graham,

I was wondering if it would be possible to use a single wildcard
statement to find all the number "types" in the document below. I
would not like to include the $ signs nor the period that comes at
the end of the sentences/numbers in the table. If there is a more
simple way to accomplish this task, that would be great too.
Furthermore, if it is too much of an inconvenience altogether, don't
waste your time.

Thanks again everybody,

Tim

I have finished 5 years of university, and have 2.7 more to go and I
am currently writing 34,67 tests and then 2,345.00 tests.
I have 2.
I have 2.7.
I have 34,67.
I have 2,345.00.
Hi, I have $4 in the bank, and $45.67 in my wallet and $54,78 in my
piggy bank, but I wish I had $46,765.00 in all.
Each test costs roughly $4.
Each test costs roughly $45.67.
Each test costs roughly $54,78.
Each test costs roughly $46,765.00.


:

No :)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Helmut Weber wrote:
Hi Tim,

hi, Graham,

I hope you don't mind me possibly being faster. ;-)

http://www.gmayor.com/replace_using_wildcards.htm
 
T

Tim

Hello Everyone,

Thank you all for your valuable input. I have decided that the easiest way
for me to proceed is to start from scratch, going through each word in a
document, checking whether or not it is numeric, then imposing a bunch of
conditions to get the proper ranges. I seem to have a working model (thanks
to a very dear friend), and now only need to address issues like numbers that
may have letters following them, such as 95.67kg or something else along
those lines.

Thank you all very much,

Tim
 
R

Russ

Tim,
Reply in line below.
Hello Everyone,

Thank you all for your valuable input. I have decided that the easiest way
for me to proceed is to start from scratch, going through each word in a
document, checking whether or not it is numeric, then imposing a bunch of
conditions to get the proper ranges.
Yes that method was always available, but it is s-l-o-w for large documents.
You can set up a chain of find and replace patterns that would probably be
quicker, especially if you can use .Execute Replace:=wdReplaceAll
Remember too, that if you chain them together within large With...End With
..find statements you can leave out a lot of redundant lines that don't need
to be repeated or are set as default. Such as this snippet of code that
makes certain words hidden.
Set oRange = ActiveDocument.Range(0, 0)
With oRange.Find
.MatchWholeWord = True
.MatchCase = False
.Replacement.Font.hidden = True
.Replacement.Text = "^&"
.Text = "a"
.Execute Replace:=wdReplaceAll
.Text = "is"
.Execute Replace:=wdReplaceAll
.Text = "the"
.Execute Replace:=wdReplaceAll
.Text = "that"
.Execute Replace:=wdReplaceAll
.Text = "an"
.Execute Replace:=wdReplaceAll
End With

It all depends on the number of words in your document to where you reach
the 'law of diminishing returns'.
I seem to have a working model (thanks
to a very dear friend), and now only need to address issues like numbers that
may have letters following them, such as 95.67kg or something else along
those lines.
Your code is going to think that 67kg is a word and that it is not numeric
when it tests.
 
T

Tim

Russ,

The documents with which I am working are not too large, so I can probably
stick with my approach. However, using with statements would indeed be the
betterway to go. I'm in the process of coming up with a solution to the 67kg
problem, too.

Thanks again,

Tim
 

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