I took another look at this and changed the bookmarks for DocVariables in the Word doc and tried various changes to the procedures - the net result is that I don't have the 424 error anymore but I'm going round in circles.
My Userform opens ok and if the user leaves a blank field, when the command button "OK" is clicked this is caught and a message displayed. All fine - however, when the user clicks ok in this dialog box, the user form disappears and you are dumped back into the Word document with no fields updated. What should happen is that the userform stays visible, all uncompleted fields are highlighted and the user can work through each of these in turn.
Also, if all fields are completed, there is no updating of DocVariable fields in the Word doc - I have probably messed up the code completely trying to solve this.
Here's the code so far:
Option Explicit
Public boolProceed As Boolean
Private Sub CommandButton1_Click()
End Sub
Private Sub CommandButton3_Click()
End Sub
Private Sub cmdCancel_Click()
Unload Me
ActiveDocument.Close SaveChanges:=False
End Sub
Private Sub cmdClearForm_Click()
optStatus1.Value = True
TextBox1.Value = Null
TextBox2.Value = Null
TextBox3.Value = Null
TextBox4.Value = Null
TextBox5.Value = Null
TextBox6.Value = Null
TextBox7.Value = Null
TextBox8.Value = Null
TextBox9.Value = Null
TextBox10.Value = Null
TextBox11.Value = Null
TextBox12.Value = Null
TextBox13.Value = Null
TextBox14.Value = Null
TextBox15.Value = Null
TextBox16.Value = Null
TextBox17.Value = Null
TextBox18.Value = Null
TextBox19.Value = Null
End Sub
Private Sub cmdOK_Click()
Me.Hide
Dim boolComplete As Boolean
Dim oVars As Variables
Set oVars = ActiveDocument.Variables
'Update the fields in the document
ActiveDocument.Fields.Update
Select Case ""
Case Me.TextBox1.Value
MsgBox "Please type your full name."
Me.TextBox1.SetFocus
Exit Sub
Case Me.TextBox2.Value
MsgBox "Please type your institution."
Me.TextBox2.SetFocus
Exit Sub
Case Me.TextBox5.Value
MsgBox "Please fill-in your address."
Me.TextBox5.SetFocus
Exit Sub
Case Me.TextBox3.Value
MsgBox "Please fill-in your phone number."
Me.TextBox3.SetFocus
Exit Sub
Case Me.TextBox4.Value
MsgBox "Please type your email address."
Me.TextBox4.SetFocus
Exit Sub
End Select
Me.boolProceed = True
Me.Hide
Dim strFrame1 As String
Dim strFrame2 As String
Dim strSubmissionType As String
Dim ctl As Control
If optStatus1 = True Then strFrame1 = "Research Student"
If optStatus2 = True Then strFrame1 = "Academic Staff Member"
If optStatus3 = True Then strFrame1 = "Practising Staff Member"
If optStatus4 = True Then strFrame1 = "Independent"
If optStatus5 = True Then strFrame1 = "Other"
If optAll = True Then strFrame2 = "All"
If optPrimary = True Then strFrame2 = "Primary"
If optSecondary = True Then strFrame2 = "Secondary"
If optFE = True Then strFrame2 = "FE"
If optHE = True Then strFrame2 = "HE"
If optResearcher = True Then strFrame2 = "Researcher"
If optOther = True Then strFrame2 = "Other (please specify)"
If optType1 = True Then strSubmissionType = "Paper"
If optType2 = True Then strSubmissionType = "Workshop"
If optType3 = True Then strSubmissionType = "Poster Session"
If optType4 = True Then strSubmissionType = "Other (please specify)"
boolComplete = True
For Each ctl In Me.Controls
Select Case TypeName(ctl)
Case "TextBox"
If ctl.Value = "" Then
boolComplete = False
ctl.BackColor = vbRed
Else
ctl.BackColor = &H80000005
End If
End Select
Next ctl
MsgBox "You have not completed the form" & vbCrLf & vbCrLf & "Please fill in the highlighted boxes", vbExclamation, "Incomplete"
Call myUpdateFields
Application.ScreenUpdating = False
Unload Me
End Sub
Private Sub OptionButton3_Click()
End Sub
Private Sub OptionButton4_Click()
End Sub
Private Sub UserForm_Initialize()
optStatus1.Value = True
'Set the default value for the text box field
TextBox1.Value = "Default value"
End Sub
Sub CallUF()
Dim oFrm As frmPresenterSubmission
Dim oVars As Word.Variables
Dim pStr As String
Dim oRng As Word.Range
Dim i As Long
Dim pMulSel As String
Dim boolProceed As Boolean
Set oVars = ActiveDocument.Variables
Set oFrm = New frmPresenterSubmission
With oFrm
.Show
If boolProceed Then
oVars("varName").Value = TextBox1
oVars("varInstitution").Value = TextBox2
oVars("varPhone").Value = TextBox3
oVars("varEmail").Value = TextBox4
oVars("varAddress").Value = TextBox5
'Replace the line breaks entered by the user with line breaks and tabs_to ensure address entry is properly indented.
pStr = Replace(TextBox5.Value, Chr(10), Chr(10) + Chr(9))
Set oRng = ActiveDocument.Bookmarks("bmAddress").Range
oRng.Text = pStr
ActiveDocument.Bookmarks.Add "bmAddress", oRng
End If
If .CheckBox1.Value = True Then pStr = "Yes,"
If .CheckBox2.Value = True Then pStr = "No,"
If .CheckBox3.Value = True Then pStr = "Yes,"
If .CheckBox4.Value = True Then pStr = "No,"
End With
Unload oFrm
Set oFrm = Nothing
Set oVars = Nothing
Set oRng = Nothing
End Sub
Sub myUpdateFields()
Dim pRange As Word.Range
Dim iLink As Long
For Each pRange In ActiveDocument.StoryRanges
Do
pRange.Fields.Update
Set pRange = pRange.NextStoryRange
Loop Until pRange Is Nothing
Next
End Sub
If you can spot the howlers that would be great !