Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Tennessee
    Posts
    194
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Quiting an application

    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

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Quiting an application

    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

  3. #3
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Tennessee
    Posts
    194
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Quiting an application

    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

  4. #4
    philmarcus
    Guest

    Re: Quiting an application

    Jerry,
    FWIW. Also trying to app.quit. Trying to run slideshow from inside Access with VBA. Can run it fine, but not return to Access automatically. Hope soemone has some ideas.
    Phil

  5. #5
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Quiting an application

    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

  6. #6
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Working( hopefully) example

    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

  7. #7
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Tennessee
    Posts
    194
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Working( hopefully) example

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

  8. #8
    philmarcus
    Guest

    Re: Working( hopefully) example

    I think the key to QUIting is to close all open presentations, but - and this is critical - to set .saved=true first.

    Here is a snippet I ma now using:

    ' Keep a watch till the slide show is over
    Do
    DoEvents
    Loop Until oPPT.SlideShowWindows(1).View.State = ppSlideShowDone
    'oPPT is a Powerrpoint object


    ' Close all open presentations and quit PowerPoint
    Do Until oPPT.Presentations.Count = 0
    oPPT.Presentations(1).Saved = True ' so it will not try to save//a close w/o this will not work, and then the following .quit will not work!
    oPPT.Presentations(1).Close
    Loop

    oPPT.Quit
    Set oPPT = Nothing


    /s/Phil

  9. #9
    philmarcus
    Guest

    Re: Working( hopefully) example

    Oops.

    To be sure it works reliably, put DoEvents after every loop.

    Phil

  10. #10
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Working( hopefully) example

    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

  11. #11
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Tennessee
    Posts
    194
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Working( hopefully) example

    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

  12. #12
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Quiting an application

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

  13. #13
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Quiting an application

    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! <img src=/S/crazy.gif border=0 alt=crazy width=15 height=15>

    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<g>)

  14. #14
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: (Mostly) working code

    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! <img src=/S/yawn.gif border=0 alt=yawn width=15 height=15>

    <pre>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</pre>

    Gary

  15. #15
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: (Mostly) working code

    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.

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
  •