• Quiting an application

    Author
    Topic
    #352405

    i used this code in a button to quit my power point project
    Sub goaway()

    With Application
    For Each w In .Presentations
    w.Save
    Next w

    End With
    Application.Quit

    End Sub

    this code worked for a while but now does not work
    can anyone tell me what is wrong
    i’m new to power point so any help would be appreciated
    Thanks
    JerryC

    Viewing 1 reply thread
    Author
    Replies
    • #512581

      Hi Jerry:

      A couple of questions:
      Does any of it still work, or is it completely not working?
      Where do you have this code stored?

      Also a comment, which is that it doesn’t look like you’re telling PowerPoint to close each presentation, just save it. It would probably be a good idea to explicitly close each one, in the code (add a “w.close” line after “w.save”).

      Gary

      • #512920

        IT LOOKS LIKE THE SAVE PART IS WORKING
        BUT DOES NOT CLOSE THE APPLICATION
        I TRIED USING THE CODE IN ANOTHER PRESENTATION
        AND THE CODE WORKED A FEW TIMES AND STOPPED WORKING IN THAT ONE TOO
        PLEASE ANY IDEAS WOULD BE GREATLY APPRECIATED
        THANKS
        JERRYC

        • #513086

          Hi Jerry,

          I had a look at the PPT VBA help, and the example they give for Application.Quit is virtually identical to the one you first posted.

          (and I guess the .Close command which I suggested trying is not necessary and may be counterproductive – once all the presentations are saved, quitting the application will close all the presentations. And closing the presentation which contains the code while there are still others open, would probably stop things in their tracks.)

          Code that works a few times, and then doesn’t, is a really tough one to suss out.
          Is there any pattern you can describe to when it works and when it doesn’t?
          Also btw, what version of PPT and what OS are you running (not that that should matter).

          Sorry not to have any better ideas. There is a Tasks collection available in Word VBA which allows you to “End Task” on any open application. Unfortunately this feature is only available from within Word VBA so the only way to run this from another Office app would be via Automation.

          I’d imagine that would be a little peculiar – from PPT VBA, you would need to create a new Word application object, add a document, perhaps switch to Word’s VB Editor, add a code module and add the code required to do the Task.Quit operation, and then execute that code from within Word – don’t know if all of the above is possible.
          And while this might work to close the PPT app, it would leave you with an open Word document that you didn’t want.

          If I get a chance to try this out and get something to work, I’ll post back.

          Gary

          • #513094

            OK, here’s an example that seems to work on my setup.
            You’ll need to follow several steps to get this set up:

            First, open Word and create a new document.
            Save it as a template (“.dot”) file, and name it “TasksCode.dot”).
            With this still open, switch to the VB Editor (F11), and insert a new standard code module.
            In the code module, insert the following procedure:

            Sub EndTaskPPT()
            If Tasks.Exists(“Microsoft PowerPoint”) = True Then
            Tasks(“Microsoft PowerPoint”).Close
            End If
            Application.Quit
            End Sub

            This code, when run in Word, should close both PowerPoint and Word.
            (You can now save and close the new template. Important: make note of the file path of the location where you have saved the template.)

            Now, we need to set up a means to run this code in Word, but from PowerPoint:

            Open the PowerPoint presentation that you’re going to want to run the code from.
            Go to the VB Editor (F11) and insert a new standard code module.
            Go to Tools>References, and scroll down in the list of Available References until you see “Microsoft Word 9.0 Object Library” (or “8.0” if you’re using Office 97). Click a checkmark next to this and then click on OK.

            Now, paste the following code into the PPT code module:

            Sub QuitPPT_Via_WordAutomation()
            Dim wdApp As Word.Application
            Dim docTmp As Document
            Dim aPres As Presentation

            With Application
            For Each aPres In .Presentations
            aPres.Save
            Next aPres
            End With

            Set wdApp = New Word.Application
            With wdApp
            ‘Note: your Word template path will probably be different:
            .Documents.Add Template:=”D:WordDataTemplatesTasksCode.dot”
            .Application.Run MacroName:=”EndTaskPPT”
            End With
            End Sub

            When you run the QuitPPT_Via_WordAutomation procedure from PowerPoint, all open presentations should save, and PowerPoint should quit.

            It would probably be a good idea to not have any open Word files, when you use this procedure.
            And it would probably be a very good idea to test this the first time, on some dummy documents rather than your live ones!

            Give this a try and let me know if it works on your setup.

            Gary

            • #513640

              Thanks Gary
              I’ll give it a try as soon as I can
              thanks again
              JerryC

          • #523461

            > create a new Word application object, add a document, perhaps switch to
            Word’s VB Editor, add a code module and add the code required to do the Task.Quit operation, and then execute that code from within Word – don’t
            know if all of the above is possible.

            Yes it is. I’ve done all of the above, but not in that sequence. I have code that invokes Word, code that adds a code module with procedures, code that executes a macro from another application.

            • #523644

              Stringing these together, entirely programatically from PPT, turns out to be quite a challenge.

              The sticking point to creating the needed code on the fly in Word is that you need to set a reference to the VB Extensibility Library – preferably only for the temp document you create via Documents.Add – first before you can generate the code into a code module.

              But in order to set the reference, you first need to activate the VB Project for that document, not an easy task considering (1) the document is born without public modules, (2) you don’t have the use of the properties or methods available in VB Extensibility, because that reference hasn’t been set yet! crazy

              It gets kind of circular. If I had to bet, I’d say there’s some way to get it done, even if it involves something like SendKeys.

              (Anyway, I thought my QuitPPTTask.dot was a neat workaround)

            • #523645

              Hi Chris,

              I couldn’t resist my own challenge.
              Here’s code that gets about 98% there – the only thing left is a pesky prompt to save the Word document – I’ll leave that one to you or I’ll try again tomorrow – wait – it already is tomorrow! yawn

              Sub QuitPPT_Via_WordAutomation()
              'Gary Frieder  April 2001
              'Run from a PPT presentation
              'Requires a reference set to Word library
              'as well as a reference set in PPT to
              'VBA extensibility
              
              Dim wdApp As Word.Application
              Dim wdTmpDoc As Word.Document
              Dim docTmp As Document
              Dim aPres As Presentation
              Dim lngProjCt As Long
              Dim VBComp As VBComponent
              Dim strQuitCode As String
              strQuitCode = _
                  "Sub EndTaskPPT()" & vbCr & _
                  "If Tasks.Exists(" & Chr(34) & "Microsoft PowerPoint" & Chr(34) & ")" & _
                  "= True Then" & vbCr & "Tasks(" & Chr(34) & "Microsoft PowerPoint" & Chr(34) & ").Close" & _
                  vbCr & "End If" & vbCr & "Application.Quit" & vbCr & "End Sub"
              
              With Application
              For Each aPres In .Presentations
              aPres.Save
              Next aPres
              End With
              
              Set wdApp = New Word.Application
              With wdApp
                  '.Visible = True
                  Set wdTmpDoc = .Documents.Add
                  lngProjCt = wdApp.VBE.VBProjects.Count
                  With wdApp.VBE.VBProjects.Item(lngProjCt)
                      .References.AddFromFile _
                      "D:Program FilesCommon FilesMicrosoft SharedVBAVBA6VBE6EXT.OLB"
                      Set VBComp = .VBComponents.Add(vbext_ct_StdModule)
                      With VBComp
                         .CodeModule.AddFromString strQuitCode
                      End With
                  End With
                 .Run MacroName:="EndTaskPPT"
                  
              'In theory, PPT closes before this line runs:!
              'Set wdApp = Nothing
              End With
              End Sub

              Gary

            • #523649

              OK. I looked at this around 6:30 this morning and decided it was too nice a day to run it (grin!). Tell me again what it does?

              I looked for the Word chunk but couldn’t spot it.

            • #523651

              Hi Chris,

              Our posts just crossed – it’s 7am – we’re both sick puppies doing this eh?

              The code:
              Saves every presentation in the PPT session.
              Creates a new Word application session – not visible though.
              Creates a new empty document based on Normal.dot.
              Adds a reference in this new document’s VB Project, to the VBA Extensibility library.
              (The tricky part of ensuring that the correct VB Project is active (and not, for example, Normal.dot’s project), is done by getting a count of current projects. The new document’s project is going to have the highest indext number.)
              It then adds a standard code module to this document’s VB Project.
              In this code module, it adds a string which contains the lines of code for a procedure (“EndTaskPPT”).
              This procedure contains the code to end the PPT session via Tasks, as well as quit the temporary Word session.
              The last statement in the procedure tells the Word app to run the EndTaskPPT macro.
              The EndTaskPPT macro then runs, shutting down both PPT and Word.
              Poof – everything’s gone, clean, no processes left behind. cool

              Gary

            • #523688

              Now I remember. You said that PowerPoint users have to write a Word app to shut down Powerpoint, which Word app also must shut down itself.

              I wish I’d thought of that!

              No wonder I can’t get a job at Microsoft.

              Your solution gives me new standards to aim for.

              I’ll let lounge members decide in which direction (grin!)

              7am for you. 6am for me. It takes me AGES to adjust my clocks. My girlfriend is much better than I. She waits until around 11pm or 1am (depending on the season) and runs around the house unplugging and replugging every device with a digital clock. Bedside alarm, VCR, microwave, oven etc.

              The flashing stuff drives me NUTS! (I nearly wrote “The flashings drive me smuts”!)

            • #523650

              This appears to work nicely now – the PPT session vaporizes, and there are no PPT nor Word processes left behind.
              Just adding a “ActiveDocument.Saved = True” statement takes care of the pesky prompt. So:

              strQuitCode = _
                  "Sub EndTaskPPT()" & vbCr & _
                  "If Tasks.Exists(" & Chr(34) & "Microsoft PowerPoint" & Chr(34) & ")" & _
                  "= True Then" & vbCr & "Tasks(" & Chr(34) & "Microsoft PowerPoint" & Chr(34) & ").Close" & _
                  vbCr & "End If" & vbCr & "ActiveDocument.Saved = True" & _
                  vbCr & "Application.Quit" & vbCr & "End Sub"
              

              And the entire procedure, cleaned up a bit more:

              Sub QuitPPT_Via_WordAutomation()
              'Gary Frieder  April 2001
              'Run from a PPT presentation
              'Requires a reference set to Word library
              'as well as a reference set in PPT to
              'VBA extensibility
              
              Dim wdApp As Word.Application
              Dim wdTmpDoc As Word.Document
              Dim aPres As Presentation
              Dim lngProjCt As Long
              Dim VBComp As VBComponent
              Dim strQuitCode As String
              strQuitCode = _
                  "Sub EndTaskPPT()" & vbCr & _
                  "If Tasks.Exists(" & Chr(34) & "Microsoft PowerPoint" & Chr(34) & ")" & _
                  "= True Then" & vbCr & "Tasks(" & Chr(34) & "Microsoft PowerPoint" & Chr(34) & ").Close" & _
                  vbCr & "End If" & vbCr & "ActiveDocument.Saved = True" & _
                  vbCr & "Application.Quit" & vbCr & "End Sub"
              
              With Application
              For Each aPres In .Presentations
              aPres.Save
              Next aPres
              End With
              
              Set wdApp = New Word.Application
              With wdApp
                  '.Visible = True
                  .Documents.Add
                  lngProjCt = wdApp.VBE.VBProjects.Count
                  With wdApp.VBE.VBProjects.Item(lngProjCt)
                      .References.AddFromFile _
                      "D:Program FilesCommon FilesMicrosoft SharedVBAVBA6VBE6EXT.OLB"
                      Set VBComp = .VBComponents.Add(vbext_ct_StdModule)
                      With VBComp
                         .CodeModule.AddFromString strQuitCode
                      End With
                  End With
                 .Run MacroName:="EndTaskPPT"
                  
              'PPT closes before this line runs:!
              'Set wdApp = Nothing
              End With
              End Sub
              
    • #514185

      Hi Phil,

      Glad you’ve got something that’s working.
      In the testing I did regarding Jerry’s problem (which involved code running directly in PPT), I found that it turned out not to be necessary to close the files before quitting – as long as they were saved, the App.Quit would close them as well.

      The problem Jerry was having is that this would work a few times and then stop – always hard to track that kind of thing down.

      The suggestion I posted for Jerry does an end-run around the whole Quit issue by using the Task.Close method, which surprisingly is only available in Word. Surprising because it’s such a potentially useful feature – kind of like having a Task Manager you can control from code.

      Gary

      • #519284

        sorry for not getting back any sooner
        the problem wound up being with our IT department they were resetting the macro security on boot up that is why it worked when we wrote the macro but not the next time we tried it
        thanks
        for all your help
        JerryC

    Viewing 1 reply thread
    Reply To: Quiting an application

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

    Your information: