How to execute an external program and halting VBA code till external program finishes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Greg Fierro

    How to execute an external program and halting VBA code till external program finishes

    I would appreciate any help from anyone with the following:

    I have an external program (window32 based) that I am executing with the
    VBA SHELL command. This program produces a text file which I have to
    read after the external program finishes. I use the transfertext method
    to read the file.

    The problem is that the SHELL command executes external programs in an
    asynch way. In other words, the VBA code will continue running and NOT
    wait for the external program to finish.

    I need to run this external program and HALT my VBA code until the
    external program terminates. Is there any code out there or methods I
    can use to do this??

    Any help is greatly appreciated!

    Thanks!
    Greg Fierro
    gfierro@SoftWer ksDev.com



    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
  • Trevor Best

    #2
    Re: How to execute an external program and halting VBA code till external program finishes

    On 04 Sep 2003 04:42:27 GMT in comp.databases. ms-access, Greg Fierro
    <gfierro@softwe rksdev.com> wrote:
    [color=blue]
    >I would appreciate any help from anyone with the following:
    >
    >I have an external program (window32 based) that I am executing with the
    >VBA SHELL command. This program produces a text file which I have to
    >read after the external program finishes. I use the transfertext method
    >to read the file.
    >
    >The problem is that the SHELL command executes external programs in an
    >asynch way. In other words, the VBA code will continue running and NOT
    >wait for the external program to finish.
    >
    >I need to run this external program and HALT my VBA code until the
    >external program terminates. Is there any code out there or methods I
    >can use to do this??
    >
    >Any help is greatly appreciated![/color]

    The code below will do it.

    Disclaimer: I didn't write this code, I don't know where it came from
    as the original author didn't comment it and credit him/herself and
    I'm not taking credit for it.

    --- code start ---
    Option Compare Database
    Option Explicit

    Private Const STARTF_USESHOWW INDOW& = &H1
    Private Const NORMAL_PRIORITY _CLASS = &H20
    Private Const INFINITE = -1&

    Private Type STARTUPINFO
    cb As Long
    lpReserved As String
    lpDesktop As String
    lpTitle As String
    dwX As Long
    dwY As Long
    dwXSize As Long
    dwYSize As Long
    dwXCountChars As Long
    dwYCountChars As Long
    dwFillAttribute As Long
    dwFlags As Long
    wShowWindow As Integer
    cbReserved2 As Integer
    lpReserved2 As Long
    hStdInput As Long
    hStdOutput As Long
    hStdError As Long
    End Type

    Private Type PROCESS_INFORMA TION
    hProcess As Long
    hThread As Long
    dwProcessID As Long
    dwThreadId As Long
    End Type
    '
    Private Declare Function WaitForSingleOb ject Lib "kernel32" (ByVal _
    hHandle As Long, ByVal dwMilliseconds As Long) As Long

    Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
    lpApplicationNa me As Long, ByVal lpCommandLine As String, ByVal _
    lpProcessAttrib utes As Long, ByVal lpThreadAttribu tes As Long, _
    ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
    ByVal lpEnvironment As Long, ByVal lpCurrentDirect ory As Long, _
    lpStartupInfo As STARTUPINFO, lpProcessInform ation As _
    PROCESS_INFORMA TION) As Long

    Private Declare Function CloseHandle Lib "kernel32" (ByVal _
    hObject As Long) As Long

    Public Sub ShellWait(Pathn ame As String, Optional WindowStyle As Long)
    Dim proc As PROCESS_INFORMA TION
    Dim start As STARTUPINFO
    Dim RET As Long
    ' Initialize the STARTUPINFO structure:
    With start
    .cb = Len(start)
    If Not IsMissing(Windo wStyle) Then
    .dwFlags = STARTF_USESHOWW INDOW
    .wShowWindow = WindowStyle
    End If
    End With
    ' Start the shelled application:
    RET& = CreateProcessA( 0&, Pathname, 0&, 0&, 1&, _
    NORMAL_PRIORITY _CLASS, 0&, 0&, start, proc)
    ' Wait for the shelled application to finish:
    RET& = WaitForSingleOb ject(proc.hProc ess, INFINITE)
    RET& = CloseHandle(pro c.hProcess)
    End Sub
    --- code ends ---
    --
    A)bort, R)etry, I)nfluence with large hammer.

    (replace sithlord with trevor for email)

    Comment

    • Terry Kreft

      #3
      Re: How to execute an external program and halting VBA code till external program finishes



      Terry

      "Trevor Best" <bouncer@localh ost> wrote in message
      news:gfpdlv4up3 qa1g79shd9i1pcg oilo8lvbv@4ax.c om...[color=blue]
      > On 04 Sep 2003 04:42:27 GMT in comp.databases. ms-access, Greg Fierro
      > <gfierro@softwe rksdev.com> wrote:
      >[color=green]
      > >I would appreciate any help from anyone with the following:
      > >
      > >I have an external program (window32 based) that I am executing with the
      > >VBA SHELL command. This program produces a text file which I have to
      > >read after the external program finishes. I use the transfertext method
      > >to read the file.
      > >
      > >The problem is that the SHELL command executes external programs in an
      > >asynch way. In other words, the VBA code will continue running and NOT
      > >wait for the external program to finish.
      > >
      > >I need to run this external program and HALT my VBA code until the
      > >external program terminates. Is there any code out there or methods I
      > >can use to do this??
      > >
      > >Any help is greatly appreciated![/color]
      >
      > The code below will do it.
      >
      > Disclaimer: I didn't write this code, I don't know where it came from
      > as the original author didn't comment it and credit him/herself and
      > I'm not taking credit for it.
      >
      > --- code start ---
      > Option Compare Database
      > Option Explicit
      >
      > Private Const STARTF_USESHOWW INDOW& = &H1
      > Private Const NORMAL_PRIORITY _CLASS = &H20
      > Private Const INFINITE = -1&
      >
      > Private Type STARTUPINFO
      > cb As Long
      > lpReserved As String
      > lpDesktop As String
      > lpTitle As String
      > dwX As Long
      > dwY As Long
      > dwXSize As Long
      > dwYSize As Long
      > dwXCountChars As Long
      > dwYCountChars As Long
      > dwFillAttribute As Long
      > dwFlags As Long
      > wShowWindow As Integer
      > cbReserved2 As Integer
      > lpReserved2 As Long
      > hStdInput As Long
      > hStdOutput As Long
      > hStdError As Long
      > End Type
      >
      > Private Type PROCESS_INFORMA TION
      > hProcess As Long
      > hThread As Long
      > dwProcessID As Long
      > dwThreadId As Long
      > End Type
      > '
      > Private Declare Function WaitForSingleOb ject Lib "kernel32" (ByVal _
      > hHandle As Long, ByVal dwMilliseconds As Long) As Long
      >
      > Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
      > lpApplicationNa me As Long, ByVal lpCommandLine As String, ByVal _
      > lpProcessAttrib utes As Long, ByVal lpThreadAttribu tes As Long, _
      > ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
      > ByVal lpEnvironment As Long, ByVal lpCurrentDirect ory As Long, _
      > lpStartupInfo As STARTUPINFO, lpProcessInform ation As _
      > PROCESS_INFORMA TION) As Long
      >
      > Private Declare Function CloseHandle Lib "kernel32" (ByVal _
      > hObject As Long) As Long
      >
      > Public Sub ShellWait(Pathn ame As String, Optional WindowStyle As Long)
      > Dim proc As PROCESS_INFORMA TION
      > Dim start As STARTUPINFO
      > Dim RET As Long
      > ' Initialize the STARTUPINFO structure:
      > With start
      > .cb = Len(start)
      > If Not IsMissing(Windo wStyle) Then
      > .dwFlags = STARTF_USESHOWW INDOW
      > .wShowWindow = WindowStyle
      > End If
      > End With
      > ' Start the shelled application:
      > RET& = CreateProcessA( 0&, Pathname, 0&, 0&, 1&, _
      > NORMAL_PRIORITY _CLASS, 0&, 0&, start, proc)
      > ' Wait for the shelled application to finish:
      > RET& = WaitForSingleOb ject(proc.hProc ess, INFINITE)
      > RET& = CloseHandle(pro c.hProcess)
      > End Sub
      > --- code ends ---
      > --
      > A)bort, R)etry, I)nfluence with large hammer.
      >
      > (replace sithlord with trevor for email)[/color]


      Comment

      • Trevor Best

        #4
        Re: How to execute an external program and halting VBA code till external program finishes

        On Thu, 4 Sep 2003 11:19:33 +0100 in comp.databases. ms-access, "Terry
        Kreft" <terry.kreft@mp s.co.uk> wrote:
        [color=blue]
        >http://www.mvps.org/access/api/api0004.htm
        >
        >Terry[/color]

        Thanks Terry, I'm going to issue a memo to my programmers that any
        code they cut and paste from the web should retain the original
        comments including the author's name. You'll be glad to know I use
        your "select folder" code a lot and that still has your name on it.
        The reason I made that disclaimer in my followup was because I
        suspected it came from *somewhere* and last time I posted code like
        that I was accused of taking credit for someone else's work even
        though anyone who knows my programming style could tell I didn't write
        it.

        --
        A)bort, R)etry, I)nfluence with large hammer.

        (replace sithlord with trevor for email)

        Comment

        • Greg Fierro

          #5
          Re: How to execute an external program and halting VBA code till external program finishes





          *** Sent via Developersdex http://www.developersdex.com ***
          Don't just participate in USENET...get rewarded for it!

          Comment

          • Greg Fierro

            #6
            Re: How to execute an external program and halting VBA code till external program finishes

            Thanks Trevor and Terry for your answers to my problem. I have enought
            to solve my problem!


            Thanks Again!!
            Greg Fierro

            *** Sent via Developersdex http://www.developersdex.com ***
            Don't just participate in USENET...get rewarded for it!

            Comment

            • Terry Kreft

              #7
              Re: How to execute an external program and halting VBA code till external program finishes

              Trevor,
              I wasn't being picky, just thought I'd answer the question about where it
              came from.

              Personally, I think most code that people write is bound to be influenced by
              code they've seen, that's after all one of the best ways to learn, reading
              and writing code.

              I must admit though I always try to keep the original authors name in the
              code, notably I have a number of routines from Stephen Lebans which I use,
              they all keep the copyright and disclaimers through into my code ( that way
              I can always blame him if it doesn't work <g>).

              Terry


              "Trevor Best" <bouncer@localh ost> wrote in message
              news:th0flv4uj8 uasmrh7nrp5md3m rgfm9ffps@4ax.c om...[color=blue]
              > On Thu, 4 Sep 2003 11:19:33 +0100 in comp.databases. ms-access, "Terry
              > Kreft" <terry.kreft@mp s.co.uk> wrote:
              >[color=green]
              > >http://www.mvps.org/access/api/api0004.htm
              > >
              > >Terry[/color]
              >
              > Thanks Terry, I'm going to issue a memo to my programmers that any
              > code they cut and paste from the web should retain the original
              > comments including the author's name. You'll be glad to know I use
              > your "select folder" code a lot and that still has your name on it.
              > The reason I made that disclaimer in my followup was because I
              > suspected it came from *somewhere* and last time I posted code like
              > that I was accused of taking credit for someone else's work even
              > though anyone who knows my programming style could tell I didn't write
              > it.
              >
              > --
              > A)bort, R)etry, I)nfluence with large hammer.
              >
              > (replace sithlord with trevor for email)[/color]


              Comment

              • David W. Fenton

                #8
                Re: How to execute an external program and halting VBA code till external program finishes

                terry.kreft@mps .co.uk (Terry Kreft) wrote in
                <bj9l1u$44j$1@n ewsreaderg1.cor e.theplanet.net >:
                [color=blue]
                >I must admit though I always try to keep the original authors name
                >in the code, notably I have a number of routines from Stephen
                >Lebans which I use, they all keep the copyright and disclaimers
                >through into my code ( that way I can always blame him if it
                >doesn't work <g>).[/color]

                I also include the URL where I got it, where applicable.

                --
                David W. Fenton http://www.bway.net/~dfenton
                dfenton at bway dot net http://www.bway.net/~dfassoc

                Comment

                • Trevor Best

                  #9
                  Re: How to execute an external program and halting VBA code till external program finishes

                  On Fri, 5 Sep 2003 10:32:53 +0100 in comp.databases. ms-access, "Terry
                  Kreft" <terry.kreft@mp s.co.uk> wrote:
                  [color=blue]
                  >Trevor,
                  >I wasn't being picky[/color]

                  I didn't take it as such :-)

                  --
                  A)bort, R)etry, I)nfluence with large hammer.

                  (replace sithlord with trevor for email)

                  Comment

                  Working...