Consulting

Results 1 to 9 of 9

Thread: How do I attach a macro to a command button?

  1. #1
    VBAX Newbie
    Joined
    Mar 2006
    Posts
    4
    Location

    Angry How do I attach a macro to a command button?

    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,

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    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
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Newbie
    Joined
    Mar 2006
    Posts
    4
    Location

    How do I attach a macro to a command button?

    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,

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]
    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

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Newbie
    Joined
    Mar 2006
    Posts
    4
    Location
    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?

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Either
    [VBA]
    Private Sub CommandButton1_Click()
    PrintAndClear
    End Sub
    [/VBA]
    or
    [VBA]
    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
    [/VBA]

    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.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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?

  8. #8
    VBAX Newbie
    Joined
    Mar 2006
    Posts
    4
    Location
    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,

  9. #9
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •