VBA help to populate for every sheet in word document

D

DavidRocastle

Hi i currently have a word document that contains 60 pages. In this i have on
every sheet a label called "code:" where the data is inputted.

Page1.....
code:
Page2.....
code:

This is not part of a header. I would like to run a macro that pops up
looking for the code to be entered and will populate the number that is enter
in the pop up after "code:" in every page on the document.

How could i do this?
 
D

Doug Robbins - Word MVP

Use the following code

Dim num As String
num = InputBox("Enter the number that is to be inserted.")
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="Code:", ReplaceWith:="Code: " & num,
Forward:=True, Wrap:=wdFindStop) = True
Selection.Collapse wdCollapseEnd
Loop
End With

--
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
 
D

DavidRocastle

Thanks that Doug

I am getting an error on this part of the code saying Do While

Do While
.Execute(findText:="Code:", ReplaceWith:="Code: " & num,
Forward:=True, Wrap:=wdFindStop) = True

Syntax Error,

Sorry about this, it is just i am not a VBA programmer, am i missing
something from this
 
J

Jean-Guy Marcil

DavidRocastle was telling us:
DavidRocastle nous racontait que :
Thanks that Doug

I am getting an error on this part of the code saying Do While

Do While
.Execute(findText:="Code:", ReplaceWith:="Code: " & num,
Forward:=True, Wrap:=wdFindStop) = True

Syntax Error,

Sorry about this, it is just i am not a VBA programmer, am i missing
something from this

In the VBA editor code pane, make sure that the following is actually on one
line.

..Execute(findText:="Code:", ReplaceWith:="Code: " & num, Forward:=True,
Wrap:=wdFindStop) = True

Or, use tis:

..Execute(findText:="Code:", ReplaceWith:="Code: " & num, _
Forward:=True, Wrap:=wdFindStop) = True

Make sure there is a space before the underscore at the end of the first
line. The underscore is a line continuing marker.

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
D

DavidRocastle

Thanks for your assistance.

i do have a problem with this piece of code, Jus say i enter it the first
time, i for example enter 1123
i get
Page1:
Code: 1123
Page2:
Code: 1123
........
and so on.

After i have saved that i want to run it again. and i enter 98701
i get the following
Page1:
Code: 98701 1123
Page2:
Code: 98701 1123
.....
and so on

I cannot seem to get rid of the previous data. Would you have any ideas?

Thanks again
 
J

Jean-Guy Marcil

DavidRocastle was telling us:
DavidRocastle nous racontait que :
Thanks for your assistance.

i do have a problem with this piece of code, Jus say i enter it the
first time, i for example enter 1123
i get
Page1:
Code: 1123
Page2:
Code: 1123
.......
and so on.

After i have saved that i want to run it again. and i enter 98701
i get the following
Page1:
Code: 98701 1123
Page2:
Code: 98701 1123
....
and so on

I cannot seem to get rid of the previous data. Would you have any
ideas?

You can modify Doug's original code like this:

Dim numBefore As String
Dim numAfter As String
numBefore = InputBox("Enter the number that is to be replaced.")
numAfter = InputBox("Enter the number that is to be inserted.")
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="Code: " & numAfter, ReplaceWith:="Code: " &
numAfter, _
Forward:=True, Wrap:=wdFindStop) = True
Selection.Collapse wdCollapseEnd
Loop
End With



--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
D

DavidRocastle

I was more looking for something that could offset and delete the characters
to the right Code:

Would you have any ideas? I would like to jus have a single text box to
enter rather than a find and replace.
 
G

Graham Mayor

In that case

Dim num As String
num = InputBox("Enter the number that is to be inserted.")
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="([Cc]ode)[: 0-9]{1,}", ReplaceWith:="\1: "
& num, _
Forward:=True, MatchWildcards:=True, Wrap:=wdFindStop) = True
Selection.Collapse wdCollapseEnd
Loop
End With

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
D

DavidRocastle

Tansk for that Graham

Graham Mayor said:
In that case

Dim num As String
num = InputBox("Enter the number that is to be inserted.")
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="([Cc]ode)[: 0-9]{1,}", ReplaceWith:="\1: "
& num, _
Forward:=True, MatchWildcards:=True, Wrap:=wdFindStop) = True
Selection.Collapse wdCollapseEnd
Loop
End With

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
I was more looking for something that could offset and delete the
characters to the right Code:

Would you have any ideas? I would like to jus have a single text box
to enter rather than a find and replace.
 
D

Doug Robbins - Word MVP

Save it with a different name and run the code again on the original
document.

--
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
 
D

DavidRocastle

Graham i just have one last thing to tweak.

Where the code digits themselves are entered, i have a great textfield where
the values are to be entered

Is there a way i can Find: Code:eek:ld code (inside grey textfield)
Replace with Code:new code (inside grey textfield)



Graham Mayor said:
In that case

