Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-11-2016, 07:16 PM
silverspr silverspr is offline Bookmark content controls Windows 8 Bookmark content controls Office 2010 64bit
Novice
Bookmark content controls
 
Join Date: Apr 2011
Posts: 24
silverspr is on a distinguished road
Default Bookmark content controls

HI I have 49ish content controls embedded in multiple tables. I have to reference all content controls in all three tables for a 4th "scoring" table. The content controls are the full gambit, text, checkboxes, dropdown, list, nested if statements etc. I've tried the following code but it fails and I'm thinking its because its referencing only rich text content although for the life of me I can't see where this is being referenced. Thinking ...controls.item(i) would capture all content controls. Runtime error 4605 method or property is not available as the current selection partially covers a plain text content control. Help is appreciated.

Sub AddBookmarksAtCC()
Dim ccobjA As ContentControl, i As Integer
For i = 1 To ActiveDocument.ContentControls.Count


Set ccobjA = ActiveDocument.ContentControls.Item(i)
Debug.Print ccobjA.Title
ActiveDocument.Bookmarks.Add ccobjA.Title, ActiveDocument.ContentControls.Item(i).Range
Next i
End Sub
Reply With Quote
  #2  
Old 06-11-2016, 08:35 PM
macropod's Avatar
macropod macropod is online now Bookmark content controls Windows 7 64bit Bookmark content controls Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

Question 1: What purpose is bookmarking the content controls intended to serve?
Question 2: Does every content control have a unique title?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 06-11-2016, 08:58 PM
silverspr silverspr is offline Bookmark content controls Windows 8 Bookmark content controls Office 2010 64bit
Novice
Bookmark content controls
 
Join Date: Apr 2011
Posts: 24
silverspr is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
Question 1: What purpose is bookmarking the content controls intended to serve?
Question 2: Does every content control have a unique title?
Every control has a unique title. I'm creating a summary table and with each bookmark control I'm using the value to calculate a score. There are 5 scores or table rows each with it's own bookmark. The five rows are then summed.
Reply With Quote
  #4  
Old 06-11-2016, 09:17 PM
macropod's Avatar
macropod macropod is online now Bookmark content controls Windows 7 64bit Bookmark content controls Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

If the content controls are in the same table as the calculate score is to go, all you need is a formula field summing the values of the cells concerned. No bookmarks are required.

If the content controls are not in the same table as the calculate score is to go, all you need is table-level bookmark a formula field summing the values of the relevant cells in the bookmarked range. No content control bookmarks are required.

To see how to do a wide range of calculations in Word, check out my Microsoft Word Field Maths Tutorial, at:
http://windowssecrets.com/forums/sho...Maths-Tutorial
or:
http://www.gmayor.com/downloads.htm#Third_party
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 06-11-2016, 09:41 PM
silverspr silverspr is offline Bookmark content controls Windows 8 Bookmark content controls Office 2010 64bit
Novice
Bookmark content controls
 
Join Date: Apr 2011
Posts: 24
silverspr is on a distinguished road
Default

Thank you
Since I'm creating a summary table, I am duplicating some of the values from the drop down boxes, the only way i could see to do this was to ref the bookmark. The duplicated value is used in a nested if statement.
Reply With Quote
  #6  
Old 06-11-2016, 09:56 PM
macropod's Avatar
macropod macropod is online now Bookmark content controls Windows 7 64bit Bookmark content controls Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

Even for the duplications, all that is needed is the appropriate table/cell reference in a formula field.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 06-12-2016, 04:01 AM
silverspr silverspr is offline Bookmark content controls Windows 8 Bookmark content controls Office 2010 64bit
Novice
Bookmark content controls
 
Join Date: Apr 2011
Posts: 24
silverspr is on a distinguished road
Default

Thank you. I have 3 tables to reference. In my mind using bookmarks was more efficient. I understand there are probably better approaches to what I'm trying to do however my approach does work I just need assistance with the code I posted earlier to automate book marking the content controls. Is this not possible to do. Do I have to do this "manually"?
Reply With Quote
  #8  
Old 06-12-2016, 04:30 AM
macropod's Avatar
macropod macropod is online now Bookmark content controls Windows 7 64bit Bookmark content controls Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

Assuming the content controls to be bookmarked are all in table cells, you could use:

Code:
Sub Demo()
Application.ScreenUpdating = False
Dim CCtrl As ContentControl, Rng As Range
For Each CCtrl In ActiveDocument.ContentControls
  With CCtrl
    If .Range.Information(wdWithInTable) = True Then
      Set Rng = .Range.Cells(1).Range
      With Rng
        .End = .End - 1
        .Bookmarks.Add CCtrl.Title, Rng
      End With
    End If
  End With
Next
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 06-12-2016, 05:55 AM
silverspr silverspr is offline Bookmark content controls Windows 8 Bookmark content controls Office 2010 64bit
Novice
Bookmark content controls
 
Join Date: Apr 2011
Posts: 24
silverspr is on a distinguished road
Default

Thank you again! This worked swimmingly! I neglected to mention I have a "label" in front of the content control i.e. rating: or score: The control content is being displayed with the words rating or score and the value of the content control as they are both in the same cell. I suppose I could remove the label!

I presume I can set this with the .end property? I haven't played with it yet...

thanks again
Reply With Quote
  #10  
Old 06-12-2016, 06:14 AM
gmayor's Avatar
gmayor gmayor is offline Bookmark content controls Windows 10 Bookmark content controls Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,103
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 of
Default

Instead of setting the range to the cell, set it to the range of the content control

