Replace phrase with another phrase using macro?

K

Kelly

Hi all, I am using the Office 200 package and am fairly
new to VB but have a bit of experience with Excel VB.

Anyway what I would like to do is...

I have a template that currently a number of users have
access to. If I can build a slick little macro that allows
the user to select one of four company names. Then
everywhere in the document that needs that name it would
automatically replace with the name selected.

That way I can use the same credit application template
for each of four different financial institutions. As we
have already come up with a format that each institution
will agree to use we just have to make it simple to change
the name throughout the form.

I suspect there is some already existing ways to do this
but I am unfamiliar with WORD.


Thanks

Kelly
 
M

martinique

DocProperties are the secret. Either use add a custom property or perhaps
use the Company field on the Properties > Summary dialog. Check Fields from
the Insert menu, rand read Help on it.
 
M

Mark Tangard

Kelly,

There are 2 ways -- semi-slick (using a protected form), and
very slick, using a custom dialog ("userform").

[1] Semi-Slick Method:

First, store the following macro in the template:

Sub UpdateMyFields
ActiveDocument.Fields.Update
End Sub

Now isolate the first mention of the company name so that
no other variable text is in the same paragraph. Add a
continuous section break just after it -- and also just
before it if it's not the first thing in the file. From
the user's POV this design will work best if the company
name is prominent and early.)

(Display the Forms toolbar if it's not showing -- click
View-> Toolbars-> Forms.) In place of the first mention
of the company name, add a DropDown formfield using the
Forms toolbar (icon looks like a set of little rectangles
or, as one pupil insists, a very squared-off coat of arms.

Double-click the formfield you just inserted and in the
Options dialog that appears, add the 4 company names to
the DropDown Item List box. Note the displayed bookmark
name (probably DropDown1) to be assigned to the formfield
you're creating. Optionally (but recommended), change it
to something more descriptive, say, CompanyName. In the
Run Macro On Exit box, select the name of the macro you
just stored. Click OK.

Now click Tools­> Protect Document. In the dialog that
appears, click Sections. In the next window, clear the
checkboxes for all sections except the one your dropdown
formfield is in. Click OK and OK again.

(Note that this 'protecting' has nothing to do with making
the file safe, unopenable, sanitary, child-resistant, etc.
In Wordspeak, "protecting a section for forms" just means
everything in it except formfields will be frozen, i.e.,
unchangable by the user, essentially duplicating a paper
form where you can fill in blanks, checkboxes, etc., but
not change their labels.)

In the other places in the doc that are to contain the
company name, enter regular fields like this:

{ CompanyName }

Note that you must enter the braces using CTRL+F9 and NOT
with the '{' and '}' keys. [Terminology note: These are
'regular' fields, as opposed to 'formfields' like the one
you built earlier.] Now save the template and close it.

When the user launches a new doc based on your template,
he/she can click the dropdown arrow that'll appear next
to the company name and choose one of the 4 options.
Pressing TAB to exit the formfield takes him/her to the
next section and runs the macro that updates the other
fields so that they all show the selected company name.

[2] Very Slick Method:

Build a small userform, called by the AutoNew macro in
the template so it'll appear whenever a new document is
created from it. Have it contain a 'ComboBox' control
(function is similar to the dropdown formfield above)
and have the button that dismisses the form insert the
value the user chooses from the ComboBox at locations
in the document (template) at which you have inserted
bookmarks. In this method, the user would see a custom
dialog (just like a Word dialog, such as Font, Print,
etc.) in front of the document in which he/she can
choose the company name. You can also design the
userform to prompt for other things to be inserted
into the document, potentially alleviating mountains
of tedium.

If this all sounds vague, it's because explaining how
to build a userform is beyond the scope of a simple
newsgroup reply. Building a userform is indeed more
work than building a protected form, but it will (1)
make your users ecstatic, and (2) reveal dozens of
new possibilities for saving time and nerves.
Protected forms have numerous limitations that tend
to frustrate anyone who gets deeply involved with
using them. And since you know some Excel VBA, what
you'd need to learn in Word VBA wouldn't likely faze
you much. (Both programs can have userforms and the
code specific to the design and structure userforms
is 100% identical.)

If this sounds even remotely useful, see the comments
at http://www.speakeasy.org/~mtangard/userforms.html
and then try the brief tutorial on the MVP site at
http://www.mvps.org/word/FAQs/Userforms/CreateAUserForm.htm
 
K

Kelly

-----Original Message-----
Kelly,

There are 2 ways -- semi-slick (using a protected form), and
very slick, using a custom dialog ("userform").

[1] Semi-Slick Method:

First, store the following macro in the template:

Sub UpdateMyFields
ActiveDocument.Fields.Update
End Sub

Now isolate the first mention of the company name so that
no other variable text is in the same paragraph. Add a
continuous section break just after it -- and also just
before it if it's not the first thing in the file. From
the user's POV this design will work best if the company
name is prominent and early.)

