Microsoft Word - Userform Bookmarks runtime error 424

Asked By Richard Davis on 02-Dec-10 11:21 AM
Hi,

I'm trying to resolve a runtime error 424 when clicking the "OK" commandbutton on a userform and would apreciate some guidance.

I have created a Word template for a presenter's submission form and inserted Bookmarks for user details.  I have then created a userform which should transfer the relevant user text inputs and optionbuttons to the template when clicking on "OK".  The userform opens successfully but the runtime error 424 "object required" problem occurs at this point.  Activating debug brings up the vba code for the cmdclick button with the first line relating to bookmarking the first textbox highlighted.  I have checked each textbox name property matches the corresponding bookmark name in the template but cannot find what is causing this error.

My vba code is below:

Private Sub cmdOK_Click()
  Dim strFrame1 As String
    Dim strFrame2 As String
    Dim strSubmissionType As String
  
  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)"
    Application.ScreenUpdating = False
    With ActiveDocument
    .Bookmarks("Name").Range.Text = txtName.Value
    .Bookmarks("Institution1").Range.Text = txtInstitution1.Value
    .Bookmarks("Address").Range.Text = txtAddress.Value
    .Bookmarks("Phone").Range.Text = txtPhone.Value
    .Bookmarks("Email").Range.Text = txtEmail.Value
    .Bookmarks("Lead").Range.Text = txtLead.Value
    .Bookmarks("Institution").Range.Text = txtInstitution.Value
    .Bookmarks("Coauthor_attending").Range.Text = txtCoauthor_attending.Value
    .Bookmarks("coauthor_institution_attending").Range.Text = txtcoauthor_institution_attending.Value
    .Bookmarks("coauthor_notattending").Range.Text = txtcoauthor_notattending.Value
    .Bookmarks("coauthor_institution_notattending").Range.Text = txtcoauthor_institution_notattending.Value
    .Bookmarks("Session_Title").Range.Text = txtSession_Title.Value
    .Bookmarks("Relationship").Range.Text = txtRelationship.Value
    .Bookmarks("Abstract").Range.Text = txtAbstact.Value
    End With
    Application.ScreenUpdating = True
    Unload Me
End Sub
olvin j replied to Richard Davis on 03-Dec-10 10:00 PM
hi

go into the IDE and step through the code -- pressing F8 steps it.
You'll get an error on one line ... we need to identify it.

"Jennifer" <Jennifer@discussions.microsoft.com> wrote in message
news:9CA02C3B-B465-43B8-B184-880CC3F2F984@microsoft.com...
> Hey Patrick,
> It is not hightling the area that has a problem it just says
> "Run Time Error 424"
> Object required
>
> I have to let you know this is a first time for me writing something like
> this, so I'm pretty stuck. How can I tell were the error occurs. Have
> double
> checked all names and worksheet names. I will double check again. Thank
> you
> for your time. Jennifer
>
> "Patrick Molloy" wrote:
>
>> on what line does the error occur. Your code worked fine for me. Step
>> through
>> your code and see where it breaks. In the immediate window
>> ?err.description
>>
>> I did this with a new workbook
>> 1) chaged a sheetname to wksLookupLists
>> 3) added three named ranges matching your names in the code
>> 4) added a userform with three combos, same names as per your code
>> 5) pasted your code into the code page of the userform
>>
>> I ran first time, no issues. If your range names or wks name was wrong,
>> you'd get a 1004 error. if combobox names were wrong you'd geta compile
>> error.
>>
>>
>>
>> "Jennifer" wrote:
>>
>> > Hey all my BRILLIANT friends,
>> > Can you help? I am getting a run time error
>> >
>> > Private Sub UserForm_Initialize()
>> > Dim cVend As Range
>> > Dim cRan As Range
>> > Dim cProd As Range
>> >
>> > For Each cVend In wksLookupLists.Range("VendorList")
>> > With Me.cboVend
>> > .AddItem cVend.Value
>> > .List(.ListCount - 1, 1) = cVend.Offset(0, 1).Value
>> > End With
>> > Next cVend
>> > For Each cRan In wksLookupLists.Range("RanchIDList")
>> > With Me.cboRan
>> > .AddItem cRan.Value
>> > .List(.ListCount - 1, 1) = cRan.Offset(0, 1).Value
>> > End With
>> > Next cRan
>> > For Each cProd In wksLookupLists.Range("ProduceIDList")
>> > With Me.cboProd
>> > .AddItem cProd.Value
>> > .List(.ListCount - 1, 1) = cProd.Offset(0, 1).Value
>> > End With
>> > Next cProd
>> >
>> > End Sub
>> > --
>> > Though daily learning, I LOVE EXCEL!
>> > Jennifer
Richard Davis replied to olvin j on 07-Dec-10 03:08 PM


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 !
Richard Davis replied to olvin j on 07-Dec-10 03:18 PM
Hi Olvin,

As you can see from my last post, I've played around with it and now find that the Userform opens ok but if the user leaves a blank field, when the command button "OK" is clicked and the "incomplete"  message is displayed, the user form disappears when this message is acknowledged and you are dumped back into the Word document with no fields updated.  The userform should stay visible and all uncompleted fields are highlighted and the user can work through each of these in turn.

Also, where in my coding am I going wrong with updating the DocVariable fields in the Word doc ? None of the fields are updated.