Solved

Populating Word Checkboxes with VB

Posted on 2000-03-16
6
763 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

770 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