(Display the Forms toolbar if it's not showing -- click
View-> Toolbars-> Forms.) In place of the first mention
of the company name, add a DropDown formfield using the
Forms toolbar (icon looks like a set of little rectangles
or, as one pupil insists, a very squared-off coat of arms.

Double-click the formfield you just inserted and in the
Options dialog that appears, add the 4 company names to
the DropDown Item List box. Note the displayed bookmark
name (probably DropDown1) to be assigned to the formfield
you're creating. Optionally (but recommended), change it
to something more descriptive, say, CompanyName. In the
Run Macro On Exit box, select the name of the macro you
just stored. Click OK.

Now click Tools­> Protect Document. In the dialog that
appears, click Sections. In the next window, clear the
checkboxes for all sections except the one your dropdown
formfield is in. Click OK and OK again.

(Note that this 'protecting' has nothing to do with making
the file safe, unopenable, sanitary, child-resistant, etc.
In Wordspeak, "protecting a section for forms" just means
everything in it except formfields will be frozen, i.e.,
unchangable by the user, essentially duplicating a paper
form where you can fill in blanks, checkboxes, etc., but
not change their labels.)

In the other places in the doc that are to contain the
company name, enter regular fields like this:

{ CompanyName }

Note that you must enter the braces using CTRL+F9 and NOT
with the '{' and '}' keys. [Terminology note: These are
'regular' fields, as opposed to 'formfields' like the one
you built earlier.] Now save the template and close it.

When the user launches a new doc based on your template,
he/she can click the dropdown arrow that'll appear next
to the company name and choose one of the 4 options.
Pressing TAB to exit the formfield takes him/her to the
next section and runs the macro that updates the other
fields so that they all show the selected company name.

[2] Very Slick Method:

Build a small userform, called by the AutoNew macro in
the template so it'll appear whenever a new document is
created from it. Have it contain a 'ComboBox' control
(function is similar to the dropdown formfield above)
and have the button that dismisses the form insert the
value the user chooses from the ComboBox at locations
in the document (template) at which you have inserted
bookmarks. In this method, the user would see a custom
dialog (just like a Word dialog, such as Font, Print,
etc.) in front of the document in which he/she can
choose the company name. You can also design the
userform to prompt for other things to be inserted
into the document, potentially alleviating mountains
of tedium.

If this all sounds vague, it's because explaining how
to build a userform is beyond the scope of a simple
newsgroup reply. Building a userform is indeed more
work than building a protected form, but it will (1)
make your users ecstatic, and (2) reveal dozens of
new possibilities for saving time and nerves.
Protected forms have numerous limitations that tend
to frustrate anyone who gets deeply involved with
using them. And since you know some Excel VBA, what
you'd need to learn in Word VBA wouldn't likely faze
you much. (Both programs can have userforms and the
code specific to the design and structure userforms
is 100% identical.)

If this sounds even remotely useful, see the comments
at http://www.speakeasy.org/~mtangard/userforms.html
and then try the brief tutorial on the MVP site at
http://www.mvps.org/word/FAQs/Userforms/CreateAUserForm.ht
m
--
Mark Tangard <[email protected]>, Microsoft Word MVP
Please reply ONLY to the newsgroup, not by private mail.
Note well: MVPs do not work for Microsoft.
"Life is nothing if you're not obsessed." --John Waters


.
WOW, I am truly impressed with the effort you spent in
trying to help me out. I have not tryied your suggestions
yet but will likley get started this afternoon.

Some of the stuff you speak of I am already familiar with
but I appreciate the effort and clear descriptions.

Kelly
 
K

Kelly

-----Original Message-----
Kelly,

There are 2 ways -- semi-slick (using a protected form), and
very slick, using a custom dialog ("userform").

[1] Semi-Slick Method:

First, store the following macro in the template:

Sub UpdateMyFields
ActiveDocument.Fields.Update
End Sub

Now isolate the first mention of the company name so that
no other variable text is in the same paragraph. Add a
continuous section break just after it -- and also just
before it if it's not the first thing in the file. From
the user's POV this design will work best if the company
name is prominent and early.)

