Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / July 2007

Tip: Looking for answers? Try searching our database.

ListBox and selected results

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
corky_guy@yahoo.com - 08 Jul 2007 19:43 GMT
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!
Russ - 09 Jul 2007 07:29 GMT
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.

> Hey there --
>
[quoted text clipped - 29 lines]
>
> Thanks a lot!

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

corky_guy@yahoo.com - 09 Jul 2007 15:03 GMT
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
Russ - 09 Jul 2007 23:16 GMT
Cork,
> Hi Russ -
>
[quoted text clipped - 4 lines]
>  .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.

> Thank you for your reply!
>
> cork

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

corky_guy@yahoo.com - 09 Jul 2007 23:32 GMT
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?
Russ - 09 Jul 2007 23:51 GMT
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.

> I didn't type it in either. I used INSERT | FIELD | DOCVARIABLE.
>
[quoted text clipped - 6 lines]
>
> Good thing I'm in the beginners forum, eh?

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Russ - 10 Jul 2007 00:01 GMT
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.
> Corky,
>
[quoted text clipped - 19 lines]
>>
>> Good thing I'm in the beginners forum, eh?

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

corky_guy@yahoo.com - 10 Jul 2007 01:33 GMT
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?
corky_guy@yahoo.com - 10 Jul 2007 02:46 GMT
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!
Russ - 10 Jul 2007 04:54 GMT
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"

> OK, I got it.   I changed:
>
[quoted text clipped - 15 lines]
>
> ugh -- thanks for all of the help, Russ!

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.