Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-09-2014, 05:55 AM
ksigcajun ksigcajun is offline Adding text to after a bookmark based on drop down menu choice Windows 7 64bit Adding text to after a bookmark based on drop down menu choice Office 2010 64bit
Advanced Beginner
Adding text to after a bookmark based on drop down menu choice
 
Join Date: May 2014
Posts: 76
ksigcajun is on a distinguished road
Default Adding text to after a bookmark based on drop down menu choice

Hey everyone!



I'm hoping to create a drop down menu with two choices (Low and Minimal) and if low is chosen it would add the following "Please delete all content for Benchmark team" after bookmark "BM2" in the document. If minimal is chosen, it wouldn't add anything and the space would be blank in the document.

Can anyone help with the code? This is what I have so far and its not adding the text where I want in the document.

Code:
Private Sub Paragraph_Addition
Dim StrDetails As String
With ContentControl
  If .Title = "Grade" Then
    Select Case .Range.Text
      Case "LOW"
        StrDetails = "Please delete all content for Benchmark team."
    Case "MINIMAL"
        StrDetails = ""
      Case Else: StrDetails = ""
    End Select
    ActiveDocument.ContentControls(2).Range.Text = StrDetails
  End If
End With
End Sub
Thanks!
Reply With Quote
  #2  
Old 06-09-2014, 06:28 AM
jpb103's Avatar
jpb103 jpb103 is offline Adding text to after a bookmark based on drop down menu choice Windows 7 64bit Adding text to after a bookmark based on drop down menu choice Office 2007
Advanced Beginner
 
Join Date: May 2014
Location: Thunder Bay, Ontario
Posts: 58
jpb103 is on a distinguished road
Default

You seem to be changing the caption of a content control. Is this intended? It seemed from the description of your problem that you simply wish to paste StrDetails at the location of the bookmark BM2. If you want to move the cursor to a bookmark location, you could use something like this:
Code:
Selection.GoTo What:=wdGoToBookmark, Name:="BM2"
Then, if you want to put that text on the next line after the bookmark, you could do something like this:
Code:
Selection.TypeParagraph
Selection.Range.Text = StrDetails
So if you replace this line:
Code:
ActiveDocument.ContentControls(2).Range.Text = StrDetails
with the three lines I've provided I think it should work.

Last edited by jpb103; 06-09-2014 at 06:40 AM. Reason: more efficient code
Reply With Quote
  #3  
Old 06-09-2014, 09:13 AM
gmaxey gmaxey is offline Adding text to after a bookmark based on drop down menu choice Windows 7 32bit Adding text to after a bookmark based on drop down menu choice Office 2010 (Version 14.0)
Word MVP 2003-2009
 
Join Date: May 2010
Location: Marble, NC
Posts: 1,033
gmaxey will become famous soon enoughgmaxey will become famous soon enough
Default

If you have Option Explicit at the top of your code pane you should be getting a “Compile Error” variable not defined on ContentControl.If you don’t then you must be getting a run-time error “424” on the If .Title = “Grade” Then line.

That, or there is more to your code than meets to the eye.

You mention bookmark BM2 in your post but there is no reference to a bookmark named “BM2” or anything else related to a bookmark in your code.


Code:
Private Sub Paragraph_Addition()
Dim strDetails As String
Dim oCC As ContentControl
Dim oRng As Range
  Set oCC = ActiveDocument.SelectContentControlsByTitle("Grade").Item(1)
    Select Case oCC.Range.Text
      Case "LOW"
        strDetails = "Please delete all content for Benchmark team."
      Case Else
        strDetails = vbNullString
    End Select
    'Write to bookmark
    If ActiveDocument.Bookmarks.Exists("BM2") Then
      Set oRng = ActiveDocument.Bookmarks("BM2").Range
      oRng.Text = strDetails
      ActiveDocument.Bookmarks.Add "BM2", oRng
    End If
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #4  
Old 06-09-2014, 10:42 AM
ksigcajun ksigcajun is offline Adding text to after a bookmark based on drop down menu choice Windows 7 64bit Adding text to after a bookmark based on drop down menu choice Office 2010 64bit
Advanced Beginner
Adding text to after a bookmark based on drop down menu choice
 