(Display the Forms toolbar if it's not showing -- click
View-> Toolbars-> Forms.) In place of the first mention
of the company name, add a DropDown formfield using the
Forms toolbar (icon looks like a set of little rectangles
or, as one pupil insists, a very squared-off coat of arms.

Double-click the formfield you just inserted and in the
Options dialog that appears, add the 4 company names to
the DropDown Item List box. Note the displayed bookmark
name (probably DropDown1) to be assigned to the formfield
you're creating. Optionally (but recommended), change it
to something more descriptive, say, CompanyName. In the
Run Macro On Exit box, select the name of the macro you
just stored. Click OK.

Now click Tools­> Protect Document. In the dialog that
appears, click Sections. In the next window, clear the
checkboxes for all sections except the one your dropdown
formfield is in. Click OK and OK again.

(Note that this 'protecting' has nothing to do with making
the file safe, unopenable, sanitary, child-resistant, etc.
In Wordspeak, "protecting a section for forms" just means
everything in it except formfields will be frozen, i.e.,
unchangable by the user, essentially duplicating a paper
form where you can fill in blanks, checkboxes, etc., but
not change their labels.)

In the other places in the doc that are to contain the
company name, enter regular fields like this:

{ CompanyName }

Note that you must enter the braces using CTRL+F9 and NOT
with the '{' and '}' keys. [Terminology note: These are
'regular' fields, as opposed to 'formfields' like the one
you built earlier.] Now save the template and close it.

When the user launches a new doc based on your template,
he/she can click the dropdown arrow that'll appear next
to the company name and choose one of the 4 options.
Pressing TAB to exit the formfield takes him/her to the
next section and runs the macro that updates the other
fields so that they all show the selected company name.

[2] Very Slick Method:

Build a small userform, called by the AutoNew macro in
the template so it'll appear whenever a new document is
created from it. Have it contain a 'ComboBox' control
(function is similar to the dropdown formfield above)
and have the button that dismisses the form insert the
value the user chooses from the ComboBox at locations
in the document (template) at which you have inserted
bookmarks. In this method, the user would see a custom
dialog (just like a Word dialog, such as Font, Print,
etc.) in front of the document in which he/she can
choose the company name. You can also design the
userform to prompt for other things to be inserted
into the document, potentially alleviating mountains
of tedium.

If this all sounds vague, it's because explaining how
to build a userform is beyond the scope of a simple
newsgroup reply. Building a userform is indeed more
work than building a protected form, but it will (1)
make your users ecstatic, and (2) reveal dozens of
new possibilities for saving time and nerves.
Protected forms have numerous limitations that tend
to frustrate anyone who gets deeply involved with
using them. And since you know some Excel VBA, what
you'd need to learn in Word VBA wouldn't likely faze
you much. (Both programs can have userforms and the
code specific to the design and structure userforms
is 100% identical.)

If this sounds even remotely useful, see the comments
at http://www.speakeasy.org/~mtangard/userforms.html
and then try the brief tutorial on the MVP site at
http://www.mvps.org/word/FAQs/Userforms/CreateAUserForm.ht
m
--
Mark Tangard <[email protected]>, Microsoft Word MVP
Please reply ONLY to the newsgroup, not by private mail.
Note well: MVPs do not work for Microsoft.
"Life is nothing if you're not obsessed." --John Waters


.
Hey Mark, I finally had a chance to try out your
suggestion. Method one.

But I am having problems getting it to work.

I think I followed your advice pretty close but when the
section of the document that contains the dropdown is
protected then the "run macro on exit" does not seem to
run. And if I leave it unprotected then the dropdown does
not work. Actually it seems I can never get the "run macro
on exit" to work?? Even with the form unprotected a test
macro I made never ran when I clicked in and then out of
the dropdown field?

Any ideas?

I am using Word 2000 sp-3

Kelly
 
M

Mark Tangard

Kelly,

Are the subsequent mentions of the company name all in
headers or footer, perchance? If so, you need a slightly
different macro.

To see if the macro is running at all, put this line:

MsgBox "Running now"

just after the first line of the macro and then try the
form (protected). If you get the MsgBox when exiting
the field then something else is going on. In that case
please email me the file and I'll have a look.

--
Mark Tangard <[email protected]>, Microsoft Word MVP
Please reply only to the newsgroup, not by private email.
Note well: MVPs do not work for Microsoft.
"Life is nothing if you're not obsessed." --John Waters


-----Original Message-----
Kelly,

There are 2 ways -- semi-slick (using a protected form), and
very slick, using a custom dialog ("userform").

[1] Semi-Slick Method:

First, store the following macro in the template:

Sub UpdateMyFields
ActiveDocument.Fields.Update
End Sub

Now isolate the first mention of the company name so that
no other variable text is in the same paragraph. Add a
continuous section break just after it -- and also just
before it if it's not the first thing in the file. From
the user's POV this design will work best if the company
name is prominent and early.)

