Copy - Paste problems in macro

H

Henry

I'm automating a word doc. that has one paragraph which may be repeated
several times with only one word difference.
I've put a form field where the word should go and copied and pasted the
paragraph the required number of times (from a numerical user input).
E.g. If the user inputs 3, the paragraph is copied once and pasted twice.
The paragraph pastes ok (with the form field in place) the requisite number
of times, but there is/are no bookmark(s) for the(se) pasted field(s).
I tried a manual copy and paste of a form field and it appears that this is
normal behavior for word (no bookmark is pasted).

My problem is, how to refer to the(se) pasted formfield(s) from code.

It shouldn't make a difference, but FYI, I'm writing the macro in Excel VBA.

This is how I'm getting data into other form fields.

appWD.ActiveDocument.FormFields("Surname").Select 'Surname
appWD.ActiveDocument.FormFields("Surname").Result =
InputForm.Surnamebox.Text

Any suggestion would be appreciated.

Henry
 
J

Jay Freedman

Hi, Henry,

After the first occurrence, don't try to use form fields. Instead, use REF
fields pointing to the bookmark name of the (one and only) form field.
 
H

Henry

Hi Jay,

Thanks for your reply but, AFAIK REF fields put a copy of whatever they
refer to in themselves.
In the example below all the questions would ask about a Dog.

The sort of thing I want is:

Q1 [there will always be a Q1] Do you own a [bookmarked formfield here].....
If so, then ......etc [several lines of
text]
Q2 [there might not be a Q2] Do you own a [bookmarked formfield here].....
If so, then ......etc [several lines of
text]
Q3 [there might not be a Q3] Do you own a [bookmarked formfield here].....
If so, then ......etc [several lines of
text]
Qetc.
Every paragraph will be the same except for the entry in the form field
I then want to programatically enter in the formfields, for Q1: Dog, for Q2:
Cat, for Q3: Cow etc. on one run of the doc.
On the next run I may only need one question and enter Car.
There can be up to 9 questions and the entry in the form field will not be
repeated within the doc.
What I have done so far is to copy Q1 (with a blank formfield) and then
pasted it the required number of times (from an excel macro).
I planned to fill in the formfields from the macro, but as there is no
bookmark, I can't refer to the 2nd and subsequent fields.
When it's running properly, I'll set AppWd.Visible = False and it will run
and print from behind excel.

As a workround, I can set up 9 different documents and get excel to invoke
the doc. with the appropriate number of questions.
Seems a longwinded way of doing it, but at least I know I can get it to
work!

Henry
 
J

Jay Freedman

Hi, Henry,

I see I misread your original post. My aplogies.

After your code inserts as many copies as needed, without bookmark names,
run a loop like this to assign names to them:

Dim i As Integer
For i = ActiveDocument.FormFields.Count To 1 Step -1
ActiveDocument.FormFields(i).Select
With Dialogs(wdDialogFormFieldOptions)
.Name = "Item" & i
.Execute
End With
Next

This code contains not one but *two* workarounds for bugs (aka "features")
in Word VBA: (1) Although the Help claims that a FormField's .Name property
is read/write, attempting a simple ActiveDocument.FormFields(i).Name =
"Item" & i fails with an error if the current name is empty. The dialog
..Execute works, though. (2) For unknown reasons, trying to use a loop like
For Each oFld in ActiveDocument.FormFields only selects the first formfield,
and keeps renaming it in an infinite loop, hence the countdown For...Next
loop.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://www.mvps.org/word
Hi Jay,

Thanks for your reply but, AFAIK REF fields put a copy of whatever
they refer to in themselves.
In the example below all the questions would ask about a Dog.

The sort of thing I want is:

