One ComboBox for several Bookmarks

S

Seapup

In my form (table style) I keep re-creating my dropdown list for each bookmark
how can I use the same drop down list for each different bookmark
 
A

alborg

Hi Seapup:

Let me get this straight- by "each bookmark" do you mean each bookmark in a
new cell row in your table?

Original forum codeset is in the thread "Add new row in Table + new set of
Form Fields" located in this list-
http://www.microsoft.com/office/com...oc=en-us&dg=microsoft.public.word.vba.general.

I have a "charge capture template" that I give out to fellow physicians that
pretty much does that, but better. I find that this version flows better...
download the template from here:
http://www.box.net/shared/static/1pdpez2g4k.zip and click on one of the 8
buttons to table fill dates and formfields. This is the code used-

Sub checkaddrow()
Dim rownum As Integer, I As Integer, ii As Integer
response = MsgBox("Add new rows?", vbQuestion + vbYesNo)
If response = vbYes Then
ii = 0
If IsNull(ActiveDocument.FormFields("admdate1").Result) Then
Do Until ii > 18
ActiveDocument.Unprotect
ActiveDocument.Tables(1).Rows.Add
rownum = ActiveDocument.Tables(1).Rows.Count
For I = 1 To ActiveDocument.Tables(1).Columns.Count
ActiveDocument.FormFields.Add Range:=ActiveDocument.Tables(1).Cell(rownum,
I).Range, Type:=wdFieldFormTextInput
Next I
ActiveDocument.Tables(1).Cell(ActiveDocument.Tables(1).Rows.Count,
ActiveDocument.Tables(1).Columns.Count - 1).Range.FormFields(1).Result =
DateAdd("d", ii, ActiveDocument.FormFields("admdate1").Result)
ActiveDocument.Tables(1).Cell(ActiveDocument.Tables(1).Rows.Count,
ActiveDocument.Tables(1).Columns.Count).Range.FormFields(1).EntryMacro =
"addinfo"
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True
ii = ii + 1
Loop
Else
MsgBox "Please fill in the admission date first!", vbInformation
End If
End If
End Sub

If you want to substitute a dropdown object, just substitute this phrase-

Selection.FormFields.Add Range:=Selection.Range, Type:=wdFieldFormDropDown

Is this what you wanted?

Cheers,
AL
 
D

Doug Robbins - Word MVP

I would suggest that instead of using a DropDown formfield at each location,
you use a Text Formfield and have and Macro that runs on entry to the
formfield display a userform that contains the combobox with a command
button which when clicked, sets the result of the entered formfield to the
item that is selected in the combobox.

The way in which this is done is given in the Knowledge Base Article at
http://support.microsoft.com/kb/q198561/

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

Seapup

Thanks Alborg

I can't follow what your trying to teach me, I'm extremely new at this with
no programing experience. I'm working in Word 2002. I have this ComboBox
macro that has an array (text value). I'd like to use this combobox macro on
many bookmark cell in my table form. It works good when it sassign to only
one bookmark. but I'd like to find a way to use the same macro for many
bookmarks. Its seem that it can only be assign to one bookmark.
 
G

Greg Maxey

Unless the code that you are using is a State secret is there any reason you
can't post it here with a detailed explanation of what it does now and what
it is that you want it to do?
 
S

Seapup

Thanks Doug Robbins

The article that you offered me is the one that I have been using to learn
on how to create a ComboBox macro. But as you can see the first line states
the bookmark name. how can I add more bookmarks without having to create a
whole new macro for each bookmark.

My programing level is beginer, working in Words 2002
 
S

Seapup

Here is the macro, as you can see there is several items in my array. and
this one is short. How can I use this macro for several bookmark cell in my
form template

---------------------------------------------------------------------------------
Private Sub ComboBox1_Change()

ActiveDocument.FormFields("PumpInletValve1").Result = ComboBox1.Value

End Su
----------------------------------------------------------------------------------
Private Sub UserForm_Initialize()

ComboBox1.ColumnCount = 1

'Load data into ComboBox

ComboBox1.List() = Array("Northline Piston FSGR450", "Northline Piston
FSGR460", "Akron Ball 7825", _
"Akron Ball 8825", "Akron Butterfly 7950", "Akron Butterfly 7960", "Hale
Butterfly 538-1560-20-0", _
"Triton Butterfly Valve", "Hasbra Butterfly 270-6", "Hasbra Butterfly 270-5")

End Su
---------------------------------------------------------------------------------------
Private Sub Cmdclose_Click()

Unload Me

End Sub
====================================================
 
G

Greg Maxey

What do you mean by "... several bookmark cell in my form template?"