(Display the Forms toolbar if it's not showing -- click
View-> Toolbars-> Forms.) In place of the first mention
of the company name, add a DropDown formfield using the
Forms toolbar (icon looks like a set of little rectangles
or, as one pupil insists, a very squared-off coat of arms.

Double-click the formfield you just inserted and in the
Options dialog that appears, add the 4 company names to
the DropDown Item List box. Note the displayed bookmark
name (probably DropDown1) to be assigned to the formfield
you're creating. Optionally (but recommended), change it
to something more descriptive, say, CompanyName. In the
Run Macro On Exit box, select the name of the macro you
just stored. Click OK.

Now click Tools­> Protect Document. In the dialog that
appears, click Sections. In the next window, clear the
checkboxes for all sections except the one your dropdown
formfield is in. Click OK and OK again.

(Note that this 'protecting' has nothing to do with making
the file safe, unopenable, sanitary, child-resistant, etc.
In Wordspeak, "protecting a section for forms" just means
everything in it except formfields will be frozen, i.e.,
unchangable by the user, essentially duplicating a paper
form where you can fill in blanks, checkboxes, etc., but
not change their labels.)

In the other places in the doc that are to contain the
company name, enter regular fields like this:

{ CompanyName }

Note that you must enter the braces using CTRL+F9 and NOT
with the '{' and '}' keys. [Terminology note: These are
'regular' fields, as opposed to 'formfields' like the one
you built earlier.] Now save the template and close it.

When the user launches a new doc based on your template,
he/she can click the dropdown arrow that'll appear next
to the company name and choose one of the 4 options.
Pressing TAB to exit the formfield takes him/her to the
next section and runs the macro that updates the other
fields so that they all show the selected company name.

[2] Very Slick Method:

Build a small userform, called by the AutoNew macro in
the template so it'll appear whenever a new document is
created from it. Have it contain a 'ComboBox' control
(function is similar to the dropdown formfield above)
and have the button that dismisses the form insert the
value the user chooses from the ComboBox at locations
in the document (template) at which you have inserted
bookmarks. In this method, the user would see a custom
dialog (just like a Word dialog, such as Font, Print,
etc.) in front of the document in which he/she can
choose the company name. You can also design the
userform to prompt for other things to be inserted
into the document, potentially alleviating mountains
of tedium.

If this all sounds vague, it's because explaining how
to build a userform is beyond the scope of a simple
newsgroup reply. Building a userform is indeed more
work than building a protected form, but it will (1)
make your users ecstatic, and (2) reveal dozens of
new possibilities for saving time and nerves.
Protected forms have numerous limitations that tend
to frustrate anyone who gets deeply involved with
using them. And since you know some Excel VBA, what
you'd need to learn in Word VBA wouldn't likely faze
you much. (Both programs can have userforms and the
code specific to the design and structure userforms
is 100% identical.)

If this sounds even remotely useful, see the comments
at http://www.speakeasy.org/~mtangard/userforms.html
and then try the brief tutorial on the MVP site at
http://www.mvps.org/word/FAQs/Userforms/CreateAUserForm.ht m

--
Mark Tangard <[email protected]>, Microsoft Word MVP
Please reply ONLY to the newsgroup, not by private mail.
Note well: MVPs do not work for Microsoft.
"Life is nothing if you're not obsessed." --John Waters

Hi all, I am using the Office 200 package and am fairly
new to VB but have a bit of experience with Excel VB.

Anyway what I would like to do is...

I have a template that currently a number of users have
access to. If I can build a slick little macro that allows
the user to select one of four company names. Then
everywhere in the document that needs that name it would
automatically replace with the name selected.

That way I can use the same credit application template
for each of four different financial institutions. As we
have already come up with a format that each institution
will agree to use we just have to make it simple to change
the name throughout the form.

I suspect there is some already existing ways to do this
but I am unfamiliar with WORD.

Thanks

Kelly
.
Hey Mark, I finally had a chance to try out your
suggestion. Method one.

But I am having problems getting it to work.

I think I followed your advice pretty close but when the
section of the document that contains the dropdown is
protected then the "run macro on exit" does not seem to
run. And if I leave it unprotected then the dropdown does
not work. Actually it seems I can never get the "run macro
on exit" to work?? Even with the form unprotected a test
macro I made never ran when I clicked in and then out of
the dropdown field?

Any ideas?

I am using Word 2000 sp-3

Kelly
 
K

Kelly

-----Original Message-----
Kelly,

Are the subsequent mentions of the company name all in
headers or footer, perchance? If so, you need a slightly
different macro.

To see if the macro is running at all, put this line:

MsgBox "Running now"

just after the first line of the macro and then try the
form (protected). If you get the MsgBox when exiting
the field then something else is going on. In that case
please email me the file and I'll have a look.

--
Mark Tangard <[email protected]>, Microsoft Word MVP
Please reply only to the newsgroup, not by private email.
Note well: MVPs do not work for Microsoft.
"Life is nothing if you're not obsessed." --John Waters


-----Original Message-----
Kelly,

There are 2 ways -- semi-slick (using a protected
form),
and
very slick, using a custom dialog ("userform").

[1] Semi-Slick Method:

First, store the following macro in the template:

Sub UpdateMyFields
ActiveDocument.Fields.Update
End Sub

Now isolate the first mention of the company name so that
no other variable text is in the same paragraph. Add a
continuous section break just after it -- and also just
before it if it's not the first thing in the file. From
the user's POV this design will work best if the company
name is prominent and early.)

