Lou
Lou

Reputation: 403

vbscript validate checkboxes in Microsoft Word

I recently posted the topic Manipulating content in Microsoft Word with VBScript or VBA and received tremendous help solving my issue. Spurring off of this topic, I need to be able to use what I have learned and incorporate a new task.

I have a document similar to the one in the link above. However, the only difference is there are checkboxes being used and the code needs to select/deselect according to the user's choice. Here is the code I have so far:

Sub Check_Document()

    objWord.Visible = True
    Set objDoc = objWord.Documents.Open(UserProfile & "\Desktop\lettermiscreason - original.docx")

    Set objRange = objDoc.Bookmarks("TodaysDate").Range
    objRange.Text = TodaysDate
    Set objRange = objDoc.Bookmarks("ProvName").Range
    objRange.Text = ProvName
    Set objRange = objDoc.Bookmarks("ProvAddress").Range
    objRange.Text = ProvAddress
    Set objRange = objDoc.Bookmarks("ProvCity").Range
    objRange.Text = ProvCity
    Set objRange = objDoc.Bookmarks("ProvState").Range
    objRange.Text = ProvState
    Set objRange = objDoc.Bookmarks("ProvZip").Range
    objRange.Text = ProvZip
    Set objRange = objDoc.Bookmarks("Check6").Range
    If objRange.Value = False Then
        objRange.Value = True
    End If
    Set objRange = objDoc.Bookmarks("Check5").Range
    If objRange.Value = True Then
        objRange.Value = False
    End If
    Set objRange = objDoc.Bookmarks("ProcInit").Range
    objRange.Text = ProcInit

End Sub

Check6 is the checkbox I want to select and Check5 is the box I want to deselect. With the code as it sits, I get an error saying: Microsoft VBScript runtime error: Object doesn't support this property or method: 'objRange.Value'. They show as bookmarks when I look at the rest I've used to enter data. Any suggestions? Should I be using something other than objRange or can I use that but use something other than .Value?

Upvotes: 0

Views: 787

Answers (1)

Lou
Lou

Reputation: 403

After trying a few random ideas that I remember from validating HTML forms, I found a solution. I used the VBA FormFields of the document object and utilized the bookmarks for each checkbox to locate and change the values.

Sub Check_Document()

    objWord.Visible = True
    Set objDoc = objWord.Documents.Open(UserProfile & "\Desktop\lettermiscreason - original.docx")
    ' Created a FormFields object
    Set objFrmFld = objDoc.FormFields

    Set objRange = objDoc.Bookmarks("TodaysDate").Range
    objRange.Text = TodaysDate
    Set objRange = objDoc.Bookmarks("ProvName").Range
    objRange.Text = ProvName
    Set objRange = objDoc.Bookmarks("ProvAddress").Range
    objRange.Text = ProvAddress
    Set objRange = objDoc.Bookmarks("ProvCity").Range
    objRange.Text = ProvCity
    Set objRange = objDoc.Bookmarks("ProvState").Range
    objRange.Text = ProvState
    Set objRange = objDoc.Bookmarks("ProvZip").Range
    objRange.Text = ProvZip
    ' Used the FormFields object to change the value from unchecked to checked
    If objFrmFld("Check6").CheckBox.Value = False Then
        objFrmFld("Check6").CheckBox.Value = True
    End If
    ' Used the FormFields object to change the value from checked to unchecked
    If objFrmFld("Check5").CheckBox.Value = True Then
        objFrmFld("Check5").CheckBox.Value = False
    End If
    Set objRange = objDoc.Bookmarks("ProcInit").Range
    objRange.Text = ProcInit

End Sub

I hope this helps someone. I do apologize for not doing a little extra research and coming up with the solution before asking the question.

Upvotes: 1

Related Questions