I am going to try to guess (I wouldn't have to do this if you would provide
a detailed description from the beginning). I am guessing that you have
several formfields in you form (these are formfields not "several bookmark
cell") that you need to populate with a specific item. Since you have more
than 25 specific items you need to choose from you are unable to use a
dropdown field and you are trying to use a UserForm instead. The first
fromfield is bookmarked "PumpInletValve1" and the form is working for you.
You want to use the same form for another formfield. Lets call this one
"PumpDischargeValve1"

Fisrt you need to run an OnEntry macro when you enter each formfield:

Sub OnEntrySeaPupFields()
Dim oFrm As UserForm1
Set oFrm = New UserForm1
oFrm.Show
Set oFrm = Nothing
End Sub

Then you need to determine which FormField you are working with when the
form initializes and populate the ComboBox accordingly.

Option Explicit
Private pStr As String

Private Sub UserForm_Initialize()
Select Case FieldID 'Use a Function (shown below) to get the current
FormField name.
Case Is = "PumpInletValve1"
ComboBox1.ColumnCount = 1
ComboBox1.List() = Array("Northline Piston FSGR450", "Northline Piston
FSGR460", "Akron Ball 7825", _
"Akron Ball 8825", "Akron Butterfly 7950", "Akron Butterfly 7960", "Hale
Butterfly 538-1560-20-0", _
"Triton Butterfly Valve", "Hasbra Butterfly 270-6", "Hasbra Butterfly
270-5")
Case Is = "PumpDischargeValve1"
ComboBox1.List() = Array("A", "B", "C", "D", "E", "Etc.")
Case Else
'Do Nothing
End Select
End Sub

Function FieldID() As String
If Selection.FormFields.Count = 1 Then
FieldID = Selection.FormFields(1).Name
ElseIf Selection.FormFields.Count = 0 And Selection.Bookmarks.Count > 0 Then
FieldID = Selection.Bookmarks(Selection.Bookmarks.Count).Name
End If
pStr = FieldID
End Function

Finally you need to set the value of the current FormField based on the
selection in the ComboBox. Note you don't need a command button to close the
form:

Private Sub ComboBox1_Change()
ActiveDocument.FormFields(pStr).Result = ComboBox1.Value
Unload Me
End Sub
 
S

Seapup

Yes you are right I didn't explain myself properly, and not as an excuse, I'm
new at this so I don't know all the proper terms. Anyway I used your codes
and I can select from the ComboBox but when I go to close the Box I get an
error message "5491" and when I select DEBUG it hightlights this line

Private Sub ComboBox1_Change()Unload Me
End Sub

I have no clue on what to do for this problem
 
G

Greg Maxey

It appears that you have neglected to declare pStr at the module level.
Here is the complete code for your UserForm. Since you want to update text
field as you type in the ComboBox then I suppose you would need the Command
button. By adding Option Exlicit to your module you will be forced to
explicitly declare all variables.

Option Explicit
Private pStr As String

Private Sub ComboBox1_Change()
ActiveDocument.FormFields(pStr).Result = ComboBox1.Value
End Sub

Private Sub CommandButton1_Click()
Unload Me
End Sub

Private Sub UserForm_Initialize()
Select Case FieldID
Case Is = "PumpInletValve1"
ComboBox1.ColumnCount = 1
ComboBox1.List() = Array("Northline Piston FSGR450", "Northline Piston
FSGR460", "Akron Ball 7825", _
"Akron Ball 8825", "Akron Butterfly 7950", "Akron Butterfly 7960", "Hale
Butterfly 538-1560-20-0", _
"Triton Butterfly Valve", "Hasbra Butterfly 270-6", "Hasbra Butterfly
270-5")
Case Is = "PumpDischargeValve1"
ComboBox1.List() = Array("A", "B", "C", "D", "E", "Etc.")
Case Else
'Do Nothing
End Select
End Sub

Function FieldID() As String
If Selection.FormFields.Count = 1 Then
FieldID = Selection.FormFields(1).Name
ElseIf Selection.FormFields.Count = 0 And Selection.Bookmarks.Count > 0 Then
FieldID = Selection.Bookmarks(Selection.Bookmarks.Count).Name
End If
pStr = FieldID
End Function
 
G

Greg Maxey

Send me e-mail using the feedback link on my website and I will send you the
working file.
 
F

fumei via OfficeKB.com

Let's be clear about formfields verus bookmarks.

All formfields are bookmarks, but not all bookmarks are formfields.

Are you trying to use the ComboBox result, and put that result into REAL
bookmarks, or formfields?

If they are bookmarks, NOT formfields, you could do it like this.

Sub FillABookmark(strBM As String, strText As String)
Dim oRange As Word.Range
Set oRange = ActiveDocument.Bookmarks(strBM).Range
ActiveDocument.Bookmarks(strBM).Range.Text = strText
With oRange
.Collapse Direction:=wdCollapseEnd
.MoveEnd Unit:=wdCharacter, Count:=Len(strText)
End With
ActiveDocument.Bookmarks.Add strBM, Range:=oRange
End Sub


Private Sub ComboBox1_Change()
Dim var
Dim oBM As Bookmark
Dim BM_Names()
‘ set the array of bookmark names
BM_Names = Array("Northline Piston FSGR450", _
"Northline Piston FSGR460", "Akron Ball 7825", _
"Akron Ball 8825", "Akron Butterfly 7950", _
"Akron Butterfly 7960", "Hale Butterfly 538-1560-20-0", _
"Triton Butterfly Valve", "Hasbra Butterfly 270-6", _
"Hasbra Butterfly 270-5")

‘ loop through every bookmark and make the range
‘ the text from ComboBox1

For Each oBM In ActiveDocument.Bookmarks
For var = 0 To Ubound(BM_Names)
Call FillABookmark (BM_Names(var), ComboBox1.Value)
Next
Unload Me
End Sub

That would put the ComboBox1 text value into every bookmark.

This is NOT the same as putting the value into every formfield.

Private Sub ComboBox1_Change()
Dim oFF As Formfield

‘ loop through every formfield and make the Result
‘ the text from ComboBox1

For Each oFF In ActiveDocument.Formfields
oFF.Result = ComboBox1.Value
Next
Unload Me
End Sub

This would make every formfield equal ComboBox1.Value. This is assuming that
every formfield is a text formfield. If some are not, then you must test to
see if it is a text formfield.

Can you explain more clearly? Are the things you are trying to put text into
formfieds – inserted from the Forms toolbar? Are they real bookmarks –
inserted through Insert > Bookmark?

Thanks Doug Robbins

The article that you offered me is the one that I have been using to learn
on how to create a ComboBox macro. But as you can see the first line states
the bookmark name. how can I add more bookmarks without having to create a
whole new macro for each bookmark.

My programing level is beginer, working in Words 2002
I would suggest that instead of using a DropDown formfield at each location,
you use a Text Formfield and have and Macro that runs on entry to the
[quoted text clipped - 8 lines]
 
D

Doug Robbins - Word MVP

Use

Selection.FormFields(1).Result =

when you want to transfer the selected item from the userform to the
document. That will transfer it into the TextBox FormField from which the
On Entry macro was run be the selection entering that FormField.

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

Seapup

Sorry for confusing everyone as I am with the terms. Yes they are FormFields.

fumei via OfficeKB.com said:
Let's be clear about formfields verus bookmarks.

All formfields are bookmarks, but not all bookmarks are formfields.

Are you trying to use the ComboBox result, and put that result into REAL
bookmarks, or formfields?

If they are bookmarks, NOT formfields, you could do it like this.

Sub FillABookmark(strBM As String, strText As String)
Dim oRange As Word.Range
Set oRange = ActiveDocument.Bookmarks(strBM).Range
ActiveDocument.Bookmarks(strBM).Range.Text = strText
With oRange
.Collapse Direction:=wdCollapseEnd
.MoveEnd Unit:=wdCharacter, Count:=Len(strText)
End With
ActiveDocument.Bookmarks.Add strBM, Range:=oRange
End Sub


Private Sub ComboBox1_Change()
Dim var
Dim oBM As Bookmark
Dim BM_Names()
‘ set the array of bookmark names
BM_Names = Array("Northline Piston FSGR450", _
"Northline Piston FSGR460", "Akron Ball 7825", _
"Akron Ball 8825", "Akron Butterfly 7950", _
"Akron Butterfly 7960", "Hale Butterfly 538-1560-20-0", _
"Triton Butterfly Valve", "Hasbra Butterfly 270-6", _
"Hasbra Butterfly 270-5")

‘ loop through every bookmark and make the range
‘ the text from ComboBox1

For Each oBM In ActiveDocument.Bookmarks
For var = 0 To Ubound(BM_Names)
Call FillABookmark (BM_Names(var), ComboBox1.Value)
Next
Unload Me
End Sub

That would put the ComboBox1 text value into every bookmark.

This is NOT the same as putting the value into every formfield.

Private Sub ComboBox1_Change()
Dim oFF As Formfield

‘ loop through every formfield and make the Result
‘ the text from ComboBox1

For Each oFF In ActiveDocument.Formfields
oFF.Result = ComboBox1.Value
Next
Unload Me
End Sub

This would make every formfield equal ComboBox1.Value. This is assuming that
every formfield is a text formfield. If some are not, then you must test to
see if it is a text formfield.

Can you explain more clearly? Are the things you are trying to put text into
formfieds – inserted from the Forms toolbar? Are they real bookmarks –
inserted through Insert > Bookmark?

Thanks Doug Robbins

The article that you offered me is the one that I have been using to learn
on how to create a ComboBox macro. But as you can see the first line states
the bookmark name. how can I add more bookmarks without having to create a
whole new macro for each bookmark.

My programing level is beginer, working in Words 2002
I would suggest that instead of using a DropDown formfield at each location,
you use a Text Formfield and have and Macro that runs on entry to the
[quoted text clipped - 8 lines]
bookmark
how can I use the same drop down list for each different bookmark
 

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