Code:
   If .Range.Information(wdWithInTable) = True Then
      Set Rng = CCtrl.Range
      With Rng
        .Bookmarks.Add CCtrl.Title, Rng
      End With
    End If
  End With
__________________
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
  #11  
Old 06-12-2016, 06:19 AM
macropod's Avatar
macropod macropod is online now Bookmark content controls Windows 7 64bit Bookmark content controls Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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 silverspr View Post
I neglected to mention I have a "label" in front of the content control i.e. rating: or score: The control content is being displayed with the words rating or score and the value of the content control as they are both in the same cell.
That should be easy enough to manage, by inserting:
.Start = CCtrl.Range.Start - 1
before:
.Bookmarks.Add CCtrl.Title, Rng

However, do you realise that, when you cross-reference a bookmarked content control, the cross-reference includes the content control, not just its content? And, if that content control has the 'cannot be deleted property set, the cross-reference can't be updated?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #12  
Old 06-12-2016, 06:20 AM
macropod's Avatar
macropod macropod is online now Bookmark content controls Windows 7 64bit Bookmark content controls Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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 gmayor View Post
Instead of setting the range to the cell, set it to the range of the content control
Try that with a plain text content control or a date content control, for example, and you'll soon find it fails.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #13  
Old 06-12-2016, 06:42 AM
gmayor's Avatar
gmayor gmayor is offline Bookmark content controls Windows 10 Bookmark content controls Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,103
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 of
Default

That's what comes of not paying full attention to the whole thread.
__________________
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
  #14  
Old 06-12-2016, 06:50 AM
silverspr silverspr is offline Bookmark content controls Windows 8 Bookmark content controls Office 2010 64bit
Novice
Bookmark content controls
 
Join Date: Apr 2011
Posts: 24
silverspr is on a distinguished road
Default

HI, Yes Macropod's solution worked and no I did not realize if the "do not delete" property was set, the cross reference would not update. It worked in "testing" but I must not have set the "do not delete" property.

So am I back to your original recommendation to reference the table and cell of the source content control...arghhh.

I do want the cross reference to update when changed at the "source" control.

my this got complicated fast....

thanks
Reply With Quote
  #15  
Old 06-12-2016, 07:13 AM
gmaxey gmaxey is offline Bookmark content controls Windows 7 32bit Bookmark content controls Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Paul,

I suppose someone may move the goal post and add text after the CC in the cell as well.

Code:
Sub Demo()
Dim oCC As ContentControl, oRng As Range
  For Each oCC In ActiveDocument.ContentControls
    With oCC
      If .Range.Information(wdWithInTable) = True Then
        Set oRng = .Range
        oRng.Start = oRng.Start - 1
        oRng.End = oRng.End + 1
        ActiveDocument.Bookmarks.Add .Title, oRng
      End If
    End With
  Next
  Application.ScreenUpdating = True
End Sub
silverspr,

While you may have a method working using fields and conditionals, if you are going to use Content Controls, then why not take full advantage of them?

If you have your five CCs uniquely titled, why not map them to a customXMLPart and use the document CC OnExit Event to perform the calculation:

Code:
Private Sub Document_ContentControlOnExit(ByVal oCC As ContentControl, Cancel As Boolean)
Dim oXMLPart As CustomXMLPart
Dim oNode As CustomXMLNode, oSumNode As CustomXMLNode
Dim sngSum As Single
  Set oXMLPart = oCC.XMLMapping.CustomXMLPart
  For Each oNode In oXMLPart.DocumentElement.ChildNodes
    If oNode.BaseName <> "Sum" Then 'Change Sum to whatever CC title you used for the CC set to display the summed values.
      If IsNumeric(oNode.Text) Then sngSum = sngSum + oNode.Text
    Else
      Set oSumNode = oNode
    End If
  Next
  oSumNode.Text = sngSum
lbl_Exit:
  Exit Sub
End Sub
You can quickly map CCs in any document using my CC Tools Add-In:
http://gregmaxey.com/word_tip_pages/...rol_tools.html

or use:

Code:
Sub MapYourDocumentCCs()
Dim oXMLPart As Office.CustomXMLPart
Dim oNode As CustomXMLNode
Dim oCC As ContentControl
  On Error Resume Next
  'Keep parts from piling up if you rerun the code.
  Set oXMLPart = ActiveDocument.CustomXMLParts.SelectByNamespace("http://Greg_Maxey/Demo").Item(1)
  oXMLPart.Delete
  On Error GoTo 0
  Set oXMLPart = ActiveDocument.CustomXMLParts.Add("<?xml version='1.0' encoding='utf-8'?><Root xmlns='http://Greg_Maxey/Demo'></Root>")
    For Each oCC In ActiveDocument.ContentControls
      'Currently no conditions. You can set conditions here e.g., If oCC.Tag = "Map me" etc.
      oXMLPart.AddNode oXMLPart.DocumentElement, Replace(oCC.Title, " ", "_"), , , msoCustomXMLNodeElement
      oCC.XMLMapping.SetMapping oXMLPart.DocumentElement.LastChild.xPath
    Next oCC
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply

Tags
bookmark;content control



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA for content controls ciresuark Word VBA 1 03-10-2015 03:14 PM
Calculate Age From Content Controls kintap Word VBA 10 07-02-2014 09:25 AM
Content Controls in Headers ejungk99 Word 2 06-16-2014 04:02 PM
Bookmark content controls Content Controls Sammie0Sue Word 6 11-06-2013 10:56 PM
Bookmark content controls Grouping Content Controls cksm4 Word VBA 2 03-01-2011 12:46 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:09 PM.


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