#1
|
|||
|
|||
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 |
#2
|
||||
|
||||
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] |
#3
|
|||
|
|||
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.
|
#4
|
||||
|
||||
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] |
#5
|
|||
|
|||
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. |
#6
|
||||
|
||||
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] |
#7
|
|||
|
|||
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"?
|
#8
|
||||
|
||||
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] |
#9
|
|||
|
|||
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 |
#10
|
||||
|
||||
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 |
#11
|
||||
|
||||
Quote:
.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] |
#12
|
||||
|
||||
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] |
#13
|
||||
|
||||
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 |
#14
|
|||
|
|||
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 |
#15
|
|||
|
|||
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 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 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 |
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 |
Content Controls | Sammie0Sue | Word | 6 | 11-06-2013 10:56 PM |
Grouping Content Controls | cksm4 | Word VBA | 2 | 03-01-2011 12:46 PM |