(Display the Forms toolbar if it's not showing -- click
View-> Toolbars-> Forms.) In place of the first mention
of the company name, add a DropDown formfield using the
Forms toolbar (icon looks like a set of little rectangles
or, as one pupil insists, a very squared-off coat of arms.

Double-click the formfield you just inserted and in the
Options dialog that appears, add the 4 company names to
the DropDown Item List box. Note the displayed bookmark
name (probably DropDown1) to be assigned to the formfield
you're creating. Optionally (but recommended), change it
to something more descriptive, say, CompanyName. In the
Run Macro On Exit box, select the name of the macro you
just stored. Click OK.

Now click Tools­> Protect Document. In the dialog that
appears, click Sections. In the next window, clear the
checkboxes for all sections except the one your dropdown
formfield is in. Click OK and OK again.

(Note that this 'protecting' has nothing to do with making
the file safe, unopenable, sanitary, child-resistant, etc.
In Wordspeak, "protecting a section for forms" just means
everything in it except formfields will be frozen, i.e.,
unchangable by the user, essentially duplicating a paper
form where you can fill in blanks, checkboxes, etc., but
not change their labels.)

In the other places in the doc that are to contain the
company name, enter regular fields like this:

{ CompanyName }

Note that you must enter the braces using CTRL+F9 and NOT
with the '{' and '}' keys. [Terminology note: These are
'regular' fields, as opposed to 'formfields' like the one
you built earlier.] Now save the template and close it.

When the user launches a new doc based on your template,
he/she can click the dropdown arrow that'll appear next
to the company name and choose one of the 4 options.
Pressing TAB to exit the formfield takes him/her to the
next section and runs the macro that updates the other
fields so that they all show the selected company name.

[2] Very Slick Method:

Build a small userform, called by the AutoNew macro in
the template so it'll appear whenever a new document is
created from it. Have it contain a 'ComboBox' control
(function is similar to the dropdown formfield above)
and have the button that dismisses the form insert the
value the user chooses from the ComboBox at locations
in the document (template) at which you have inserted
bookmarks. In this method, the user would see a custom
dialog (just like a Word dialog, such as Font, Print,
etc.) in front of the document in which he/she can
choose the company name. You can also design the
userform to prompt for other things to be inserted
into the document, potentially alleviating mountains
of tedium.

If this all sounds vague, it's because explaining how
to build a userform is beyond the scope of a simple
newsgroup reply. Building a userform is indeed more
work than building a protected form, but it will (1)
make your users ecstatic, and (2) reveal dozens of
new possibilities for saving time and nerves.
Protected forms have numerous limitations that tend
to frustrate anyone who gets deeply involved with
using them. And since you know some Excel VBA, what
you'd need to learn in Word VBA wouldn't likely faze
you much. (Both programs can have userforms and the
code specific to the design and structure userforms
is 100% identical.)

If this sounds even remotely useful, see the comments
at http://www.speakeasy.org/~mtangard/userforms.html
and then try the brief tutorial on the MVP site at
http://www.mvps.org/word/FAQs/Userforms/CreateAUserForm.ht
m

--
Mark Tangard <[email protected]>, Microsoft Word MVP
Please reply ONLY to the newsgroup, not by private mail.
Note well: MVPs do not work for Microsoft.
"Life is nothing if you're not obsessed." --John Waters


Kelly wrote:

Hi all, I am using the Office 200 package and am fairly
new to VB but have a bit of experience with Excel VB.

Anyway what I would like to do is...

I have a template that currently a number of users have
access to. If I can build a slick little macro that allows
the user to select one of four company names. Then
everywhere in the document that needs that name it would
automatically replace with the name selected.

That way I can use the same credit application template
for each of four different financial institutions. As we
have already come up with a format that each institution
will agree to use we just have to make it simple to change
the name throughout the form.

I suspect there is some already existing ways to do this
but I am unfamiliar with WORD.

Thanks

Kelly
.
Hey Mark, I finally had a chance to try out your
suggestion. Method one.

But I am having problems getting it to work.

I think I followed your advice pretty close but when the
section of the document that contains the dropdown is
protected then the "run macro on exit" does not seem to
run. And if I leave it unprotected then the dropdown does
not work. Actually it seems I can never get the "run macro
on exit" to work?? Even with the form unprotected a test
macro I made never ran when I clicked in and then out of
the dropdown field?

Any ideas?

I am using Word 2000 sp-3

Kelly
.
Hey just figures it out. I have to TAB out of the field
for the macro to run. Any suggestions if there is a way to
make the macro run even if the user used his mouse to
CLICK out of the field?

Thanks again,

Kelly
 

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