Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-18-2014, 04:21 PM
Mooncat Mooncat is offline Word 2007 VBA Combobox Conditional text problem Windows 7 32bit Word 2007 VBA Combobox Conditional text problem Office 2007
Novice
Word 2007 VBA Combobox Conditional text problem
 
Join Date: Mar 2014
Posts: 3
Mooncat is on a distinguished road
Default Word 2007 VBA Combobox Conditional text problem


Hi all,
I'm having problems using a combobox to display text at a bookmark in Word 2007 and would be grateful for any clues

The code I have so far allows the user to select from the combobox and have the selected item text appear within a string at a bookmark when I run the code.
But I would like the user to be able to enter any word into the combobox and have that entry appear within the bookmark.

Example:
I have a combobox with the options Red, Green and Blue.
If pick one of these, the selected one appears at the bookmark,
however I don't know what to do if they should manually type another colour, say orange, into the combobox.

Any Ideas ?

Here is the code so far:
Code:
Sub testCombobox()
Dim objCC As ContentControl
Dim objLE As ContentControlListEntry
Dim isel As String
Set objCC = ActiveDocument.ContentControls(1)
For i = 1 To objCC.DropdownListEntries.Count
  If objCC.DropdownListEntries.Item(i).Text = objCC.Range.Text Then
    Set objLE = objCC.DropdownListEntries.Item(i)
    If objLE.Text = "Red" Then
      isel = " red"
    ElseIf objLE.Text = "Green" Then
      isel = " green"
    ElseIf objLE.Text = "Blue" Then
  isel = "blue"
    End If
  End If
Next i
ActiveDocument.Bookmarks("picked").Range.InsertAfter "You picked " & isel
End Sub
Thanks for reading
Regards,
Dan

Last edited by macropod; 03-18-2014 at 04:35 PM. Reason: Added code tags & formatting
Reply With Quote
  #2  
Old 03-18-2014, 04:35 PM
fumei fumei is offline Word 2007 VBA Combobox Conditional text problem Windows 7 64bit Word 2007 VBA Combobox Conditional text problem Office XP
Expert
 
Join Date: Jan 2013
Posts: 440
fumei is on a distinguished road
Default

Please use code tags when posting code. Could you not just use the value directly
Code:
ActiveDocument.Bookmarks("picked").Range.InsertAfter "You picked " & objCC.Range.Text


BTW: this wipes out the bookmark. You can make it so you can replace the bookmark value.
Reply With Quote
  #3  
Old 03-18-2014, 05:05 PM
macropod's Avatar
macropod macropod is offline Word 2007 VBA Combobox Conditional text problem Windows 7 32bit Word 2007 VBA Combobox Conditional text problem Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,340
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Do you want the new item added to the dropdown list, rejected, or just permitted and output to the bookmark?

As for the basic code to update the bookmark, try the following ContentControlOnExit macro:
Code:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
With ContentControl
 If .Title = "MyCtrl" Then Call UpdateBookmark("picked", .Range.Text)
End With
End Sub
 
Sub UpdateBookmark(StrBkMk As String, StrTxt As String)
Dim BkMkRng As Range
With ActiveDocument
 If .Bookmarks.Exists(StrBkMk) Then
   Set BkMkRng = .Bookmarks(StrBkMk).Range
   BkMkRng.Text = StrTxt
   .Bookmarks.Add StrBkMk, BkMkRng
 End If
End With
Set BkMkRng = Nothing
End Sub
The code assumes your Content Control has the "MyCtrl" title.

PS: When posting code, please use the code tags. They're on the 'Go Advanced' tab.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #4  
Old 03-18-2014, 07:31 PM
Mooncat Mooncat is offline Word 2007 VBA Combobox Conditional text problem Windows 7 32bit Word 2007 VBA Combobox Conditional text problem Office 2007
Novice
Word 2007 VBA Combobox Conditional text problem
 
Join Date: Mar 2014
Posts: 3
Mooncat is on a distinguished road
Default

Thanks guys, sorry about the lack of code tags !

It turns out they won't need to enter any data into the combobox, it will be sufficient just to have a none of the above option.
I can then use conditions to select which text shows up at the bookmark, it won't actually need to be the text that's selected in the box, just some disclaimer stuff. So I think I have it worked out now.

Thanks macropod for the bookmark updating code, I hadn't tackled that yet as I was focused on the first problem so that will be very educational.

Cheers guys, thanks for your help !
Regards,
Dan
Reply With Quote
  #5  
Old 03-18-2014, 08:02 PM
macropod's Avatar
macropod macropod is offline Word 2007 VBA Combobox Conditional text problem Windows 7 32bit Word 2007 VBA Combobox Conditional text problem Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,340
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by Mooncat View Post
It turns out they won't need to enter any data into the combobox
In that case, don't use a combobox - use a dropdown list instead.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 03-18-2014, 11:51 PM
Mooncat Mooncat is offline Word 2007 VBA Combobox Conditional text problem Windows 7 32bit Word 2007 VBA Combobox Conditional text problem Office 2007
Novice
Word 2007 VBA Combobox Conditional text problem
 
Join Date: Mar 2014
Posts: 3
Mooncat is on a distinguished road
Default

Yep dropdown list will be fine,
Thanks again
Cheers
Dan
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Working with Dropdown/Combobox in word 2007 lucky16 Word 0 01-26-2014 08:11 AM
Word 2007 VBA Combobox Conditional text problem Populating ComboBox or Drop Down list with contents of a text field Billy_McSkintos Word VBA 1 09-13-2011 05:50 AM
Conditional Formatting Problem Excel 2007 namedujour Excel 6 08-04-2011 10:52 AM
Word 2007 VBA Combobox Conditional text problem Array into ComboBox + Macro-Text into ActiveDocument Vivi Word VBA 1 01-27-2010 07:03 AM
Conditional Text Adding - Word 2003 chuck Mail Merge 0 06-17-2009 08:09 AM

Other Forums: Access Forums

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