Join Date: May 2014
Posts: 76
ksigcajun is on a distinguished road
Default

Quote:
Originally Posted by gmaxey View Post
If you have Option Explicit at the top of your code pane you should be getting a “Compile Error” variable not defined on ContentControl.If you don’t then you must be getting a run-time error “424” on the If .Title = “Grade” Then line.

That, or there is more to your code than meets to the eye.

You mention bookmark BM2 in your post but there is no reference to a bookmark named “BM2” or anything else related to a bookmark in your code.


Code:
Private Sub Paragraph_Addition()
Dim strDetails As String
Dim oCC As ContentControl
Dim oRng As Range
  Set oCC = ActiveDocument.SelectContentControlsByTitle("Grade").Item(1)
    Select Case oCC.Range.Text
      Case "LOW"
        strDetails = "Please delete all content for Benchmark team."
      Case Else
        strDetails = vbNullString
    End Select
    'Write to bookmark
    If ActiveDocument.Bookmarks.Exists("BM2") Then
      Set oRng = ActiveDocument.Bookmarks("BM2").Range
      oRng.Text = strDetails
      ActiveDocument.Bookmarks.Add "BM2", oRng
    End If
End Sub
Thank you so much for this. It works perfectly. Maybe I'm just an idiot, but is there anyway that this code could update automatically?
Reply With Quote
  #5  
Old 06-09-2014, 06:23 PM
gmaxey gmaxey is offline Adding text to after a bookmark based on drop down menu choice Windows 7 32bit Adding text to after a bookmark based on drop down menu choice Office 2010 (Version 14.0)
Word MVP 2003-2009
 
Join Date: May 2010
Location: Marble, NC
Posts: 1,033
gmaxey will become famous soon enoughgmaxey will become famous soon enough
Default

You could adapt it and put it in the content control onexit event.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #6  
Old 06-10-2014, 05:40 AM
ksigcajun ksigcajun is offline Adding text to after a bookmark based on drop down menu choice Windows 7 64bit Adding text to after a bookmark based on drop down menu choice Office 2010 64bit
Advanced Beginner
Adding text to after a bookmark based on drop down menu choice
 
Join Date: May 2014
Posts: 76
ksigcajun is on a distinguished road
Default

Quote:
Originally Posted by gmaxey View Post
You could adapt it and put it in the content control onexit event.
Greg, is it not possible to have the update fresh automatcially? I would prefer a user to see the change instantly, rather on exiting and reopening the document.

Thanks!
Reply With Quote
  #7  
Old 06-10-2014, 09:39 AM
gmaxey gmaxey is offline Adding text to after a bookmark based on drop down menu choice Windows 7 32bit Adding text to after a bookmark based on drop down menu choice Office 2010 (Version 14.0)
Word MVP 2003-2009
 
Join Date: May 2010
Location: Marble, NC
Posts: 1,033
gmaxey will become famous soon enoughgmaxey will become famous soon enough
Default

It is possible if you want to learn how and employ a custom content control change event: http://gregmaxey.com/word_tip_pages/...om_events.html

If you could use a second content control rather that a bookmark then you could also adapt the code shown here:
https://www.msofficeforums.com/word-...rols-word.html
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding text to after a bookmark based on drop down menu choice Multiple Choice Drop Down List teza2k06 Word 1 04-29-2014 04:54 PM
Populate a table based on choices from drop down menu Xaand Word VBA 0 02-25-2014 11:08 AM
Adding text to after a bookmark based on drop down menu choice Design: Adding text choice christopherL Word 2 10-07-2013 11:22 PM
Adding text to after a bookmark based on drop down menu choice Bookmark applied to drop-down list doesn't do the job thecreaser Word 5 04-11-2013 04:21 AM
Adding Macro menu to the right click menu (Word 2007) masam123 Word 0 10-14-2011 04:05 AM

Other Forums: Access Forums - Senior Forums

All times are GMT -7. The time now is 10:23 PM.


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