Share via

VBA return option button caption value to document

Anonymous
2018-07-18T21:20:18+00:00

Hi, I've tried multiple variations trying to get the caption of a chosen option button to show on a word document.  I have the three option buttons in the same group so only one can be chosen.  I just can't seem to get there.  I have the code I found in another thread but I receive an error on the 2nd Me on the strCaption = Me.Me.optC.Caption line.  I can't seem to figure out how to assign the caption value to the doc variable value in the word document.   I receive an error in the document that the variable has not been supplied.

.Variables("Class").Value = strCaption

Dim strCaption As String

If Me.optB.Value = True Then

     strCaption = Me.optB.Caption

 Else

 If Me.optC.Value = True Then

    strCaption = Me.Me.optC.Caption

 Else

    strCaption = optD.Caption

End If

End If

thanks for your help.

Cindy

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Jay Freedman 207.7K Reputation points Volunteer Moderator
2018-07-19T00:51:20+00:00

Just get rid of the extra .Me in that line. In fact, you can get rid of all the Me's, because they're optional in a simple case like this.

The code for the button (usually labeled OK) that closes the userform can be this:

Private Sub cmdOK_Click()

    Dim strCaption As String

    If optB.Value = True Then

        strCaption = optB.Caption

    ElseIf optC.Value = True Then

        strCaption = optC.Caption

    Else

        strCaption = optD.Caption

    End If

    ActiveDocument.Variables("Class").Value = strCaption

    Me.Hide

End Sub

Was this answer helpful?

2 people found this answer helpful.
0 comments No comments

Answer accepted by question author

Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
2018-07-19T00:53:10+00:00

The code should be

Dim strCaption As String

If Me.optB.Value = True Then

    strCaption = Me.optB.Caption

ElseIf Me.optC.Value = True Then

    strCaption = Me.Me.optC.Caption

Else

    strCaption = optD.Caption

End If

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2018-07-19T18:31:45+00:00

    Thank you both! I have it working now perfectly, could have been the Else instead of ElseIf.  Now my next quest is to set up a table as a building block and have it inserted as the last rows to an existing table using a command button on the userform.  I'm sure when I reach the pulling my hair out stage I'll be back with questions on that.

    Thanks again for your ongoing willingness to help!

    Cindy

    Was this answer helpful?

    0 comments No comments