Find number and insert text programmatically

R

Raul

I need to find numbers in the first few characters of a paragraph and then
insert some text before the number. I have modified some code that someone
from this newsgroup was kind enough to create in response to an earlier post.


The code listed below will find the numbers and insert the text, but it
will insert the text before any numbers in the paragraphs. I need to to limit
the search to a specifed number of characters in each paragraph and ignore
any numbers that are outside that range.

Thanks in advance,
Raul

Sub FindNumberAndInsertText()

Dim i As Long
Dim rngSource As Range
Dim txt2insert As String
Dim docSource As Document

Set docSource = ActiveDocument

txt2insert = "Question "

i = 0
Do
Set rngSource = docSource.Range
i = i + 1
With rngSource.Find
.Text = CStr(i) & "."
If .Execute Then
With rngSource
.MoveStart Unit:=wdParagraph, Count:=0
.InsertBefore txt2insert
End With
Else
Exit Do
End If
End With
Loop

Set docSource = Nothing
End Sub
 
J

Jezebel

You appear to be trying to insert "Question " at the start of each paragraph
that begins with a number. The better way to do this (in general) is to use
a number style, with "Question " as part of the number format.

However, for your immediate task, you don't need a macro. Use Find and
Replace directly. With 'Use Wildcards' checked --

Search for: ^013([0-9]{1,}.)
Replace with: ^013Question \1
 
R

Raul

This works beautifully if there are no blank spaces before the number. In my
case there may be one or two leading blanks. I could use multiple find and
replace statements to handle each of the different cases (i.e., with one
blank space and two blank spaces).

Can you show me how to modify the "Seach for:" statement to look for a
number with one blank space before the number and a period after the number?

Thanks,
Raul

Jezebel said:
You appear to be trying to insert "Question " at the start of each paragraph
that begins with a number. The better way to do this (in general) is to use
a number style, with "Question " as part of the number format.

However, for your immediate task, you don't need a macro. Use Find and
Replace directly. With 'Use Wildcards' checked --

Search for: ^013([0-9]{1,}.)
Replace with: ^013Question \1




Raul said:
I need to find numbers in the first few characters of a paragraph and then
insert some text before the number. I have modified some code that
someone
from this newsgroup was kind enough to create in response to an earlier
post.


The code listed below will find the numbers and insert the text, but it
will insert the text before any numbers in the paragraphs. I need to to
limit
the search to a specifed number of characters in each paragraph and
ignore
any numbers that are outside that range.

Thanks in advance,
Raul

Sub FindNumberAndInsertText()

Dim i As Long
Dim rngSource As Range
Dim txt2insert As String
Dim docSource As Document

Set docSource = ActiveDocument

txt2insert = "Question "

i = 0
Do
Set rngSource = docSource.Range
i = i + 1
With rngSource.Find
.Text = CStr(i) & "."
If .Execute Then
With rngSource
.MoveStart Unit:=wdParagraph, Count:=0
.InsertBefore txt2insert
End With
Else
Exit Do
End If
End With
Loop

Set docSource = Nothing
End Sub
 
J

Jezebel

It already looks for the period after the number. If you want to look for,
and eliminate, the preceding spaces, use --

Search for: ^013 @([0-9]{1,}.)

This requires that there be at least one space, so you'd have to do zero
spaces separately. I'm sure there's a way to look for zero or more spaces,
but off-hand I can't think of it.




Raul said:
This works beautifully if there are no blank spaces before the number. In
my
case there may be one or two leading blanks. I could use multiple find
and
replace statements to handle each of the different cases (i.e., with one
blank space and two blank spaces).

Can you show me how to modify the "Seach for:" statement to look for a
number with one blank space before the number and a period after the
number?

Thanks,
Raul

Jezebel said:
You appear to be trying to insert "Question " at the start of each
paragraph
that begins with a number. The better way to do this (in general) is to
use
a number style, with "Question " as part of the number format.

However, for your immediate task, you don't need a macro. Use Find and
Replace directly. With 'Use Wildcards' checked --

Search for: ^013([0-9]{1,}.)
Replace with: ^013Question \1




Raul said:
I need to find numbers in the first few characters of a paragraph and
then
insert some text before the number. I have modified some code that
someone
from this newsgroup was kind enough to create in response to an earlier
post.


The code listed below will find the numbers and insert the text, but
it
will insert the text before any numbers in the paragraphs. I need to to
limit
the search to a specifed number of characters in each paragraph and
ignore
any numbers that are outside that range.

Thanks in advance,
Raul

Sub FindNumberAndInsertText()

Dim i As Long
Dim rngSource As Range
Dim txt2insert As String
Dim docSource As Document

Set docSource = ActiveDocument

txt2insert = "Question "

i = 0
Do
Set rngSource = docSource.Range
i = i + 1
With rngSource.Find
.Text = CStr(i) & "."
If .Execute Then
With rngSource
.MoveStart Unit:=wdParagraph, Count:=0
.InsertBefore txt2insert
End With
Else
Exit Do
End If
End With
Loop

Set docSource = Nothing
End Sub
 
R

Raul

This worked. I'll use two different find and replace statements to cover the
two cases.

Thanks a bunch,
Raul

Jezebel said:
It already looks for the period after the number. If you want to look for,
and eliminate, the preceding spaces, use --

Search for: ^013 @([0-9]{1,}.)

This requires that there be at least one space, so you'd have to do zero
spaces separately. I'm sure there's a way to look for zero or more spaces,
but off-hand I can't think of it.




Raul said:
This works beautifully if there are no blank spaces before the number. In
my
case there may be one or two leading blanks. I could use multiple find
and
replace statements to handle each of the different cases (i.e., with one
blank space and two blank spaces).

Can you show me how to modify the "Seach for:" statement to look for a
number with one blank space before the number and a period after the
number?

Thanks,
Raul

Jezebel said:
You appear to be trying to insert "Question " at the start of each
paragraph
that begins with a number. The better way to do this (in general) is to
use
a number style, with "Question " as part of the number format.

However, for your immediate task, you don't need a macro. Use Find and
Replace directly. With 'Use Wildcards' checked --

Search for: ^013([0-9]{1,}.)
Replace with: ^013Question \1




I need to find numbers in the first few characters of a paragraph and
then
insert some text before the number. I have modified some code that
someone
from this newsgroup was kind enough to create in response to an earlier
post.


The code listed below will find the numbers and insert the text, but
it
will insert the text before any numbers in the paragraphs. I need to to
limit
the search to a specifed number of characters in each paragraph and
ignore
any numbers that are outside that range.

Thanks in advance,
Raul

Sub FindNumberAndInsertText()

Dim i As Long
Dim rngSource As Range
Dim txt2insert As String
Dim docSource As Document

Set docSource = ActiveDocument

txt2insert = "Question "

i = 0
Do
Set rngSource = docSource.Range
i = i + 1
With rngSource.Find
.Text = CStr(i) & "."
If .Execute Then
With rngSource
.MoveStart Unit:=wdParagraph, Count:=0
.InsertBefore txt2insert
End With
Else
Exit Do
End If
End With
Loop

Set docSource = Nothing
End Sub
 

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