Solved

Populating Word Checkboxes with VB

Posted on 2000-03-16
6
766 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
  • 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
message box in access 4 51
vb6 - Transfer from MSHFlexgrid1 to xls issue 8 54
Advice in Xamarin 21 96
RUNRMTCMD from AS/400 12 68
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

856 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