Q1 [there will always be a Q1] Do you own a [bookmarked formfield
here]..... If so, then ......etc
[several lines of text]
Q2 [there might not be a Q2] Do you own a [bookmarked formfield
here]..... If so, then ......etc
[several lines of text]
Q3 [there might not be a Q3] Do you own a [bookmarked formfield
here]..... If so, then ......etc
[several lines of text]
Qetc.
Every paragraph will be the same except for the entry in the form
field I then want to programatically enter in the formfields, for Q1:
Dog, for Q2: Cat, for Q3: Cow etc. on one run of the doc.
On the next run I may only need one question and enter Car.
There can be up to 9 questions and the entry in the form field will
not be repeated within the doc.
What I have done so far is to copy Q1 (with a blank formfield) and
then pasted it the required number of times (from an excel macro).
I planned to fill in the formfields from the macro, but as there is no
bookmark, I can't refer to the 2nd and subsequent fields.
When it's running properly, I'll set AppWd.Visible = False and it
will run and print from behind excel.

As a workround, I can set up 9 different documents and get excel to
invoke the doc. with the appropriate number of questions.
Seems a longwinded way of doing it, but at least I know I can get it
to work!

Henry


Jay Freedman said:
Hi, Henry,

After the first occurrence, don't try to use form fields. Instead,
use REF fields pointing to the bookmark name of the (one and only)
form field.
 
H

Henry

Thanks Jay,

I've not tried it yet, but it looks very much like what I want.

One of the bugs (aka "features") of Word is it's inability to assign a
bookmark/name to a pasted formfield.
MSForms manages to assign a new, unique name to a pasted control!

Henry

Jay Freedman said:
Hi, Henry,

I see I misread your original post. My aplogies.

After your code inserts as many copies as needed, without bookmark names,
run a loop like this to assign names to them:

Dim i As Integer
For i = ActiveDocument.FormFields.Count To 1 Step -1
ActiveDocument.FormFields(i).Select
With Dialogs(wdDialogFormFieldOptions)
.Name = "Item" & i
.Execute
End With
Next

This code contains not one but *two* workarounds for bugs (aka "features")
in Word VBA: (1) Although the Help claims that a FormField's .Name property
is read/write, attempting a simple ActiveDocument.FormFields(i).Name =
"Item" & i fails with an error if the current name is empty. The dialog
.Execute works, though. (2) For unknown reasons, trying to use a loop like
For Each oFld in ActiveDocument.FormFields only selects the first formfield,
and keeps renaming it in an infinite loop, hence the countdown For...Next
loop.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://www.mvps.org/word
Hi Jay,

Thanks for your reply but, AFAIK REF fields put a copy of whatever
they refer to in themselves.
In the example below all the questions would ask about a Dog.

The sort of thing I want is:

Q1 [there will always be a Q1] Do you own a [bookmarked formfield
here]..... If so, then ......etc
[several lines of text]
Q2 [there might not be a Q2] Do you own a [bookmarked formfield
here]..... If so, then ......etc
[several lines of text]
Q3 [there might not be a Q3] Do you own a [bookmarked formfield
here]..... If so, then ......etc
[several lines of text]
Qetc.
Every paragraph will be the same except for the entry in the form
field I then want to programatically enter in the formfields, for Q1:
Dog, for Q2: Cat, for Q3: Cow etc. on one run of the doc.
On the next run I may only need one question and enter Car.
There can be up to 9 questions and the entry in the form field will
not be repeated within the doc.
What I have done so far is to copy Q1 (with a blank formfield) and
then pasted it the required number of times (from an excel macro).
I planned to fill in the formfields from the macro, but as there is no
bookmark, I can't refer to the 2nd and subsequent fields.
When it's running properly, I'll set AppWd.Visible = False and it
will run and print from behind excel.

As a workround, I can set up 9 different documents and get excel to
invoke the doc. with the appropriate number of questions.
Seems a longwinded way of doing it, but at least I know I can get it
to work!

Henry


Jay Freedman said:
Hi, Henry,

After the first occurrence, don't try to use form fields. Instead,
use REF fields pointing to the bookmark name of the (one and only)
form field.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://www.mvps.org/word

Henry wrote:
I'm automating a word doc. that has one paragraph which may be
repeated several times with only one word difference.
I've put a form field where the word should go and copied and pasted
the paragraph the required number of times (from a numerical user
input).
E.g. If the user inputs 3, the paragraph is copied once and pasted
twice. The paragraph pastes ok (with the form field in place) the
requisite number of times, but there is/are no bookmark(s) for
the(se) pasted field(s).
I tried a manual copy and paste of a form field and it appears that
this is normal behavior for word (no bookmark is pasted).

