Results 1 to 9 of 9

Thread: Inserting value from dropdown into bookmark

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2005
    Location
    SoCal
    Posts
    54

    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?

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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:
    1. 'Behind UserForm1
    2. Private Sub CommandButton1_Click()
    3.     ActiveDocument.Bookmarks("Test").Range.Text = ActiveDocument.Bookmarks("Test").Range.Text & " " & UserForm1.ComboBox1.Text
    4. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2005
    Location
    SoCal
    Posts
    54

    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:
    1. Dim vStatus As String
    2.     Dim vStatSelect As Integer
    3.    
    4.     vStatSelect = ActiveDocument.FormFields("Status").DropDown.Value
    5.  
    6.     Select Case vStatSelect
    7.    
    8.      Case 2
    9.           ActiveDocument.Bookmarks("StatusP").Range.Text = "single person"
    10.          
    11.      Case 3
    12.           ActiveDocument.Bookmarks("StatusP").Range.Text = "widow"
    13.          
    14.      Case 4
    15.           ActiveDocument.Bookmarks("StatusP").Range.Text = "widower"
    16.      
    17.      End Select
    18.  
    19.    
    20. 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)

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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.
    Last edited by RobDog888; May 3rd, 2005 at 04:31 PM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2005
    Location
    SoCal
    Posts
    54

    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?

  6. #6

    Thread Starter
    Member
    Join Date
    Mar 2005
    Location
    SoCal
    Posts
    54

    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?

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    Member
    Join Date
    Mar 2005
    Location
    SoCal
    Posts
    54

    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?

  9. #9

    Thread Starter
    Member
    Join Date
    Mar 2005
    Location
    SoCal
    Posts
    54

    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:
    1. Sub StatusM()
    2. '
    3. ' StatusM Macro
    4. ' Macro recorded 4/29/2005 by Heather Lore
    5. '
    6. ' If the value of the dropdown is "single" then the areas
    7. ' in the document that this should populate will also include
    8. ' the text " person"
    9.  
    10.    
    11.     Dim vStatSelect As Integer
    12.     Dim vStat As String
    13.    
    14.     vStatSelect = ActiveDocument.FormFields("Status").DropDown.Value
    15.  
    16.     ActiveDocument.Unprotect
    17.        
    18.     Select Case vStatSelect
    19.    
    20.      Case 2
    21.           vStat = "single person"
    22.          
    23.      Case 3
    24.           vStat = "widow"
    25.          
    26.      Case 4
    27.           vStat = "widower"
    28.      
    29.      End Select
    30.      
    31.     Selection.GoTo What:=wdGoToBookmark, Name:="StatusP"
    32.     Selection.Delete Unit:=wdCharacter, Count:=1
    33.     Selection.InsertAfter vStat
    34.     ActiveDocument.Bookmarks.Add Range:=Selection.Range, _
    35.     Name:="StatusP"
    36.  
    37.      
    38.      ActiveDocument.Protect Type:=wdAllowOnlyFormFields

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width