Solved

Populating Word Checkboxes with VB

Posted on 2000-03-16
6
768 Views
Last Modified: 2008-02-01
Is there a way to change the value of a checkbox in a Word 97 .dot from the VB6 code that references it?  

I'm populating the .dot using bookmarks
and creating the reference to the Word object as follows ...

Set objWord = GetObject(,"Word.Application")
  If objWord Is Nothing Then    ' If true, Word is not running.
    Set objWord = New Word.Application ' Create a new instance of the Word application.
    If objWord Is Nothing Then    ' If true, MS Word 8.0 is not installed.
        MsgBox "MS Word is not installed on your computer."
    End If
  End If
'set template name
objWord.Documents.Add (App.Path & "\accinc.dot")
 
objWord.Selection.GoTo what:=wdGoToBookmark, Name:="mybookmark"
objWord.Selection.TypeText recordset!myField

need something like
objword.formfield("mycheckbox").value = vbchecked
but cannot find!  

Thanks!
0
Comment
Question by:dwadek
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
6 Comments
 
LVL 1

Expert Comment

by:okcman
ID: 2630585
Option Explicit
Dim wrdApp      As Word.Application
Dim wrdDoc      As Word.Document
Dim wrdSelection    As Word.Selection

Private Sub cmdWordCheckBoxes_Click()
   
    Dim StrToAdd        As String
   
    On Error GoTo Error_Handler
       
    'let us know we are doing something
   
    Screen.MousePointer = vbHourglass
   
   
    ' Create an instance of Word  and make it visible
    Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = True
   
    ' Add a new document
    Set wrdDoc = wrdApp.Documents.Add
    wrdDoc.Select
    Set wrdSelection = wrdApp.Selection
 
 Dim x As Integer
 For x = 4 To 1 Step -1
' Create 4 check boxes and insert it into the document
 With ActiveDocument.FormFields.Add(Range:=ActiveDocument.Range _
    (Start:=0, End:=0), Type:=wdFieldFormCheckBox)
    .Name = "Color" & x
    .CheckBox.Value = True
 End With
Next
'checking checkboxes with index values
 ActiveDocument.FormFields(1).CheckBox.Value = False
 ActiveDocument.FormFields(2).CheckBox.Value = True
 ActiveDocument.FormFields(3).CheckBox.Value = False
 ActiveDocument.FormFields(4).CheckBox.Value = True

'checking checkboxes with name values
 ActiveDocument.FormFields("Color1").CheckBox.Value = True
 ActiveDocument.FormFields("Color2").CheckBox.Value = False
 ActiveDocument.FormFields("Color3").CheckBox.Value = True
 ActiveDocument.FormFields("Color4").CheckBox.Value = False
             
   ' wrdDoc.SaveAs ("c:\Test.doc")
   ' wrdDoc.Close False
   

Exit Sub

Error_Handler:
    Screen.MousePointer = vbDefault
    MsgBox "Error: " & Err.Number & vbLf & vbLf & Err.Description, vbExclamation, "Mail Merge Error!"
End Sub




0
 
LVL 1

Expert Comment

by:okcman
ID: 2642576
hello? anybody out there?
0
 
LVL 1

Accepted Solution

by:
okcman earned 50 total points
ID: 2642579
Option Explicit
Dim wrdApp      As Word.Application
Dim wrdDoc      As Word.Document
Dim wrdSelection    As Word.Selection

Private Sub cmdWordCheckBoxes_Click()
     
    Dim StrToAdd        As String
     
    On Error GoTo Error_Handler
       
    'let us know we are doing something
     
    Screen.MousePointer = vbHourglass
     
     
    ' Create an instance of Word  and make it visible
    Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = True
     
    ' Add a new document
    Set wrdDoc = wrdApp.Documents.Add
    wrdDoc.Select
    Set wrdSelection = wrdApp.Selection
 
 Dim x As Integer
 For x = 4 To 1 Step -1
