[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Populating Word Checkboxes with VB

Posted on 2000-03-16
6
Medium Priority
?
778 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…
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…
Suggested Courses

649 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