PDA

View Full Version : [SOLVED:] Add userform (text boxes) to word document



Iris
02-01-2015, 04:12 AM
Hi

I have created a user form based on text boxes for a letter document, the text appears in the document however the user form doesn't disappear and the document doesn't automatically show. I have been referring to different websites for code but can't work out the code I am missing for this to happen - any advice would be appreciated.

The attached document includes code and document.
12779

Thank you
Martine

gmayor
02-01-2015, 06:19 AM
This looks like part of a process I created for you recently. It doesn't work because the main module that makes it work is missing from the project.
The project that works is attached to my last post in the thread - http://www.vbaexpress.com/forum/showthread.php?51613-Run-Time-Error-94-Invalid-use-of-Null&p=320168#post320168
This document is very similar, but it does need all the code.
As I demonstrated in the earlier project, the code should go in the calling macro module, leaving minimal code in the userform itself.
Making this work is just a question of changing the name of the form and the fields to match what you have and remove the additional material that is not used.
The principle is going to be the same in any similar template you create, so use the earlier one as a proforma.
I have modified the this document. Compare it with the other one so you will see what I mean. It has a userform and a module.

Iris
02-05-2015, 02:57 AM
Hi

Thank you for your help again, I had tried to amend the original document however can now see that I was taking too much code out. Based on the original doc I have now created a third template however am now receiving the attached error message and am unable to ascertain how to resolve - could you please assist?

Also I have used the date picker field in the userform however the only way that I can get this too work is to have the checkbox value set as true - is there a better way to do this?

I also have an other box as part of the mulitple selection (Special Loan Features) and I have an adjacent text box for further details, is it possible to have the adjacent text box only appear if the the other text box is selected? I was wondering if I could do this through 'if' statements, just wanted to know if I was on the right track?

Thank you again, any advise is very much appreciated.

gmayor
02-05-2015, 05:46 AM
You are getting there :) The error occurred because you used 'end with' in the wrong place in your main code. The problem with the date picker is that you are trying to write null values to the date picker when reading values from the document that don't exist. You must either set the value to a date e.g. Date which will apply today's date, or, when loading the form with the values from the document, only load them when there is a value in the bookmark.

To select an item in a list box from a value in the document bookmark you need to compare each item in the list with the value in the bookmark and set the listindex of the field to the position in the list. There are now two example of that in your code.

You can certainly hide and show elements according to values by using the field's change event

e.g.


Private Sub Fieldname_Change()
If Not Me.Fieldname.Text = "" then 'There is something in the field so...
Me.someotherfield.Visible = True
Else 'There is nothing in the field
Me.someotherfield.Visible = False
End if
End SubSet the initial value of the field to Visible = False

Iris
02-08-2015, 07:36 PM
Thank you again for your help and advise.