' Create 4 check boxes and insert it into the document
 With ActiveDocument.FormFields.Add(Range:=ActiveDocument.Range _
    (Start:=0, End:=0), Type:=wdFieldFormCheckBox)
    .Name = "Color" & x
    .CheckBox.Value = True
 End With
Next
'checking checkboxes with index values
 ActiveDocument.FormFields(1).CheckBox.Value = False
 ActiveDocument.FormFields(2).CheckBox.Value = True
 ActiveDocument.FormFields(3).CheckBox.Value = False
 ActiveDocument.FormFields(4).CheckBox.Value = True

'checking checkboxes with name values
 ActiveDocument.FormFields("Color1").CheckBox.Value = True
 ActiveDocument.FormFields("Color2").CheckBox.Value = False
 ActiveDocument.FormFields("Color3").CheckBox.Value = True
 ActiveDocument.FormFields("Color4").CheckBox.Value = False
             
   ' wrdDoc.SaveAs ("c:\Test.doc")
   ' wrdDoc.Close False
   

Exit Sub

Error_Handler:
    Screen.MousePointer = vbDefault
    MsgBox "Error: " & Err.Number & vbLf & vbLf & Err.Description, vbExclamation, "Mail Merge Error!"
End Sub
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:dwadek
ID: 2642794
From my reference to a .dot file ...
it should have been apparent that I was trying to modify a copy of an existing document ... not create a new one.

Your answer got me looking in the right
direction ... but the way you approached it had me experimenting with the code and I ended up only needing one line "ActiveDocument.FormFields("Color1").CheckBox.Value = True"
 
For your reference, you must also refer to the "ActiveDocument" with the object or the code will only work the first time the program reference the word object. (i.e. wrdApp.ActiveDocument)

0
 
LVL 1

Expert Comment

by:okcman
ID: 2643108
OK,  I realize that you were using an existing document, however
since I couldn't use the "exact" document you were using, I posted a example of code, that gave you examples of creating , checking and unchecking by index, and checking and unchecking by name value. This I believed could be easily recreated by pasting the code into a blank project, simply by adding a command button,  shows true clear examples of addressing your issue.

Though you may not have wanted to know how to create the checkboxes by VB,and the example of creating the checkboxes were superflous to your particular application, because you were wanting to modify the existing file.In my case since I did not have any of your  documents to play with, I created my own document with checkboxes,and manipulated the values of said checkboxes, and it is obvious that, you have by just looking at the code,  have determined the correct call.


 The example that I gave wasnt just an answer, it was a "proof", something that can be proved that it works(like in math ,programming, etc....), I only post answers that work ...i.e. this code was taken directly out of a
project that worked 100% and pasted into the post message box............though I didn't address your "application" as a .dot file directly, I did address your question  right on the mark....and right on the money....
0
 
LVL 1

Expert Comment

by:okcman
ID: 2643259
OK,  I realize that you were using an existing document, however
since I couldn't use the "exact" document you were using, I posted a example of code, that gave you examples of creating , checking and unchecking by index, and checking and unchecking by name value. This I believed could be easily recreated by pasting the code into a blank project, simply by adding a command button,  shows true clear examples of addressing your issue.

Though you may not have wanted to know how to create the checkboxes by VB,and the example of creating the checkboxes were superflous to your particular application, because you were wanting to modify the existing file.In my case since I did not have any of your  documents to play with, I created my own document with checkboxes,and manipulated the values of said checkboxes, and it is obvious that, you have by just looking at the code,  have determined the correct call.


 The example that I gave wasnt just an answer, it was a "proof", something that can be proved that it works(like in math ,programming, etc....), I only post answers that work ...i.e. this code was taken directly out of a
project that worked 100% and pasted into the post message box............though I didn't address your "application" as a .dot file directly, I did address your question  right on the mark....and right on the money....
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question