Inserting value from dropdown into bookmark
I want to take the value selected from a dropdown and insert it elsewhere in the text. I have added a second bookmark so as not to change the value of the original because if the value is "single" I will concatenate it to be "single person". Elsewise it remain as is.
Do I need to cast somehow in order to put a formfield variable value into a bookmark/string/range?? variable value?
Re: Inserting value from dropdown into bookmark
I take it that this is a Word UserForm.
Upon the combo clikc or a command button click event you can append the value in the bookmark with the selection in the combo.
Its easier to do this upon a command button click.
VB Code:
'Behind UserForm1
Private Sub CommandButton1_Click()
ActiveDocument.Bookmarks("Test").Range.Text = ActiveDocument.Bookmarks("Test").Range.Text & " " & UserForm1.ComboBox1.Text
End Sub
Re: Inserting value from dropdown into bookmark
I'm sorry I wasn't more specific. Yes, this is Word 2003. And I'm not using the dropdown from a user form (this time). I ended up doing this:
VB Code:
Dim vStatus As String
Dim vStatSelect As Integer
vStatSelect = ActiveDocument.FormFields("Status").DropDown.Value
Select Case vStatSelect
Case 2
ActiveDocument.Bookmarks("StatusP").Range.Text = "single person"
Case 3
ActiveDocument.Bookmarks("StatusP").Range.Text = "widow"
Case 4
ActiveDocument.Bookmarks("StatusP").Range.Text = "widower"
End Select
End Sub
But I get an error on the ActiveDocument.Bookmarks... line & I don't have a clue why. I've also tried ActiveDocument.Bookmarks.("StatusP").Range.InsertAfter "single person"
There's something I'm missing here (like a course in VBA)
Re: Inserting value from dropdown into bookmark
Verify that your bookmarks are named correctly and exist.
If you .InsertAfter then it not really "in" the bookmark but rather after the location of the bookmark.
Re: Inserting value from dropdown into bookmark
If I see the bookmark when I'm within the document & go to Insert Bookmark, then am I assured it exists? Is it possible it has a space in the name that might be throwing it off? Is there a technique to creating the bookmark and placing it in the text that I should be following to ensure it existence?
Re: Inserting value from dropdown into bookmark
Actually, now that I pay closer attention to the error that is generated before I went into Debug Mode, it says
Run Time Error 6124:
You are not allowed to edit this region because document protection is in effect.
Did I set this up in a way that is locking the form from playing the macros? If so, how do I correct this?
Re: Inserting value from dropdown into bookmark
Go to Tools > Protect Document...
And check out any protection that may be turned on. If not then it could be that the form is in design mode and not
run mode when you are trying to run the macro code.
Re: Inserting value from dropdown into bookmark
I do have protection turned on for the section that contains the dropdowns & checkboxes so that they will be enabled to limit editing to filling in forms. It would seem counterproductive if this in turn disables the macros that those features run when they cannot be enabled without the document protection. I've seen something about using XML or Active X or (some such thing) checkboxes & dropdowns, however, that allow the document to be unprotected and still have these features enabled. Is this the direction I should be taking instead, or is there something else that I'm missing about the standard features?
Re: Inserting value from dropdown into bookmark
After playing around with this, if I write into my code to unprotect the document for the duration of the macro, it updates the bookmarks properly. However, then it resets the dropdown to the default one. If I take the unprotect code out of the document, the dropdown keeps the value selected, but I get the debug error that I cannot edit the document because it is protected. However, I have the document divided into 2 sections: the top part where the options are is protected & the bottom part where the text to be affected by the option is unprotected. What do I need to do to make my selection stick but still allow the document to be modified by the macros run in conjunction with the dropdowns?
VB Code:
Sub StatusM()
'
' StatusM Macro
' Macro recorded 4/29/2005 by Heather Lore
'
' If the value of the dropdown is "single" then the areas
' in the document that this should populate will also include
' the text " person"
Dim vStatSelect As Integer
Dim vStat As String
vStatSelect = ActiveDocument.FormFields("Status").DropDown.Value
ActiveDocument.Unprotect
Select Case vStatSelect
Case 2
vStat = "single person"
Case 3
vStat = "widow"
Case 4
vStat = "widower"
End Select
Selection.GoTo What:=wdGoToBookmark, Name:="StatusP"
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.InsertAfter vStat
ActiveDocument.Bookmarks.Add Range:=Selection.Range, _
Name:="StatusP"
ActiveDocument.Protect Type:=wdAllowOnlyFormFields