#1
|
|||
|
|||
Assign a value to a Content Control
Hello -
I am a novice at VBA but I think I need it to do the following. This pertains to a form I am creating that needs content controls for text as well as calculations and need the text controls to be able to be spell checked. (I know Spell check cannot be run on Legacy Controls) I would like a combo box content control to that once selected retains a value to be calculated while showing the text chosen. For example: Choices would be Excellent, Average, Below Average the values would be 20, 10, 5 respectively. I would have several of the combo boxes and at the end I would like to take an average of the selections. I have the table set-up and searched the forum for answers but have not found anything like this or maybe I am to novice to understand the VBA presented. Thank you for any assistance |
#2
|
||||
|
||||
See attached.
For an alternative approach, see: https://www.msofficeforums.com/word-...g-results.html
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Awesome - I am trying to use the example you sent in the .zip file.
This is the 2nd table in the form so I changed the line With ActiveDocument.Tables(1) to (2) but when I go to try the form I get stopped at the code line With .Cell(i, 2).Range.ContentControls(1) I am not sure what that line is referring to. Thank you for your help. I am trying my best to understand the code by reading in the VBA Word Help but it is a bit confusing for a novice. The code in the .zip is below just to refresh your memory. Code:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean) Dim i As Long, j As Long, k As Long, l As Long With ActiveDocument.Tables(2) If ContentControl.Range.InRange(.Range) Then For i = 1 To .Rows.Count - 1 With .Cell(i, 2).Range.ContentControls(1) If .Range.Text <> .PlaceholderText Then k = k + 1 For j = 1 To .DropdownListEntries.Count If .DropdownListEntries(j) = .Range.Text Then l = l + .DropdownListEntries(j).Value Exit For End If Next End If End With Next With .Rows.Last.Cells(2).Range If k > 0 Then .Text = Format(l / k, "0.00") Else .Text = vbNullString End If End With End If End With End Sub Last edited by macropod; 12-07-2013 at 01:38 PM. Reason: Added code tags & formatting |
#4
|
||||
|
||||
The 'With .Cell(i, 2).Range.ContentControls(1)' statement occurs in a loop that starts at the first row in the table and stops at the penultimate row. The 'i' refers to the row number, the '2' refers to the column number and the '1' refers to the first ContentControl in that cell. So, if your ContentControl is not in the second column, you'll need to change the '2' (twice) and, if your table has a header row, you'll need to change:
For i = 1 To .Rows.Count - 1 to: For i = 2 To .Rows.Count - 1 PS: When posting code, please use the code tags. They're on the 'Go Advanced' tab.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Thank you for your quick response on a Saturday no less!
Alas I did not count the header row!! But now it is stuck at the next line of code. My ContentControl is in column 2. Code:
If .Range.Text <> .PlaceholderText Then |
#6
|
||||
|
||||
Actually, it was Sunday am here...
Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Sorry, the error is
Run-time error '91': Object variable or With block variable not set when I click on debug the line in my last post is highlighted. |
#8
|
||||
|
||||
Have you changed anything in the code other than the table reference (to 2) and the starting row (also to 2)?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
No, but it is late here and I have certainly been reading to much vba help to make much sense tonight. Here is the code I have in the document. I may pick this up again tomorrow. Cheers to you - Until tomorrow?
Code:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean) Dim i As Long, j As Long, k As Long, l As Long With ActiveDocument.Tables(2) If ContentControl.Range.InRange(.Range) Then For i = 2 To .Rows.Count - 1 With .Cell(i, 2).Range.ContentControls(1) If .Range.Text <> .PlaceholderText Then k = k + 1 For j = 1 To .DropdownListEntries.Count If .DropdownListEntries(j) = .Range.Text Then l = l + .DropdownListEntries(j).Value Exit For End If Next End If End With Next With .Rows.Last.Cells(2).Range If k > 0 Then .Text = Format(l / k, "0.00") Else .Text = vbNullString End If End With End If End With End Sub |
#10
|
||||
|
||||
The code looks OK - it's trying to process the 2nd to 2nd-last rows in the 2nd column of the 2nd table (a bit of a mouthful of 2nds there!). Do you have anything other than the combobox contentcontrols there?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#11
|
|||
|
|||
Good Morning!
A rested brain is a good thing. I just deleted the content control boxes , re-entered them and now it works like a charm. Thank you so very much!!! |
Tags |
content controls, vba |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Move to next content control | cksm4 | Word VBA | 13 | 07-02-2019 07:48 PM |
Deleting a table from a content control -- preserving the content control | BrainSlugs83 | Word Tables | 8 | 11-14-2013 03:06 AM |
Content control problem | Anja | Word | 2 | 10-16-2013 09:56 AM |
Hierarchical content control | ntjson | Word VBA | 1 | 04-04-2013 12:07 AM |
Content control titles | jillapass | Word VBA | 3 | 05-29-2012 06:11 AM |