Jump to content

How do you change a Word CheckBox Value


Recommended Posts

I can change the text of a Bookmark with this

$CompletedBy = "Ken Nichols"

$oWordApp.Activedocument.Bookmarks("preparedby").Range.Text = $CompletedBy

How would I change the value of a CheckBox?

I found the following VBA code on the web, but it will not work for me.

$oWordApp.Activedocument.FormFields("checkbox1").CheckBox.Value = True

I think I need to do a Collection of the FormFields but I have no idea on how to do that.

I really hate to keep asking. But I really need help with this.

Edited by KenNichols
[topic="21048"]New to AutoIt? Check out AutoIt 1-2-3![/topic] Need to make a GUI? You NEED KODA FormDesigner!
Link to comment
Share on other sites

look up the word UDF in the help file

Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.

Link to comment
Share on other sites

  • Moderators

The code you posted is formatted correctly. The part I highlighted either needs to be the index number or the bookmark name name associated with the check box.

$oWordApp.Activedocument.FormFields("checkbox1").CheckBox.Value = True

CheckBox Object

Edited by big_daddy
Added reference link
Link to comment
Share on other sites

The code you posted is formatted correctly. The part I highlighted either needs to be the index number or the bookmark name name associated with the check box.

CheckBox Object

How do I find the index number?

I created a bookmark called "test" on the checkbox and got this

C:\Documents and Settings\Technician\Desktop\Word Test\CheckBox.au3 (4) : ==> The requested action with this object has failed.:

$oWordApp.Activedocument.FormFields("test").CheckBox.Value = True

$oWordApp.Activedocument.FormFields("test")^ ERROR

I also tried

$oWordApp.Activedocument.Bookmarks("test").Range.Text = "This is a test!!!"

and it replaces the checkbox with "This is a test!!!"

Edited by KenNichols
[topic="21048"]New to AutoIt? Check out AutoIt 1-2-3![/topic] Need to make a GUI? You NEED KODA FormDesigner!
Link to comment
Share on other sites

  • Moderators

See what you get with this. The form field # or name is what you need to use.

#include <Word.au3>

Const $wdFieldFormCheckBox = 71

Global $iIndex = 1
Global $sFilePath = "C:\temp\test.docx"

_WordErrorHandlerRegister()
Global $oWordApp = _WordCreate($sFilePath, 1)
Global $oDoc = _WordDocGetCollection($oWordApp, 0)

Global $oFormFields = $oDoc.FormFields()
If IsObj($oFormFields) Then
    ConsoleWrite("Total Form Fields: " & $oFormFields.Count & @CR)
    ConsoleWrite("==================================================" & @CR)
Else
    ConsoleWrite("There are no form fields in this document." & @CR)
    Exit
EndIf

For $oFormField In $oFormFields
    ConsoleWrite("Form Field #" & $iIndex & @CR)
    ConsoleWrite("Name: " & $oFormField.Name & @CR)
    ConsoleWrite("Type: " & $oFormField.Type & @CR) ; http://msdn.microsoft.com/en-us/library/bb213727.aspx
    If $oFormField.Type = $wdFieldFormCheckBox Then
        ConsoleWrite("CheckBox Value: " & $oFormField.CheckBox.Value & @CR)
    EndIf
    ConsoleWrite("==================================================" & @CR)
    $iIndex += 1
Next
Link to comment
Share on other sites

On my test doc I get

Total Form Fields: 0

on the other 1 I get

Total Form Fields: 47

==================================================

Form Field #1

Name:

Type: 70

==================================================

Form Field #2

Name:

Type: 70

==================================================

Form Field #3

Name: FormDate

Type: 70

==================================================

Form Field #4

Name: preparedby

Type: 70

==================================================

Form Field #5

Name: companyname

Type: 70

etc etc....................

[topic="21048"]New to AutoIt? Check out AutoIt 1-2-3![/topic] Need to make a GUI? You NEED KODA FormDesigner!
Link to comment
Share on other sites

  • Moderators

This was a tricky one.

#include <Word.au3>

Const $wdFieldFormCheckBox = 71
Const $wdFieldOCX = 87
Const $wdFieldFormTextInput = 70

Const $wdHalfCheck = -1
Const $wdUnCheck = 0
Const $wdFullCheck = 1

Global $iIndex = 1
Global $sFilePath = "C:\temp\test.docx"

_WordErrorHandlerRegister()
Global $oWordApp = _WordCreate($sFilePath, 1)
Global $oDoc = _WordDocGetCollection($oWordApp, 0)

Global $oFields = $oDoc.Fields
If IsObj($oFields) Then
    ConsoleWrite("Total Fields: " & $oFields.Count & @CR)
    ConsoleWrite("==================================================" & @CR)
Else
    ConsoleWrite("There are no fields in this document." & @CR)
    Exit
EndIf

For $oField In $oFields
    If $oField.Type = $wdFieldOCX Then
        With $oField
            ConsoleWrite("Index #" & .Index & @CR)
            ConsoleWrite("Type: " & .Type & @CR) ; http://msdn.microsoft.com/en-us/library/bb213727.aspx
            $oCheckBox = .InlineShape.OLEFormat
        EndWith
        With $oCheckBox
            ConsoleWrite("ClassType: " & .ClassType & @CR)
            If .ClassType = "Forms.CheckBox.1" Then
                ConsoleWrite("Old Value: " & .Object.Value & @CR)
                .Object.Value = $wdFullCheck
                ConsoleWrite("New Value: " & .Object.Value & @CR)
            EndIf
        EndWith
        ConsoleWrite("==================================================" & @CR)
    EndIf
    $iIndex += 1
Next
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...