Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-29-2021, 01:59 AM
caracoder caracoder is offline how to select a default value for a combo in a userform from value of bookmark in the word document Windows 10 how to select a default value for a combo in a userform from value of bookmark in the word document Office 2016
Novice
how to select a default value for a combo in a userform from value of bookmark in the word document
 
Join Date: Aug 2021
Posts: 1
caracoder is on a distinguished road
Question how to select a default value for a combo in a userform from value of bookmark in the word document

I am using a VBA user form in Microsoft Word to fill in fields in a document. I have this working fine for text fields but I having more difficulty with the combo boxes.

So I have a user form called DocumentFields, with a combo called cmbColour and a button called cmdSave.

When I open this userform I fill in options in the cmbColour fields as follows:

DocumentFields.Show vbModeless

With DocumentFields.cmbColour
.AddItem "Red"
.AddItem "Green"


.AddItem "Blue"
End With

This works fine and shows the three values

When the user clicks on the save button it takes the value of the cmbColour field and saves it into a bookmark called My_Colour in the document

Dim myColour As Range
Set myColour = ActiveDocument.Bookmarks("My_Colour").Range
myColour.Text = Me.cmbColour.Value
ActiveDocument.Bookmarks.Add "My_Colour", myColour

This also works fine

The problem is when I close the user form and then open it up again I want it to read the value that is in the bookmark, and select the correct field in cmbColour so it remembers which one was last selected.

i.e. If they previously had selected red, it would read in the value "red" from the bookmark field, loop through the cmbColours items and select the one that matched or none if there was no match.

I have tried lots of different code samples for this but none of them seem to work.
I think most of them seem to be written for excel but I don't seem to be able to get them to work in word.

Does anyone know how I can do this in Word? Any help would be most appreciated!
Reply With Quote
  #2  
Old 08-29-2021, 04:19 AM
gmayor's Avatar
gmayor gmayor is offline how to select a default value for a combo in a userform from value of bookmark in the word document Windows 10 how to select a default value for a combo in a userform from value of bookmark in the word document Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,138
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

In the userform code


Code:
Option Explicit

Private Sub btnSave_Click()
Dim oRng As Range
    If cmbColour.ListIndex >= 0 Then
        With ActiveDocument
            On Error GoTo lbl_Exit
            If .Bookmarks.Exists("My_Colour") = True Then
                Set oRng = .Bookmarks("My_Colour").Range
                oRng.Text = cmbColour.Value
                oRng.Bookmarks.Add "My_Colour"
            End If
        End With
    Else
        MsgBox "Select Colour", vbCritical
    End If
    Unload Me
lbl_Exit:
    Set oRng = Nothing
    Exit Sub
End Sub

Private Sub UserForm_Initialize()
Dim i As Integer
Dim sColour As String
    With ActiveDocument
        If .Bookmarks.Exists("My_Colour") = True Then
            sColour = .Bookmarks("My_Colour").Range.Text
        End If
    End With
    With cmbColour
        .AddItem "Red"
        .AddItem "Green"
        .AddItem "Blue"
        For i = 0 To .ListCount - 1
            If sColour = .List(i) Then
                .ListIndex = i
                Exit For
            End If
        Next i
    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
Reply

Tags
bookmarks reference field, combo box, default values



Similar Threads
Thread Thread Starter Forum Replies Last Post
UserForm with Combo Box from data in table within template jhansrod Word VBA 6 06-13-2019 09:02 AM
how to select a default value for a combo in a userform from value of bookmark in the word document Multi-select Combo Box with Item Descriptions SLAB8002 Excel Programming 3 05-30-2019 12:27 PM
Pasting a table with combo boxes to a new document looses the combo box bobsut@enviro-consult.com Word 1 01-03-2017 01:29 PM
Userform without inserting Bookmark text mktate Word VBA 8 12-31-2015 01:11 PM
how to select a default value for a combo in a userform from value of bookmark in the word document Moving Selected Items from a Multiselect Listbox on a userform to a bookmark in Word marksm33 Word VBA 3 01-15-2015 07:55 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:52 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