Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-19-2015, 10:19 PM
scarda scarda is offline Linking Combobox to multiple bookmarks Windows 8 Linking Combobox to multiple bookmarks Office 2010 64bit
Novice
Linking Combobox to multiple bookmarks
 
Join Date: Jan 2015
Posts: 5
scarda is on a distinguished road
Default Linking Combobox to multiple bookmarks

I have set up numerous combo-boxes on my Userform. I connected the combo-box to the relevant bookmark in the document. However there are other bookmarks that I want to populate, based on the combo-box selection but with different wording.


As an example, I have a series of town names each with their own unique description. So when I select a particular town, the description will populate that particular bookmark.
Does anyone have suggestions on what sort of code to use?
Thank you in anticipation
Reply With Quote
  #2  
Old 01-20-2015, 04:10 AM
gmayor's Avatar
gmayor gmayor is offline Linking Combobox to multiple bookmarks Windows 7 64bit Linking Combobox to multiple bookmarks Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Call the following sub to fill your bookmarks with the data required. You can call it from your userform e.g. as follows, replacing the bookmark names and values as appropriate.

Code:
Select Case Me.ComboBoxName.ListIndex
        Case 0
               FillBM "Bookmarkname", "Value to put in bookmark"
               FillBM "AnotherBookmarkname", "Another value to put in bookmark"
         'etc
     Case 1
               FillBM "Bookmarkname", "Value to put in bookmark"
               FillBM "AnotherBookmarkname", "Another value to put in bookmark"
         'etc
     'Repeat for each list index as required
End Select
Code:
Public Sub FillBM(strBMName As String, strValue As String)
Dim orng As Range
    With ActiveDocument
        On Error GoTo lbl_Exit
        Set orng = .Bookmarks(strBMName).Range
        orng.Text = strValue
        orng.Bookmarks.Add strBMName
    End With
lbl_Exit:
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 01-20-2015, 07:15 AM
gmaxey gmaxey is offline Linking Combobox to multiple bookmarks Windows 7 32bit Linking Combobox to multiple bookmarks Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,598
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Graham has shown you one way. I'm going to Shanghai part of his code to show you another. If you define all of your variables in the combobox (multi-column) you can do it like this:

Code:
Private Sub UserForm_Initialize()
  With Me.ComboBox1
    .AddItem
    .List(.ListCount - 1, 0) = "Cleveland"
    .List(.ListCount - 1, 1) = "A city in northwestern Ohio, USA"
    .AddItem
    .List(.ListCount - 1, 0) = "Cincinnati"
    .List(.ListCount - 1, 1) = "A city in southwestern Ohio, USA"
  End With
End Sub
Private Sub CommandButton1_Click()
  On Error GoTo Err_UserDefined
   FillBM "bmCityName", Me.ComboBox1.Column(0)
   FillBM "bmDescription", Me.ComboBox1.Column(1)
lbl_Exit:
   Unload Me
   Exit Sub
Err_UserDefined:
  FillBM "bmCityName", Me.ComboBox1.Value
  FillBM "bmDescription", "Not defined"
  Resume lbl_Exit
End Sub
Public Sub FillBM(strBMName As String, strValue As String)
Dim oRng As Range
  With ActiveDocument
    On Error GoTo lbl_Exit
    Set oRng = .Bookmarks(strBMName).Range
    oRng.Text = strValue
    oRng.Bookmarks.Add strBMName
  End With
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #4  
Old 01-20-2015, 08:50 AM
gmayor's Avatar
gmayor gmayor is offline Linking Combobox to multiple bookmarks Windows 7 64bit Linking Combobox to multiple bookmarks Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

If you are creating the combobox, then Greg's approach works well, but seeing that he has improved on my suggestion, I'll return the favour.

I would set the combobox to display only the first column, by setting the column count (here 2, but you can have more colums as required) and making all but the first column have a width of zero. As it is a combobox and not a list box, I would also add a prompt at the top of the list and set the listindex to that prompt i.e. 0
Code:
Private Sub UserForm_Initialize()
    With Me.ComboBox1
        .ColumnCount = 2
        .ColumnWidths = .Width & ",0"
        .AddItem
        .list(.ListCount - 1, 0) = "[Select City]"
        .list(.ListCount - 1, 1) = "" 
        .AddItem
        .list(.ListCount - 1, 0) = "Cleveland"
        .list(.ListCount - 1, 1) = "A city in northwestern Ohio, USA"
        .AddItem
        .list(.ListCount - 1, 0) = "Cincinnati"
        .list(.ListCount - 1, 1) = "A city in southwestern Ohio, USA"
        .ListIndex = 0
    End With
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #5  
Old 01-20-2015, 10:48 PM
scarda scarda is offline Linking Combobox to multiple bookmarks Windows 8 Linking Combobox to multiple bookmarks Office 2010 64bit
Novice
Linking Combobox to multiple bookmarks
 
Join Date: Jan 2015
Posts: 5
scarda is on a distinguished road
Default

Thanks Graham and Greg it's much appreciated, I used a combination of both your codes to work this one out.
Reply With Quote
Reply

Tags
bookmarks, combobox



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linking Combobox to multiple bookmarks Form updating Bookmarks - writes to the bookmarks multiple times PeterPlys Word VBA 13 01-14-2015 06:41 AM
Linking Combobox to multiple bookmarks Linking to Bookmarks savo Word 6 04-11-2013 11:04 AM
Linking Combobox to multiple bookmarks VBA code to extract specific bookmarks from multiple word files Rattykins Word VBA 4 06-27-2012 10:02 PM
Linking multiple choice questions to answers Microsoftenquirer1000 Word 1 06-11-2012 06:53 AM
Linking Combobox to multiple bookmarks linking title across multiple documents Foxtrot75 Word 1 03-02-2012 03:31 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:26 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft