• How do I close a Print Preview window from code? (97)

    Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » How do I close a Print Preview window from code? (97)

    Author
    Topic
    #359589

    I’ve tried various methods, like SendKeys and activating the workbook
    windows, etc. but nothing seems to work. The Print Preview window
    seems to be some kind of modal dialog. I’m thinking if I can get the
    hwnd of the dialog then I can use the API to close it. But I don’t
    know how to get the hwnd. Does anyone know what the Window Style and
    caption of the Print preview window is? I could then use FindWindow.
    Thanks,

    Adam Ralph

    Viewing 0 reply threads
    Author
    Replies
    • #539357

      The actual Print Preview window does not seem to have a caption, but has a class name of Excel@ if that is of any help.

      Just curious, but why not let the user close it when they have finished previewing ?

      Andrew

      • #539363

        Thanks Andrew, I’ll experiment with Excel@ and see if I can get it to work. Out of curiosity, how did you find that out?
        With regard to your quesion about letting the user close the preview, I hear what you are saying, but I’m actually trying to solve this for another programmer, who seems to spawn an Excel.Application from code, and then needs to kill Print Preview _if_ a user has switched to print preview mode in that instance of the application.

        • #539382

          How did I find out ?. Check This Thread. There is some code for getting at Class names posted there, but not suitable for Office 97. However I am attaching a VB version compiled to a standalone EXE (zipped), which you can experiment with.

          It is not very sophisticated, but it shows the class name of the window under the mouse pointer.

          Andrew

          • #539595

            Andrew, thanks for the thread. I also have VB so I ran the code from there. I also added a call to GetWindowText to find out the caption. It appears that the Excel@ window has NO caption.
            In fact, I’m beginning to think that this method is not going to work. I don’t think I can just kill the Window since the normal user action is a button press outside the window, and besides, using FindWindow, passing Excel@ as the classname seems risky as it might yield the hwnd of another print preview window somewhere else.

            • #539604

              If it has no name, can you not tell it to close any window where the name is empty? Just a suggestion.

              Although this would risk closing other preview windows, as you say.

            • #539842

              that would not only risk closing other print previews, but also other windows which also have no text. I can’t see the benefit if I already know that the WindowClass is Excel@

            • #539863

              Could you get a handle on all the child windows of the main applcation (in this case Excel), and then close the one with class name EXCEL@ (EXCELB, if I recall in XL 2000). I cannot imagine 2 print previews at the same time in the one application.

              I thought the code included GetWindowText to provide the caption where applicable.

              Andrew

            • #540161

              yes, I think you’re right, I should get a child window of the Application which is Excel@ and try and close that. Whether I can actually do that, or whether I have so simulate the ‘Close’ button click is another question.

        • #539933

          This might be totally useless, but just in case.

          Would it help if you disabbled Print Preview for that particular application?

          Or perhaps you could intercept it, and set a value in a hidden worksheet? When it’s time for your code, check that value? (Though I’m not quite sure how to reset the value when the window is closed).

          Sorry, just trying a little lateral thinking here.

          • #540160

            I’m trying to solve this problem for another programmer, and I don’t believe disabling print preview is an option for him, I believe print preview still needs to be allowable. As for setting a hidden value, I can’t see how that can help in closing the print preview window from code, could you perhaps expand?

    Viewing 0 reply threads
    Reply To: How do I close a Print Preview window from code? (97)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: