We help IT Professionals succeed at work.

VBA Code to have Checkbox Print a Word Document

I have a very simple form in Word 2007 that users enter data that is put on a Word document. In some instances users want the form to be filled out manually and have requested a checkbox on the userform to allow them to just print the document rather than fill our the form.

I have attached a sample with just two textboxes and the one checkbox needed.

Appreciate any help you can provide.
Sub Name_and_Date_Form()
Dim oMyForm As frmName_and_Date
Dim oDoc As Document
Dim sDeponentName As String
Dim sDate As String


Set oDoc = ActiveDocument
Set oMyForm = New frmName_and_Date
With oMyForm
    .Tag = "CANCEL"
    .Show
    
    sDeponentName = .txtDeponentName.Text
    sDate = .txtDate.Text
   
    
End With
Unload oMyForm
Set oMyForm = Nothing
CompleteBookmark sDeponentName, "Deponentname", oDoc
CompleteBookmark sDate, "Date", oDoc



Set oDoc = Nothing
End Sub
Private Sub CompleteBookmark(theText As String, BookmarkName As String, oDoc As Document)
Dim rngBookmark As Range
If ActiveDocument.Bookmarks.Exists(BookmarkName) Then
Set rngBookmark = ActiveDocument.Bookmarks(BookmarkName).Range
rngBookmark.Text = theText
oDoc.Bookmarks.Add BookmarkName, rngBookmark
Else: MsgBox ("The template is missing the" & BookmarkName _
  & "bookmark." & vbCr & vbCr _
  & "Either the template is corrupted or someone" & _
   "has edited it and removed the bookmark.")
End If
Set rngBookmark = Nothing
End Sub

Open in new window

CheckboxScreenCapture.JPG
Comment
Watch Question

Commented:
It appears that you have left out some vital information.
Are the values in the two text boxes currently mandatory, i.e. require non-empty values?

You could provide a third button (rather than a check box) that says "Manual Entry" with more complete tool tip text. This button would skip the mandatory field validation code and allow them to print without filling in the text boxes. I would suggest you leave the Ok button as the default and place the new button after Ok in the tab order and between the Ok and Cancel buttons.

Author

Commented:
Hi Kuroil,

Sorry about that; the values in the text boxes are not mandatory. I see the logic in your suggestion to place a button between the Ok and Cancel buttons; however the original forms were in an old version of WordPerfect (more than one document needs this functionality) that many people are accustomed to using with checkboxes.

If you can provide the checkbox solution and the button (sorry, but I am compelled to ask) I could roll them both out so they can decide which to use?  In either case I agree that the Ok button should remain.

Thank you for your assistance,

Commented:
Ok. Still missing some details here.
Is this form meant to be used as part of an AutoNew macro,
e.g. appears once when a new document is created from a template?
Then the user will be printing in the usual way?
or
Does the form appear when the user opens the .doc and they are expected to fill it in and then print, and also possibly save the file as a new one?

Commented:
The date field. Do they expect a nice date selector control or just raw text entry with validation?
Are the users American Lawyers, because they'll expect to be able to use M/d/YY format probably, right.

Author

Commented:
Hi Kuroji,

It is supposed to appear once when a new doc is created, the user will print the file and save the file with the data that was input. The idea is that the filled-out document is printed and then saved as a new document, while the template remains unchanged.

Thanks again for your persistance; sorry for the incomplete informationl

Author

Commented:
The users are secretaries for American lawyers and frequently type the date as: December, 3, 2011
It varies from secretary to secretary and even by document; that's why I did not specify a date format.

Author

Commented:
However, a nice date control would be very helpful for other tasks I have before me; but I don't want to impose on you.  Thanks.

Commented:
If the date format varies so much a date control would probably cause grief anyway since they would be 'forced' to conform to a particular format on output in the form leading to more manual printings (a bad thing).
Commented:
Here you are. Enjoy the attached doc.
Code is provided as is etc. etc... :-)

I am hoping you have the necessary skiills to transfer this stuff into your own template document.

The form is activated via the Document_Open event.
You may want to move it to Document_New but it is designed to work either way.

Module1 ShowDepositionForm is for testing purposes.

The document itself shows how the custom properties are used to fill in the necessary parts of the document, i.e. within a printable form.
Q-27476417.doc

Author

Commented:
Hi Kuroji,

Your solution works perfectly, and executes quickly; clearly you're a guru at coding.  I'm new to Expert-Exchange and have to say that my faith in humanity has been elevated.  Perhaps to you, this was not a big deal, but to me it represents a monumental extension of goodwill from you to me.  

I have just begun my path in coding, but am encouraged by what you have shown me can be accomplished when done expertly.  Perhaps someday I can be expert enough to return the favor to somebody else.

In the meantime, I want to once again thank you for your help.  If it were possible, I would double the points to you.

Ron

Author

Commented:
I've requested that this question be closed as follows:

Accepted answer: 250 points for kuroji's comment http:/Q_27476417.html#37232288
Assisted answer: 0 points for Gollum10's comment http:/Q_27476417.html#37232314
Assisted answer: 250 points for kuroji's comment http:/Q_27476417.html#37232996

for the following reason:

To all the other folks who are in need of solutions; you will be lucky indeed if you get Kuroji to assist you. &nbsp;Clear, concise and perceptive questions asked of you to clarify your need, will shortly result in an elegant solution. &nbsp;Expert-Exchange is what the Interned should be; people helping people succeed.<br /><br />Thank you Kuroji and Expert-Exchange. &nbsp;The best of humanity is alive and well, right here.

Commented:
Thanks Gollum10. It was my pleasure to assist you.
Pay it forward mate.

Author

Commented:
I don't mean to object, but I was a bit premature in accepting the solution.  It does work in Word 97-2003, but not in Word 2007.  I just want to recontact Kuroji to see what can be done about the issue. In any event, I'm not objecting to it.

Thanks

Commented:
Sorry Gollum10. It was developed in Word 2003.
The code ought to be quite generic so please post what the issue appears to be and I shall try to help as best I can without having access to that version of Word.

Author

Commented:
Thank Kuroji, I will do so tomorrow; nearly 12 am here in San Diego.  I will play with it a bit and then get back to you.

In any event, this in no way diminishes my respect and admiration for you; on the contrary I hold you in higher esteem for it. By the way, the poetry on your website is thought-provoking and insightful; a bit Haiku, a bit Dylanesque. I too wish we could figure out how to decouple from consumerism and preserve our environment.

Thanks again.

Author

Commented:
Koroji,

I "merged" the your code and mine in a new project and made slight changes in the "ThisDocument" section and everything works great!  Opens as Template, data goes in (unless users checks the checbos - document prints) and if user wants to save the template , they cannot, but (as is normal for a template) they must save as a local document.

Fantastic and you were absolutely correct Word 2003 VBA code runs perfectly when saved as a Word 2007 dotm.

Thanks again for your help; will pay forward at the very next opportunity, but expect no pay back.

Glad to know there are kindred spirits out there in the world.