Hello all

I have made a user form to populate a Word 2003 template. I have added a
piece of code that exits the population of the document if the required
fields aren't filled in, it reads like the sample below:

If Len(Trim(cmbDiv.text)) <= 0 Or Len(Trim(cmbDept.text)) <= 0 Or
Len(Trim(txtEMail.text)) <= 0 Then
MsgBox "You must fill in all the required fields"
Exit Sub
ElectronicOption.Enabled = True
End If

The code works, however what happens is that if the code is exited the form
has already placed some data in the template, and it duplicates it. So
instead of for example seeing the Department name as "Sales and Marketing",
you see it as "Sales and Marketing Sales and Marketing". I thought I would
have to write something like me.refresh before Exit Sub, but I can't find
anything in Word vba like that, I tried Repaint, but that didn't work.

I think the problem is that the data is already there, and when the rest of
the fields are filled in and the OK button clicked, the fact that some of the
data was already there is not recognised, and in that case I don't know what
to do. Does anyone have any ideas?

Thanks for all your help
Aehan

Re: User Form duplicates information when sub is cancelled by Doug

Doug
Wed Nov 21 11:34:11 PST 2007

Your validation code should be the first part of the routine that transfers
the data into the document so that nothing is transferred before it is run.

Another way around it however is to use document variables and docvariable
fields as you can set the value of the variable as many times as you want
and it is only the last value that it is set to that will be displayed in
the docvariable field.

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

"aehan" <aehan@discussions.microsoft.com> wrote in message
news:B0BA87E0-DD6B-4BE2-A589-425CEBB79D8B@microsoft.com...
> Hello all
>
> I have made a user form to populate a Word 2003 template. I have added a
> piece of code that exits the population of the document if the required
> fields aren't filled in, it reads like the sample below:
>
> If Len(Trim(cmbDiv.text)) <= 0 Or Len(Trim(cmbDept.text)) <= 0 Or
> Len(Trim(txtEMail.text)) <= 0 Then
> MsgBox "You must fill in all the required fields"
> Exit Sub
> ElectronicOption.Enabled = True
> End If
>
> The code works, however what happens is that if the code is exited the
> form
> has already placed some data in the template, and it duplicates it. So
> instead of for example seeing the Department name as "Sales and
> Marketing",
> you see it as "Sales and Marketing Sales and Marketing". I thought I
> would
> have to write something like me.refresh before Exit Sub, but I can't find
> anything in Word vba like that, I tried Repaint, but that didn't work.
>
> I think the problem is that the data is already there, and when the rest
> of
> the fields are filled in and the OK button clicked, the fact that some of
> the
> data was already there is not recognised, and in that case I don't know
> what
> to do. Does anyone have any ideas?
>
> Thanks for all your help
> Aehan



Re: User Form duplicates information when sub is cancelled by aehan

aehan
Wed Nov 21 12:37:01 PST 2007

Thank you, it's now working perfectly. Your help is really appreciated.
Aehan

"Doug Robbins - Word MVP" wrote:

> Your validation code should be the first part of the routine that transfers
> the data into the document so that nothing is transferred before it is run.
>
> Another way around it however is to use document variables and docvariable
> fields as you can set the value of the variable as many times as you want
> and it is only the last value that it is set to that will be displayed in
> the docvariable field.
>
> --
> 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
>
> "aehan" <aehan@discussions.microsoft.com> wrote in message
> news:B0BA87E0-DD6B-4BE2-A589-425CEBB79D8B@microsoft.com...
> > Hello all
> >
> > I have made a user form to populate a Word 2003 template. I have added a
> > piece of code that exits the population of the document if the required
> > fields aren't filled in, it reads like the sample below:
> >
> > If Len(Trim(cmbDiv.text)) <= 0 Or Len(Trim(cmbDept.text)) <= 0 Or
> > Len(Trim(txtEMail.text)) <= 0 Then
> > MsgBox "You must fill in all the required fields"
> > Exit Sub
> > ElectronicOption.Enabled = True
> > End If
> >
> > The code works, however what happens is that if the code is exited the
> > form
> > has already placed some data in the template, and it duplicates it. So
> > instead of for example seeing the Department name as "Sales and
> > Marketing",
> > you see it as "Sales and Marketing Sales and Marketing". I thought I
> > would
> > have to write something like me.refresh before Exit Sub, but I can't find
> > anything in Word vba like that, I tried Repaint, but that didn't work.
> >
> > I think the problem is that the data is already there, and when the rest
> > of
> > the fields are filled in and the OK button clicked, the fact that some of
> > the
> > data was already there is not recognised, and in that case I don't know
> > what
> > to do. Does anyone have any ideas?
> >
> > Thanks for all your help
> > Aehan
>
>
>