PDA

View Full Version : How do I attach a macro to a command button?



tccrep
03-14-2006, 11:02 AM
Hi there,

I'm designing a fax cover sheet that is to be reused on a regular basis. Rather than deleting all the required fields after each time its printed, I need to create a macro using a command button to do this for me.

I have 2 buttons at the bottom of my word form, one to print, the other to clear the text fields.

What code could be used that can be attached to each button so I can clear the designated text & check boxes, and then to print the document?

Thanks, :banghead:

mdmackillop
03-14-2006, 11:24 AM
Hi,
Welcome to VBAX
I don't think that this is the correct approach. If you create your fax cover as a template, you can print then delete the new document with or without saving it. You'll have a new blank fax form each time from the template.
If you're really looking to use it on a regular basis, you should consider setting it up as a mail merge document where you can use a database of recipients details to fill in the basic information.
Let us know how you want to proceed.
Regards
MD

tccrep
03-14-2006, 12:50 PM
Thanks,

I know it seems kind of strange, and the most logical approach would be just to re-open a new file each and every time, but this would be used as a high volume and it would be easier to just delete the fields with a click of a button rather than exiting and re-opening.

I've successfully created this same type of sheet in Excel and all works well, but need to have one similar in Word.

My excel code is below and works great, but need to create something similar in word:

Private Sub CommandButton1_Click()
Range("C11,C12,C13,E14,H14,D17,D18,A36").Select
Range("C11,C12,C13,E14,H14,D17,D18,A36").Activate
Range("A23,A24,A25,A26,A27,A28,A29,A30,A31,A32").ClearContents
CheckBox1.Value = False
CheckBox2.Value = False
CheckBox3.Value = False
CheckBox4.Value = False
CheckBox5.Value = False
CheckBox6.Value = False
CheckBox7.Value = False
CheckBox8.Value = False
CheckBox9.Value = False
CheckBox10.Value = False
Selection.ClearContents
End Sub

Any ideas? Any info would be appreciated.

Thanks,:dunno

mdmackillop
03-14-2006, 02:17 PM
Sub PrintAndClear()
Application.ScreenUpdating = False
ActiveDocument.PrintOut
For Each f In ActiveDocument.FormFields
If f.Type = wdFieldFormCheckBox Then
f.CheckBox.Value = False
Else
f.Result = ""
End If
Next
Application.ScreenUpdating = False
End Sub

tccrep
03-14-2006, 03:00 PM
Thanks,

I've been playing with that code and I can see how it works, but how do I tie that into using it with the command button?

mdmackillop
03-14-2006, 03:34 PM
Either

Private Sub CommandButton1_Click()
PrintAndClear
End Sub

or

Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
ActiveDocument.PrintOut
For Each f In ActiveDocument.FormFields
If f.Type = wdFieldFormCheckBox Then
f.CheckBox.Value = False
Else
f.Result = ""
End If
Next
Application.ScreenUpdating = False
End Sub


The first method is better if you have more than one routine to run and is also self explanatory. You can also store the PrintAndClear routine in a Global macro. It can then be called from any document.

The second is simple and self-contained.

fumei
03-14-2006, 10:29 PM
I'm sorry, but Malcom was correct in his first post. Clearing fields is not the way to go at all.

If you have a a template file (a .dot file), then you get a fresh blank document every time. Simple. Getting a new document could very easily made into a menu item, a toolbar icon, whatever. I don't think you understand what a template is.
I know it seems kind of strange, and the most logical approach would be just to re-open a new file each and every time, but this would be used as a high volume and it would be easier to just delete the fields with a click of a button rather than exiting and re-opening.When you use a template you do NOT re-open a new file each time. There is no re-opening. There is no exiting and reo-opening. I would strongly disagree that it is easier to delete the fields.

In any case, what are you using for a command button?

tccrep
03-15-2006, 02:58 PM
For the purposes that I need it for, it works great. I just needed something that can be filled in, printed and cleared all in one process, and this is what I was looking to do.

As for my point of view as a template, I believe we do have a different opinions. I have acutally never used a .dot file, which would be why I'm confused with that.

This is what I was looking to do, even thought it may not be the best method, but serves my purpose just fine.

Thanks very much, everyone for all the help. This is a great forum.

I'm sure I'll be back. I'm kind of a novice, but would like to sharpen up my skills.

Thanks,

ctengelen
03-20-2006, 01:03 PM
Hi There,

I hope you don't mind if I send you a little file, that will explain the template a bit for you.

This way is so easy and simple to manage your fax cover sheet without the need for delete data.

You can keep your 'print' command button for the print part. If you do not want to save the file, just close and don't save.

Have a look.

Trudy