Dim num As String
num = InputBox("Enter the number that is to be inserted.")
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="([Cc]ode)[: 0-9]{1,}", ReplaceWith:="\1: "
& num, _
Forward:=True, MatchWildcards:=True, Wrap:=wdFindStop) = True
Selection.Collapse wdCollapseEnd
Loop
End With

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
I was more looking for something that could offset and delete the
characters to the right Code:

Would you have any ideas? I would like to jus have a single text box
to enter rather than a find and replace.
 
G

Graham Mayor

What sort of text field (press ALT+F9 and let us know what it says)?

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham i just have one last thing to tweak.

Where the code digits themselves are entered, i have a great
textfield where the values are to be entered

Is there a way i can Find: Code:eek:ld code (inside grey textfield)
Replace with Code:new code (inside grey textfield)



Graham Mayor said:
In that case

Dim num As String
num = InputBox("Enter the number that is to be inserted.")
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="([Cc]ode)[: 0-9]{1,}",
ReplaceWith:="\1: " & num, _
Forward:=True, MatchWildcards:=True, Wrap:=wdFindStop) = True
Selection.Collapse wdCollapseEnd
Loop
End With

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
I was more looking for something that could offset and delete the
characters to the right Code:

Would you have any ideas? I would like to jus have a single text box
to enter rather than a find and replace.

:

DavidRocastle was telling us:
DavidRocastle nous racontait que :

Thanks for your assistance.

i do have a problem with this piece of code, Jus say i enter it
the first time, i for example enter 1123
i get
Page1:
Code: 1123
Page2:
Code: 1123
.......
and so on.

After i have saved that i want to run it again. and i enter 98701
i get the following
Page1:
Code: 98701 1123
Page2:
Code: 98701 1123
....
and so on

I cannot seem to get rid of the previous data. Would you have any
ideas?

You can modify Doug's original code like this:

Dim numBefore As String
Dim numAfter As String
numBefore = InputBox("Enter the number that is to be replaced.")
numAfter = InputBox("Enter the number that is to be inserted.")
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="Code: " & numAfter,
ReplaceWith:="Code: " & numAfter, _
Forward:=True, Wrap:=wdFindStop) = True
Selection.Collapse wdCollapseEnd
Loop
End With



--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
D

DavidRocastle

Basically, a text form field is entered, and the new code will be entered in
that text form field, with the forms toolbar opened, i have to go insert
textform field and then the code is entered into it.

CODE: 123456, 123456 will be in the textford field, it is jsut a grey
background!
Graham Mayor said:
What sort of text field (press ALT+F9 and let us know what it says)?

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham i just have one last thing to tweak.

Where the code digits themselves are entered, i have a great
textfield where the values are to be entered

Is there a way i can Find: Code:eek:ld code (inside grey textfield)
Replace with Code:new code (inside grey textfield)



Graham Mayor said:
In that case

Dim num As String
num = InputBox("Enter the number that is to be inserted.")
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="([Cc]ode)[: 0-9]{1,}",
ReplaceWith:="\1: " & num, _
Forward:=True, MatchWildcards:=True, Wrap:=wdFindStop) = True
Selection.Collapse wdCollapseEnd
Loop
End With

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

My web site www.gmayor.com

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

DavidRocastle wrote:
I was more looking for something that could offset and delete the
characters to the right Code:

Would you have any ideas? I would like to jus have a single text box
to enter rather than a find and replace.

:

DavidRocastle was telling us:
DavidRocastle nous racontait que :

Thanks for your assistance.

i do have a problem with this piece of code, Jus say i enter it
the first time, i for example enter 1123
i get
Page1:
Code: 1123
Page2:
Code: 1123
.......
and so on.

After i have saved that i want to run it again. and i enter 98701
i get the following
Page1:
Code: 98701 1123
Page2:
Code: 98701 1123
....
and so on

I cannot seem to get rid of the previous data. Would you have any
ideas?

You can modify Doug's original code like this:

Dim numBefore As String
Dim numAfter As String
numBefore = InputBox("Enter the number that is to be replaced.")
numAfter = InputBox("Enter the number that is to be inserted.")
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="Code: " & numAfter,
ReplaceWith:="Code: " & numAfter, _
Forward:=True, Wrap:=wdFindStop) = True
Selection.Collapse wdCollapseEnd
Loop
End With



--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
D

DavidRocastle

Jus another query Graham, could i user a userform to enter my data rather
than using the msgbox?

Graham Mayor said:
What sort of text field (press ALT+F9 and let us know what it says)?

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham i just have one last thing to tweak.

Where the code digits themselves are entered, i have a great
textfield where the values are to be entered

Is there a way i can Find: Code:eek:ld code (inside grey textfield)
Replace with Code:new code (inside grey textfield)



Graham Mayor said:
In that case

