recreate a deleted formfield

G

Guest

Hi,
I am trying to create a template based on a number of user forms that
automatically fill in formfields, so that the resulting document is properly
formatted with all the required information. On the first user form, I ask
for (1) species common name, (2) whether there is a population name (Y/N),
(3) if so what it is, and (4) latin name. Then click OK and move onto the
next user form. I have it set up so that if the population question is
answered 'no', those formfields are deleted.

The problem is that on the next user form I have a back button, so that if a
user wanted to change information on the first user form they could go back.
But, in testing this, if I want to change the answer to the population
question to 'yes', and have the population name entered into its formfields,
the formfield has been deleted and I get an error message.

How do I incorporate the appropriate code, so that if the answer is 'No' the
field is deleted, but if the answer is then changed to 'yes' the formfield is
recreated and filled in??? The code I have so far is pasted below. BTW -
I'm pretty new to all this programming stuff, and to be honest, find it
horrifically confusing :0)

Thanks in advance for any help!

Private Sub OK3_Click()
pToggleProtectDoc

vCommonName = UserForm3.txtCommonName.Text
vPopulation = UserForm3.txtPopulation.Text
vScientificName = UserForm3.txtScientificName.Text

ActiveDocument.FormFields("bkCommonName1").Result = vCommonName
ActiveDocument.FormFields("bkCommonName2").Result = vCommonName
ActiveDocument.FormFields("bkCommonName3").Result = vCommonName
ActiveDocument.FormFields("bkCommonName4").Result = vCommonName

If UserForm3.optYes.Value = True Then
ActiveDocument.FormFields("bkPopulation1").Result = vPopulation
ActiveDocument.FormFields("bkPopulation2").Result = vPopulation
ActiveDocument.FormFields("bkPopulation3").Result = vPopulation
Else
ActiveDocument.FormFields("bkPopulation1").Delete
ActiveDocument.FormFields("bkPopulation2").Delete
ActiveDocument.FormFields("bkPopulation3").Delete
End If

ActiveDocument.FormFields("bkScientificName1").Result = vScientificName
ActiveDocument.FormFields("bkScientificName2").Result = vScientificName
ActiveDocument.FormFields("bkScientificName3").Result = vScientificName

pToggleProtectDoc

UserForm3.Hide
UserForm4.Show
End Sub
 
C

Charles Kenyon

Why not blank and disable the unused formfields rather than delete them?
They could be formatted easily so that nothing prints when they are empty.

BTW, you might want to ask this question in the vba.userforms newsgroup
rather than in the document management one.
--

Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide

See also the MVP FAQ: http://www.mvps.org/word which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
G

Guest

Thanks Charles. Way back at the beginning of this excersize that I thought
would take an afternoon to complete, I just left the formfields blank (I
don't know how to actually disable them -- a baseball bat should do it,
right?). But this ended up leaving numerous empty spaces in the final
document that I find displeasing (the formfields are within paragraphs
talking about Species X, population Y - so if the population formfield was
blank it left a funny space in the middle of a sentence).

I'll try posting this on the VBA newsgroup as suggested (I didn't even
realize there was more than one!).

Thanks again
 

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