ListBox and selected results

C

corky_guy

Hey there --

I'm having trouble turning a users selection from a list box into
something else. So, if the user selects MAN then I want the doc to
return 1001 into the document. If the user selects FEM then i want
the doc to return 2002. Here's what I have, but it's not working
right:



Private Sub CommandButton1_Click()
Dim dog As String

Select Case ListBox1.Value
Case 0 = MAN
dog = "1001"
Case 1 = FEM
dog = "2002"
End Select

With ActiveDocument
.Variables("result") = dog
.Fields.Update
End With
Me.hide
End Sub
Private Sub UserForm_Initialize()
With ListBox1
.AddItem " "
.AddItem "MAN"
.AddItem "FEM"
End With
End Sub


Thanks a lot!
 
R

Russ

Hey Corky,
All you have succeeded to do is store the dog value into a document
variable.
You must either use code to set a document .textbox"myTextBoxUseFullName" =
..Variables("result"), if you're using formfield document; or use code to
create a field in your document or have one setup already in a template that
refers to that .Variables("result")
This code will put it a selected area or insertion point. Code similar to
this could put a field into a named bookmarked range area in the document.
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"DOCVARIABLE ""result""", PreserveFormatting:=True
The field code will end up looking like: ALT/F9 to toggle code and results
{ DOCVARIABLE "result" \* MERGEFORMAT }
You can type in the fieldcode manually, if you use CTRL/F9 to make the field
braces because those aren't normal braces.
You can add that same code in multiple locations in your document or
template all referring to the same doc variable.
 
C

corky_guy

Hi Russ -

Thank you for the reply. I apologize, but I thought called the
variable (and loaded it into the document) by use of:

.Variables("result") = dog
.Fields.Update
End With

That's how I usually load the variable and it seems to work fine for
other docs. It's not working for this one, though.

Thank you for your reply!

cork
 
R

Russ

Cork,
Hi Russ -

Thank you for the reply. I apologize, but I thought called the
variable (and loaded it into the document) by use of:

.Variables("result") = dog
.Fields.Update
End With
Yes, that will work. Provided the document is opened with a template that
has the fields already setup or you use code elsewhere to set them up. If
there are no fields in the document, then using .Fields.Update is not doing
anything useful.
That's how I usually load the variable and it seems to work fine for
other docs. It's not working for this one, though.
Apparently those docs had the fields already set up in them, by way of a
Global template or attached template or other VBA code, like I suggested in
my last message.
 
C

corky_guy

Yeah, see, that's what I don't understand. Here's what's in my
document:

{ DOCVARIABLE result \* MERGEFORMAT }

I didn't type it in either. I used INSERT | FIELD | DOCVARIABLE.

Whenever I execute the code, I receive: Error! No document variable
supplied.

Very odd.
Could it have anything to do with the way I am storing the variable as
string?

Good thing I'm in the beginners forum, eh?
 
R

Russ

Corky,
Yeah, see, that's what I don't understand. Here's what's in my
document:

{ DOCVARIABLE result \* MERGEFORMAT }
{ DOCVARIABLE "result" \* MERGEFORMAT }
In my first message to you, the above line is what I said you should see in
your exposed field code.
The difference being the double quotation marks, of course. It wants the
docvariable name quoted. You are correctly storing the variable value as a
string.
 
R

Russ

Corky,
FYI:
There are functions in VBA to change a string to a number or date, or vice
versa, if you need to. In VBA help search for:
Type Conversion Functions

I'm not saying that you need to, now. Just so you'll know.
 
C

corky_guy

Thanks Russ.

I'm still stumped:


{ DOCVARIABLE result \* MERGEFORMAT } is exactly how the docvariable
is listed in my document.
The VBA code (as seen above) is trying to
populate: .Variables("result") = dog

What am I missing here?
 
C

corky_guy

OK, I got it. I changed:

Select Case ListBox1
Case MAN
dog = "1.1.1.1"
Case FEM
dog = "2.2.2.2"
End Select

to

Select Case ListBox1
Case "MAN"
dog = "1.1.1.1"
Case "FEM"
dog = "2.2.2.2"
End Select

ugh -- thanks for all of the help, Russ!
 
R

Russ

Corky,
I guess I didn't look over your code that closely when you said the same
code worked in other documents. So I thought the difference was in the new
document fields or new document lack of fields. And I assumed your MAN and
FEM were variables declared as strings or numbers.

I'm glad you worked it out.

Two things that might help in the future for debug purposes.
1. In the (DECLARATIONS) part of your code of each macro project or userform
code you use, you should always put the line:

Option Explicit

That statement tells the debugger to warn you when you try to use a variable
that is not declared. The debugger would have warned you that no variables
MAN or FEM were declared; like for example:
Dim MAN as String
or
Dim MAN as Integer
etc.

2. Use Case Else to warn you if there are no matches and the Case statements
fall through to the end.
Case Else
MsgBox "No Match Found"
 

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