HTSCF Fareha
02-18-2022, 03:29 AM
I have the following sub which should enable the user to select an answer for a number of Yes/No questions by selecting the relevant optionbutton on the UserForm, which will then populate the respective content control. Each Yes/No pair are grouped.
At the moment I'm getting a Run Time error 91: Object variable or with block object not set, but no hint on where this might be failing. I'm hoping that I'm not too far off what I wish to achieve?
Option Explicit
Dim oRng As Range
Dim HasFocus As String
Dim occ As ContentControl
Dim oCtr As MSForms.control
Dim oDoc As Document
Private Sub UserForm_Initialize()
' Sort out the options for yes/no questions
For Each oCtr In Me.Controls
Select Case TypeName(oCtr)
Case "OptionButton"
For Each occ In oDoc.ContentControls
With Me.Controls(oCtr.Name).Value
Select Case Me.Controls(oCtr.Name).GroupName
Case "Delivered"
Set occ = ActiveDocument.SelectContentControlsByTitle("Delivered").Item(1)
Select Case Me.Controls(oCtr.Name).Caption
Case "Yes"
occ.Range.Text = "YES"
Case "No"
occ.Range.Text = "NO"
End Select
Case "Evidence"
Set occ = ActiveDocument.SelectContentControlsByTitle("Evidence").Item(1)
Select Case Me.Controls(oCtr.Name).Caption
Case "Yes"
occ.Range.Text = "YES"
Case "No"
occ.Range.Text = "NO"
End Select
Case "Employee"
Set occ = ActiveDocument.SelectContentControlsByTitle("Employee").Item(1)
Select Case Me.Controls(oCtr.Name).Caption
Case "Yes"
occ.Range.Text = "YES"
Case "No"
occ.Range.Text = "NO"
End Select
Case "Funds"
Set occ = ActiveDocument.SelectContentControlsByTitle("Funds").Item(1)
Select Case Me.Controls(oCtr.Name).Caption
Case "Yes"
occ.Range.Text = "YES"
Case "No"
occ.Range.Text = "NO"
End Select
End Select
End With
Next occ
End Select
Next
lbl_Exit:
Exit Sub
End Sub
At the moment I'm getting a Run Time error 91: Object variable or with block object not set, but no hint on where this might be failing. I'm hoping that I'm not too far off what I wish to achieve?
Option Explicit
Dim oRng As Range
Dim HasFocus As String
Dim occ As ContentControl
Dim oCtr As MSForms.control
Dim oDoc As Document
Private Sub UserForm_Initialize()
' Sort out the options for yes/no questions
For Each oCtr In Me.Controls
Select Case TypeName(oCtr)
Case "OptionButton"
For Each occ In oDoc.ContentControls
With Me.Controls(oCtr.Name).Value
Select Case Me.Controls(oCtr.Name).GroupName
Case "Delivered"
Set occ = ActiveDocument.SelectContentControlsByTitle("Delivered").Item(1)
Select Case Me.Controls(oCtr.Name).Caption
Case "Yes"
occ.Range.Text = "YES"
Case "No"
occ.Range.Text = "NO"
End Select
Case "Evidence"
Set occ = ActiveDocument.SelectContentControlsByTitle("Evidence").Item(1)
Select Case Me.Controls(oCtr.Name).Caption
Case "Yes"
occ.Range.Text = "YES"
Case "No"
occ.Range.Text = "NO"
End Select
Case "Employee"
Set occ = ActiveDocument.SelectContentControlsByTitle("Employee").Item(1)
Select Case Me.Controls(oCtr.Name).Caption
Case "Yes"
occ.Range.Text = "YES"
Case "No"
occ.Range.Text = "NO"
End Select
Case "Funds"
Set occ = ActiveDocument.SelectContentControlsByTitle("Funds").Item(1)
Select Case Me.Controls(oCtr.Name).Caption
Case "Yes"
occ.Range.Text = "YES"
Case "No"
occ.Range.Text = "NO"
End Select
End Select
End With
Next occ
End Select
Next
lbl_Exit:
Exit Sub
End Sub