+ Reply to Thread
Results 1 to 10 of 10

Temporarily stop script

  1. #1
    Registered User
    Join Date
    01-20-2006
    Posts
    19

    Temporarily stop script

    L.S.,

    Within Excel I am running a script that opens a new Word-document.
    I like to stop the script temporarily untill the Word-session is closed.

    Example:

    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True
    Set objDoc = objWord.documents.Add

    Msgbox ("Excel again")

    The message should appear after closing the Word-session.

    Possible?
    With kind regards,
    Hugo

  2. #2
    Tom Ogilvy
    Guest

    RE: Temporarily stop script

    Once word is opened and the document is added, the script moves on.

    You could possibly have it loop and check for the existence of Word.

    --
    Regards,
    Tom Ogilvy


    "H.A. de Wilde" wrote:

    >
    > L.S.,
    >
    > Within Excel I am running a script that opens a new Word-document.
    > I like to stop the script temporarily untill the Word-session is
    > closed.
    >
    > Example:
    >
    > Set objWord = CreateObject("Word.Application")
    > objWord.Visible = True
    > Set objDoc = objWord.documents.Add
    >
    > Msgbox ("Excel again")
    >
    > The message should appear after closing the Word-session.
    >
    > Possible?
    > With kind regards,
    > Hugo
    >
    >
    > --
    > H.A. de Wilde
    > ------------------------------------------------------------------------
    > H.A. de Wilde's Profile: http://www.excelforum.com/member.php...o&userid=30679
    > View this thread: http://www.excelforum.com/showthread...hreadid=538800
    >
    >


  3. #3
    Registered User
    Join Date
    01-20-2006
    Posts
    19

    existence of Word

    Dear Tom,

    thank you for your reply.
    Unfortunately I am not able to check for the existence of Word.
    Which condition should I use to exit the loop?

    With kind regards,
    Hugo

  4. #4
    Tom Ogilvy
    Guest

    Re: Temporarily stop script

    Without testing

    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True
    Set objDoc = objWord.documents.Add
    Set objDoc = Nothing
    do while objWord.Visible
    loop
    set objWord = Nothing
    Msgbox ("Excel again")

    --
    Regards,
    Tom Ogilvy


    "H.A. de Wilde" wrote:

    >
    > Dear Tom,
    >
    > thank you for your reply.
    > Unfortunately I am not able to check for the existence of Word.
    > Which condition should I use to exit the loop?
    >
    > With kind regards,
    > Hugo
    >
    >
    > --
    > H.A. de Wilde
    > ------------------------------------------------------------------------
    > H.A. de Wilde's Profile: http://www.excelforum.com/member.php...o&userid=30679
    > View this thread: http://www.excelforum.com/showthread...hreadid=538800
    >
    >


  5. #5
    Mat P:son
    Guest

    Re: Temporarily stop script

    Or, instead of using loops, why not use an event-based solution? Put the
    below code in an Excel code module, call Initialize() and Terminate() as
    appropriate, and you'll be able to listen to Application Quit and Document
    Close events through the event handlers. I would strongly recommend the use
    of event-based programming whereever and whenever possible, instead of
    implementing tight loops to poll for state information, since the latter will
    consume CPU cycles for virtually no good reason.

    Cheers,
    /MP

    ====================================

    Option Explicit

    Private WithEvents g_oApp As Word.Application
    Private WithEvents g_oDoc As Word.Document

    Private Sub Initialize()
    Set g_oApp = CreateObject("Word.Application")
    Set g_oDoc = g_oApp.Documents.Add

    g_oApp.Visible = True
    End Sub

    Private Sub Terminate()
    Set g_oDoc = Nothing
    Set g_oApp = Nothing
    End Sub

    Private Sub g_oApp_Quit()
    MsgBox "Word App Quit"
    End Sub

    Private Sub g_oDoc_Close()
    MsgBox "Word Doc Close"
    End Sub

    ==============================================


    "Tom Ogilvy" wrote:

    > Without testing
    >
    > Set objWord = CreateObject("Word.Application")
    > objWord.Visible = True
    > Set objDoc = objWord.documents.Add
    > Set objDoc = Nothing
    > do while objWord.Visible
    > loop
    > set objWord = Nothing
    > Msgbox ("Excel again")
    >
    > --
    > Regards,
    > Tom Ogilvy
    >
    >
    > "H.A. de Wilde" wrote:
    >
    > >
    > > Dear Tom,
    > >
    > > thank you for your reply.
    > > Unfortunately I am not able to check for the existence of Word.
    > > Which condition should I use to exit the loop?
    > >
    > > With kind regards,
    > > Hugo
    > >
    > >
    > > --
    > > H.A. de Wilde
    > > ------------------------------------------------------------------------
    > > H.A. de Wilde's Profile: http://www.excelforum.com/member.php...o&userid=30679
    > > View this thread: http://www.excelforum.com/showthread...hreadid=538800
    > >
    > >


  6. #6
    Registered User
    Join Date
    01-20-2006
    Posts
    19

    stop running

    Dear Tom,

    The loop works but is not working fine.
    When the Word-document is minimized or there is a switch to another application, the system does't perform good. Alos the return to Excel is very slow. I think the loop is too risky. Sometimes an error occurs.


    Dear Cheers,

    Pasting the text:

    Private WithEvents g_oApp As Word.Application
    Private WithEvents g_oDoc As Word.Document

    into a module, it turns into red.

    Thanks for your replies.

    Kind regards,
    Hugo

  7. #7
    Mat P:son
    Guest

    Re: Temporarily stop script



    "H.A. de Wilde" wrote:

    >
    > Dear Tom,
    >
    > The loop works but is not working fine.
    > When the Word-document is minimized or there is a switch to another
    > application, the system does't perform good. Alos the return to Excel
    > is very slow. I think the loop is too risky. Sometimes an error
    > occurs.
    >
    >
    > Dear Cheers,
    >
    > Pasting the text:
    >
    > Private WithEvents g_oApp As Word.Application
    > Private WithEvents g_oDoc As Word.Document
    >
    > into a module, it turns into red.
    >
    > Thanks for your replies.
    >
    > Kind regards,
    > Hugo
    >
    >
    > --
    > H.A. de Wilde
    > ------------------------------------------------------------------------
    > H.A. de Wilde's Profile: http://www.excelforum.com/member.php...o&userid=30679
    > View this thread: http://www.excelforum.com/showthread...hreadid=538800
    >
    >


  8. #8
    Mat P:son
    Guest

    Re: Temporarily stop script

    In order to use the code fragments I put together you have to properly add a
    reference to the type library of MS Word; this is because I was using the
    early-bound types Word.Application and Word.Document to declare the global
    variables.

    Hence, in the VBA editor, go to Tools > References, and in the References
    dialogue add a reference to the Microsoft Word Object Library.

    You can then paste the two global variable declarations (g_oApp and g_oDoc)
    into the General Declarations section of, for example, the ThisWorkbook code
    module.

    HTH,
    /MP

    "H.A. de Wilde" wrote:

    >
    > Dear Tom,
    >
    > The loop works but is not working fine.
    > When the Word-document is minimized or there is a switch to another
    > application, the system does't perform good. Alos the return to Excel
    > is very slow. I think the loop is too risky. Sometimes an error
    > occurs.
    >
    >
    > Dear Cheers,
    >
    > Pasting the text:
    >
    > Private WithEvents g_oApp As Word.Application
    > Private WithEvents g_oDoc As Word.Document
    >
    > into a module, it turns into red.
    >
    > Thanks for your replies.
    >
    > Kind regards,
    > Hugo
    >
    >
    > --
    > H.A. de Wilde
    > ------------------------------------------------------------------------
    > H.A. de Wilde's Profile: http://www.excelforum.com/member.php...o&userid=30679
    > View this thread: http://www.excelforum.com/showthread...hreadid=538800
    >
    >


  9. #9
    Tom Ogilvy
    Guest

    Re: Temporarily stop script

    I think Mat's concept is a much better one. Perhaps he will share with you
    how to implement it.

    --
    Regards,
    Tom Ogilvy


    "H.A. de Wilde" wrote:

    >
    > Dear Tom,
    >
    > The loop works but is not working fine.
    > When the Word-document is minimized or there is a switch to another
    > application, the system does't perform good. Alos the return to Excel
    > is very slow. I think the loop is too risky. Sometimes an error
    > occurs.
    >
    >
    > Dear Cheers,
    >
    > Pasting the text:
    >
    > Private WithEvents g_oApp As Word.Application
    > Private WithEvents g_oDoc As Word.Document
    >
    > into a module, it turns into red.
    >
    > Thanks for your replies.
    >
    > Kind regards,
    > Hugo
    >
    >
    > --
    > H.A. de Wilde
    > ------------------------------------------------------------------------
    > H.A. de Wilde's Profile: http://www.excelforum.com/member.php...o&userid=30679
    > View this thread: http://www.excelforum.com/showthread...hreadid=538800
    >
    >


  10. #10
    NickHK
    Guest

    Re: Temporarily stop script

    Hugo,
    In a class module:
    Dim WithEvents MyWord As Word.Application

    Private Sub MyWord_Quit()
    MsgBox "Excel again"
    End Sub

    Your original sub would need to end before then original MsgBox.
    But you will then be notified when Word quits.

    NickHK

    "H.A. de Wilde" <[email protected]>
    wrote in message
    news:[email protected]...
    >
    > L.S.,
    >
    > Within Excel I am running a script that opens a new Word-document.
    > I like to stop the script temporarily untill the Word-session is
    > closed.
    >
    > Example:
    >
    > Set objWord = CreateObject("Word.Application")
    > objWord.Visible = True
    > Set objDoc = objWord.documents.Add
    >
    > Msgbox ("Excel again")
    >
    > The message should appear after closing the Word-session.
    >
    > Possible?
    > With kind regards,
    > Hugo
    >
    >
    > --
    > H.A. de Wilde
    > ------------------------------------------------------------------------
    > H.A. de Wilde's Profile:

    http://www.excelforum.com/member.php...o&userid=30679
    > View this thread: http://www.excelforum.com/showthread...hreadid=538800
    >




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1