Solved

Populating Word Checkboxes with VB

Posted on 2000-03-16
6
756 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
Comment Utility
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
Comment Utility
hello? anybody out there?
0
 
LVL 1

Accepted Solution

by:
okcman earned 50 total points
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:dwadek
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
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…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now