Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-06-2013, 09:00 AM
SuzeG SuzeG is offline Assign a value to a Content Control Windows 7 64bit Assign a value to a Content Control Office 2010 64bit
Novice
Assign a value to a Content Control
 
Join Date: Dec 2013
Location: Va Beach
Posts: 21
SuzeG is on a distinguished road
Cool 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
Reply With Quote
  #2  
Old 12-06-2013, 05:00 PM
macropod's Avatar
macropod macropod is offline Assign a value to a Content Control Windows 7 32bit Assign a value to a Content Control 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

See attached.

For an alternative approach, see:
https://www.msofficeforums.com/word-...g-results.html
Attached Files
File Type: zip Content Controls - ComboBox Table Score.zip (32.7 KB, 28 views)
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 12-07-2013, 01:11 PM
SuzeG SuzeG is offline Assign a value to a Content Control Windows 7 64bit Assign a value to a Content Control Office 2010 64bit
Novice
Assign a value to a Content Control
 
Join Date: Dec 2013
Location: Va Beach
Posts: 21
SuzeG is on a distinguished road
Default

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
Reply With Quote
  #4  
Old 12-07-2013, 01:44 PM
macropod's Avatar
macropod macropod is offline Assign a value to a Content Control Windows 7 32bit Assign a value to a Content Control 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

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]
Reply With Quote
  #5  
Old 12-07-2013, 02:21 PM
SuzeG SuzeG is offline Assign a value to a Content Control Windows 7 64bit Assign a value to a Content Control Office 2010 64bit
Novice
Assign a value to a Content Control
 
Join Date: Dec 2013
Location: Va Beach
Posts: 21
SuzeG is on a distinguished road
Default

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
Thanks for the posting tip.
Reply With Quote
  #6  
Old 12-07-2013, 06:49 PM
macropod's Avatar
macropod macropod is offline Assign a value to a Content Control Windows 7 32bit Assign a value to a Content Control 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 SuzeG View Post
Thank you for your quick response on a Saturday no less!
Actually, it was Sunday am here...
Quote:
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
OK, you're stuck, but in what way?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 12-07-2013, 08:14 PM
SuzeG SuzeG is offline Assign a value to a Content Control Windows 7 64bit Assign a value to a Content Control Office 2010 64bit
Novice
Assign a value to a Content Control
 
Join Date: Dec 2013
Location: Va Beach
Posts: 21
SuzeG is on a distinguished road
Default

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.
Reply With Quote
  #8  
Old 12-07-2013, 08:28 PM
macropod's Avatar
macropod macropod is offline Assign a value to a Content Control Windows 7 32bit Assign a value to a Content Control 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

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]
Reply With Quote
  #9  
Old 12-07-2013, 08:50 PM
SuzeG SuzeG is offline Assign a value to a Content Control Windows 7 64bit Assign a value to a Content Control Office 2010 64bit
Novice
Assign a value to a Content Control
 
Join Date: Dec 2013
Location: Va Beach
Posts: 21
SuzeG is on a distinguished road
Default

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
Reply With Quote
  #10  
Old 12-07-2013, 11:48 PM
macropod's Avatar
macropod macropod is offline Assign a value to a Content Control Windows 7 32bit Assign a value to a Content Control 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

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]
Reply With Quote
  #11  
Old 12-08-2013, 08:04 AM
SuzeG SuzeG is offline Assign a value to a Content Control Windows 7 64bit Assign a value to a Content Control Office 2010 64bit
Novice
Assign a value to a Content Control
 
Join Date: Dec 2013
Location: Va Beach
Posts: 21
SuzeG is on a distinguished road
Thumbs up

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!!!
Reply With Quote
Reply

Tags
content controls, vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
Assign a value to a Content Control Move to next content control cksm4 Word VBA 13 07-02-2019 07:48 PM
Assign a value to a Content Control Deleting a table from a content control -- preserving the content control BrainSlugs83 Word Tables 8 11-14-2013 03:06 AM
Assign a value to a Content Control Content control problem Anja Word 2 10-16-2013 09:56 AM
Assign a value to a Content Control Hierarchical content control ntjson Word VBA 1 04-04-2013 12:07 AM
Assign a value to a Content Control Content control titles jillapass Word VBA 3 05-29-2012 06:11 AM

Other Forums: Access Forums

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