My problem is, how to refer to the(se) pasted formfield(s) from
code.

It shouldn't make a difference, but FYI, I'm writing the macro in
Excel VBA.

This is how I'm getting data into other form fields.

appWD.ActiveDocument.FormFields("Surname").Select
'Surname appWD.ActiveDocument.FormFields("Surname").Result =
InputForm.Surnamebox.Text

Any suggestion would be appreciated.

Henry
 
H

Henry

Thanks Jay,

I've not tried it yet, but it looks very much like what I want.

One of the bugs (aka "features") of Word is it's inability to assign a
bookmark/name to a pasted formfield.
MSForms manages to assign a new, unique name to a pasted control!

Henry

Jay Freedman said:
Hi, Henry,

I see I misread your original post. My aplogies.

After your code inserts as many copies as needed, without bookmark names,
run a loop like this to assign names to them:

Dim i As Integer
For i = ActiveDocument.FormFields.Count To 1 Step -1
ActiveDocument.FormFields(i).Select
With Dialogs(wdDialogFormFieldOptions)
.Name = "Item" & i
.Execute
End With
Next

This code contains not one but *two* workarounds for bugs (aka "features")
in Word VBA: (1) Although the Help claims that a FormField's .Name property
is read/write, attempting a simple ActiveDocument.FormFields(i).Name =
"Item" & i fails with an error if the current name is empty. The dialog
.Execute works, though. (2) For unknown reasons, trying to use a loop like
For Each oFld in ActiveDocument.FormFields only selects the first formfield,
and keeps renaming it in an infinite loop, hence the countdown For...Next
loop.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://www.mvps.org/word
Hi Jay,

Thanks for your reply but, AFAIK REF fields put a copy of whatever
they refer to in themselves.
In the example below all the questions would ask about a Dog.

The sort of thing I want is:

Q1 [there will always be a Q1] Do you own a [bookmarked formfield
here]..... If so, then ......etc
[several lines of text]
Q2 [there might not be a Q2] Do you own a [bookmarked formfield
here]..... If so, then ......etc
[several lines of text]
Q3 [there might not be a Q3] Do you own a [bookmarked formfield
here]..... If so, then ......etc
[several lines of text]
Qetc.
Every paragraph will be the same except for the entry in the form
field I then want to programatically enter in the formfields, for Q1:
Dog, for Q2: Cat, for Q3: Cow etc. on one run of the doc.
On the next run I may only need one question and enter Car.
There can be up to 9 questions and the entry in the form field will
not be repeated within the doc.
What I have done so far is to copy Q1 (with a blank formfield) and
then pasted it the required number of times (from an excel macro).
I planned to fill in the formfields from the macro, but as there is no
bookmark, I can't refer to the 2nd and subsequent fields.
When it's running properly, I'll set AppWd.Visible = False and it
will run and print from behind excel.

As a workround, I can set up 9 different documents and get excel to
invoke the doc. with the appropriate number of questions.
Seems a longwinded way of doing it, but at least I know I can get it
to work!

Henry


Jay Freedman said:
Hi, Henry,

After the first occurrence, don't try to use form fields. Instead,
use REF fields pointing to the bookmark name of the (one and only)
form field.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://www.mvps.org/word

Henry wrote:
I'm automating a word doc. that has one paragraph which may be
repeated several times with only one word difference.
I've put a form field where the word should go and copied and pasted
the paragraph the required number of times (from a numerical user
input).
E.g. If the user inputs 3, the paragraph is copied once and pasted
twice. The paragraph pastes ok (with the form field in place) the
requisite number of times, but there is/are no bookmark(s) for
the(se) pasted field(s).
I tried a manual copy and paste of a form field and it appears that
this is normal behavior for word (no bookmark is pasted).

My problem is, how to refer to the(se) pasted formfield(s) from
code.

It shouldn't make a difference, but FYI, I'm writing the macro in
Excel VBA.

This is how I'm getting data into other form fields.

appWD.ActiveDocument.FormFields("Surname").Select
'Surname appWD.ActiveDocument.FormFields("Surname").Result =
InputForm.Surnamebox.Text

Any suggestion would be appreciated.

Henry
 

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