PDA

View Full Version : Save to Field and Remember



nickirvine
05-13-2010, 03:42 AM
Hi,

I wonder if anyone can help me.

I'm trying to get a form in word vb.

The idea is you select an option from a dropdown, you press enter, it saves the document and remembers what you entered so next time you don't have to do it again. So next time you are sent to a different form and bypass the dropdown.

This is what I have so far, but it doesn't remember it, when I open a second time its forgot.

This is the button at the end of the form:

Private Sub CommandButton1_Click()
' check that a site is selected
If ComboBox1.Value > "" Then

' if yes then input and save
SavedSite.Value = ComboBox1.Value
SavedSite.Text = ComboBox1.Value
ActiveDocument.Save

' then go to next form
SelectSite.Hide
SelectOption.Show
Else

' if no then ask them to select
MsgBox "Please Select Your Site.", _
vbExclamation, _
"Site Select"
End If
End Sub

Then I have this code:

Private Sub UserForm_Initialize()
If SavedSite.Text > "" Then
SelectSite.Hide
SelectOption.Show
Else
End If
End Sub

Am i doing this the long way round? I'm using Word 2003

Paul_Hossler
05-13-2010, 09:06 AM
You could save it in Custom Document Property or a Variable (I think that was in 2003, but not sure)



Variables Collection Object
A collection of Variable (http://vbaexpress.com/forum/ms-help://MS.WINWORD.DEV.12.1033/WINWORD.DEV/content/HV10058648.htm) objects that represent the variables added to a document or template. Document variables are used to preserve macro settings in between macro sessions.


Paul

Tinbendr
05-14-2010, 05:40 AM
The field info in a userform is not stored. As Paul stated, you'll have to do that manually.

I like using the built-in document properties. (title, subject, author) That way, I can manually change to the value if needed.

ActiveDocument.BuiltInDocumentProperties(wdPropertySubject) = userform1.listbox1.listindex

fumei
05-17-2010, 11:26 AM
And I prefer to use a Document Variable. Technically I suppose there is no serious difference. Except: BuiltInDocumentProperties are visible to the users, VARIABLES are not.

BTW:
If ComboBox1.Value > "" Then
is a scary use of code. It works solely on whether you used a ListIndex = x value in your originating code.

If you DID (and this is best practice) then .Value will always be greater than "". If you did not, then it will not.

Also, regarding using ListIndex, as a value, to go into Subject (the example of BuiltInDocumentProperty), it should be remembered that ListIndex returns a number, not text.