Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    4 Star Lounger
    Join Date
    Apr 2002
    Location
    Northern Territory, Australia
    Posts
    471
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Command Button to Show Userform (Hidden) (Word 97)

    I came across this example of "How to hide a

  2. #2
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Command Button to Show Userform (Hidden) (Word 97)

    Kerry

    This works "easily" because it's a button that actually incorporates the print process. If your button serves a different purpose (to printing) you can use the same kind of code to hide/show the button, but it will have to do its stuff in the DocumentBeforePrint event, as detailed in the Alternative Solution on that page.

    I'm not sure of which other events you'd need to modify though. The article mentions FilePrint and FilePrintDefault as well as DocumentBeforePrint. You'll also have to ensure you're targetting the right shape, if there's more than one in the document.

    Alan

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Command Button to Show Userform (Hidden) (Word 97)

    If your userform is named UserForm1, you can use UserForm1.Show:

    Private Sub CommandButton1_Click()
    UserForm1.Show
    End Sub

  4. #4
    4 Star Lounger
    Join Date
    Apr 2002
    Location
    Northern Territory, Australia
    Posts
    471
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Re: Command Button to Show Userform (Hidden) (Word 97)

    I dont want to use this to print at all. I want to use it to show the userform again to allow for edits. There will only be one command button. Its the show / hide code I am looking for,

    Kerry

  5. #5
    4 Star Lounger
    Join Date
    Apr 2002
    Location
    Northern Territory, Australia
    Posts
    471
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Re: Command Button to Show Userform (Hidden) (Word 97)

    Sorry Alan - I didnt quite follow your answer before and I jumped the gun with my reply. Talking this through has made me see you are right - I understand now that the DocumentBeforePrint event is probably the best option, however, I have no idea how to put that together.

    I used Hans' code to make the form show (Thanks Hans).

    Kerry

  6. #6
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Command Button to Show Userform (Hidden) (Word 97)

    I'm afraid I'm in the dark here too. But I don't think the DocumentBeforePrint Application Event is available in '97 anyway. The code you quoted effectively hides the button, prints the doc, then unhides the button. How you would manage the unhide eludes me; unless there is a complimentary "AfterPrint" event ???

    You might have luck following the instructions for intercepting the FilePrint and FilePrintDefault commands. You could try placing your quoted code directly into them.

    Alan

  7. #7
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Command Button to Show Userform (Hidden) (Word 97)

    Hans answered the original question. If you'd like to adapt the MVPs code to hide your button, you can override the normal print behavior by creating two macros:

    Sub FilePrintDefault()
    'your code goes here; no dialog; like MVPs
    End Sub

    Sub FilePrint()
    'your code goes here; shows dialog; a bit more work
    End Sub

  8. #8
    4 Star Lounger
    Join Date
    Apr 2002
    Location
    Northern Territory, Australia
    Posts
    471
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Re: Command Button to Show Userform (Hidden) (Word 97)

    Thanks for the tip, but I have no idea what code goes "here" and where I would put this code. In a macro, module, this document, my form???

    My humble guess was CommandButton1.Visible = False but that didnt work.

    Please help!

    Kerry

  9. #9
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Command Button to Show Userform (Hidden) (Word 97)

    Kerry

    I think Jefferson was referring to the code:

    With ActiveDocument
    .Shapes(1).Visible = msoFalse
    .PrintOut Background:=False
    .Shapes(1).Visible = msoTrue
    End With

    in the procedure

    Sub FilePrintDefault()
    'your code goes here; no dialog; like MVPs
    End Sub

    Have a look at the topic "Modifying a Word Command" in the Word VBA help file (type in "command" to find it). This will explain the details.

    Alan

  10. #10
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Command Button to Show Userform (Hidden) (Word 97)

    As you know, there are two ways users typically print a document: using the "instant print" toolbar button (which runs the internal command FilePrintDefault) and using Ctrl+P or the File>Print menu item (which runs the internal command FilePrint). Alan's covered the first one, assuming the button in the document is the first "shape." The second case is a bit more work because you want to give the user the dialog. So you could do something like this:

    <pre>Sub FilePrint()
    ' Show dialog and, if user prints, hide button/print/unhide button
    Dim blnBGPrint As Boolean
    ' Show Print dialog
    With Dialogs(wdDialogFilePrint)
    ' If user clicks OK to print...
    If .Display() = -1 Then
    If ActiveDocument.Shapes.Count > 0 Then
    ' Hide first shape
    ActiveDocument.Shapes(1).Visible = msoFalse
    ' Check user's background printing setting
    blnBGPrint = .Application.Options.PrintBackground
    ' Turn off background printing if necessary
    If blnBGPrint Then _
    .Application.Options.PrintBackground = False
    ' Execute the user's settings from the print dialog
    .Execute
    ' If user prefers background printing, restore it
    If blnBGPrint Then _
    .Application.Options.PrintBackground = True
    ' Make the first shape visible again
    ActiveDocument.Shapes(1).Visible = msoTrue
    Else
    ' Execute the user's settings from the print dialog
    Debug.Print "No shapes found. Might be InlineShapes..."
    .Execute
    End If
    End If
    End With
    End Sub</pre>

    This macro will run automatically upon File>Print (assuming it is in the template associated with the form document). I tested this very lightly, but I think it should work for most documents. Note that the button can only be hidden if it is not inline with text. Select the button and choose Format>Control (assuming you inserted this button from the Control toolbox) to set the Layout for the button.

    To complete the picture, the code behind the button would be the code that calls up your userform. Separate little bit of code, I think someone already posted it. Hope this helps.

  11. #11
    4 Star Lounger
    Join Date
    Apr 2002
    Location
    Northern Territory, Australia
    Posts
    471
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Re: Command Button to Show Userform (Hidden) (Word 97)

    Jefferson thank you for your assistance, but I have inserted this code and done what I thought was to format the command button to be not inline with text and still no luck.

    In my first example I had the control in a text box and that did not work, so I took it out of the text box and still no go.

    I have attached my example which may help clarify the problem.

    Kerry
    Attached Files Attached Files

  12. #12
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Command Button to Show Userform (Hidden) (Word 97)

    Well, you've been foiled by a stray shape. Check it out. Delete that and your command button becomes .Shapes(1) and you're good to go.
    Attached Images Attached Images

  13. #13
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Command Button to Show Userform (Hidden) (Word 97)

    Kerry

    As Jefferson pointed out, you ended up with a stray shape. Your shapes were given default names "Rectangle 2" and "Control 7". As I suggested in an earlier post, it's worthwhile giving a more meaningful name to a shape you're going to be referencing in VBA. You can do this using something like:

    activedocument.Shapes(1).name = "cmbShowInputForm"

    You can then reference it as .Shapes("cmbShowInputForm") in your code, and it becomes much easier to debug if you're chasing down something you know the name of.

    Alan

  14. #14
    4 Star Lounger
    Join Date
    Apr 2002
    Location
    Northern Territory, Australia
    Posts
    471
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Re: Command Button to Show Userform (Hidden) (Word 97)

    Oh my goodness! <img src=/S/doh.gif border=0 alt=doh width=15 height=15> Thankyou Jeffereson. <img src=/S/clapping.gif border=0 alt=clapping width=19 height=23> I would never have found that.

    Kerry

  15. #15
    4 Star Lounger
    Join Date
    Apr 2002
    Location
    Northern Territory, Australia
    Posts
    471
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Solved

    Once again you are correct Alan. Please forgive me for not always totally grasping your suggestions. <img src=/S/scratch.gif border=0 alt=scratch width=25 height=29> Sometimes the pennies drop later on down the track, like now. I am still a learner and a lot of the VB terminology is only just starting to "click" with me. My best learning is done by looking at a working examples. I cut and paste bits and pieces that work and build on it. Sometimes a little knowledge is dangerous! But on the other hand I AM slowly learning. <img src=/S/snail.gif border=0 alt=snail width=21 height=17>

    Thanks to you and Jefferson I think I have this one solved! <img src=/S/thankyou.gif border=0 alt=thankyou width=40 height=15>

    Kerry

Page 1 of 2 12 LastLast

Posting Permissions

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