Dim num As String
num = InputBox("Enter the number that is to be inserted.")
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="([Cc]ode)[: 0-9]{1,}",
ReplaceWith:="\1: " & num, _
Forward:=True, MatchWildcards:=True, Wrap:=wdFindStop) = True
Selection.Collapse wdCollapseEnd
Loop
End With

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

My web site www.gmayor.com

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

DavidRocastle wrote:
I was more looking for something that could offset and delete the
characters to the right Code:

Would you have any ideas? I would like to jus have a single text box
to enter rather than a find and replace.

:

DavidRocastle was telling us:
DavidRocastle nous racontait que :

Thanks for your assistance.

i do have a problem with this piece of code, Jus say i enter it
the first time, i for example enter 1123
i get
Page1:
Code: 1123
Page2:
Code: 1123
.......
and so on.

After i have saved that i want to run it again. and i enter 98701
i get the following
Page1:
Code: 98701 1123
Page2:
Code: 98701 1123
....
and so on

I cannot seem to get rid of the previous data. Would you have any
ideas?

You can modify Doug's original code like this:

Dim numBefore As String
Dim numAfter As String
numBefore = InputBox("Enter the number that is to be replaced.")
numAfter = InputBox("Enter the number that is to be inserted.")
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="Code: " & numAfter,
ReplaceWith:="Code: " & numAfter, _
Forward:=True, Wrap:=wdFindStop) = True
Selection.Collapse wdCollapseEnd
Loop
End With



--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
G

Graham Mayor

You can certainly populate text form fields from vba, and you can gather
data in a user form, which is what a user form is for. Rather than drip
feeding us with requirements, perhaps you would explain exactly what it is
you are trying to do?

In the meantime see Word MVP FAQ - Userforms
http://word.mvps.org/FAQs/Userforms.htm

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


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

Basically, a text form field is entered, and the new code will be
entered in that text form field, with the forms toolbar opened, i
have to go insert textform field and then the code is entered into it.

CODE: 123456, 123456 will be in the textford field, it is jsut a
grey background!
Graham Mayor said:
What sort of text field (press ALT+F9 and let us know what it says)?

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham i just have one last thing to tweak.

Where the code digits themselves are entered, i have a great
textfield where the values are to be entered

Is there a way i can Find: Code:eek:ld code (inside grey textfield)
Replace with Code:new code (inside grey textfield)



:

In that case

Dim num As String
num = InputBox("Enter the number that is to be inserted.")
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="([Cc]ode)[: 0-9]{1,}",
ReplaceWith:="\1: " & num, _
Forward:=True, MatchWildcards:=True, Wrap:=wdFindStop) = True
Selection.Collapse wdCollapseEnd
Loop
End With

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

My web site www.gmayor.com

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

DavidRocastle wrote:
I was more looking for something that could offset and delete the
characters to the right Code:

Would you have any ideas? I would like to jus have a single text
box to enter rather than a find and replace.

:

DavidRocastle was telling us:
DavidRocastle nous racontait que :

Thanks for your assistance.

i do have a problem with this piece of code, Jus say i enter it
the first time, i for example enter 1123
i get
Page1:
Code: 1123
Page2:
Code: 1123
.......
and so on.

After i have saved that i want to run it again. and i enter
98701 i get the following
Page1:
Code: 98701 1123
Page2:
Code: 98701 1123
....
and so on

I cannot seem to get rid of the previous data. Would you have
any ideas?

You can modify Doug's original code like this:

Dim numBefore As String
Dim numAfter As String
numBefore = InputBox("Enter the number that is to be replaced.")
numAfter = InputBox("Enter the number that is to be inserted.")
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="Code: " & numAfter,
ReplaceWith:="Code: " & numAfter, _
Forward:=True, Wrap:=wdFindStop) = True
Selection.Collapse wdCollapseEnd
Loop
End With



--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
D

DavidRocastle

Okay,

I am actually going to forget the Text Field Part of this as i don't really
need it.
-----------------
So currently the macro is working fine. I jus want to have userform rather
than a msgbox, "Enter the number that is to be inserted". When i run the
macro i want a userform to appear, which i will be able to do in VBA and
insert a logo into it, have a textfield1 which i will input the number to be
inserted into, rather than having the dull InputBox.

Dim num As String
num = InputBox("Enter the number that is to be inserted.")
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="([Cc]ode)[: 0-9]{1,}", ReplaceWith:="\1: "
& num, _
Forward:=True, MatchWildcards:=True, Wrap:=wdFindStop) = True
Selection.Collapse wdCollapseEnd
Loop
End With
 
G

Graham Mayor

I am not even going to try and explain how to code a user form in this
forum, however my old pal Greg Maxey has done most of the work for you - see
http://gregmaxey.mvps.org/UserForm_Pass_Data.htm.

Personally i would stick to the input box which is a simple user form. Why
make it more complicated than it needs to be?

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


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

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