Results 1 to 13 of 13

Thread: [RESOLVED] Shell function syntax to wait until process is complete

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2017
    Posts
    13

    Resolved [RESOLVED] Shell function syntax to wait until process is complete

    Hello everybody.

    What i want is a to open IE and wait until the process is complete to continue the program.

    Can't understand why, if the shell function syntax in vba is:

    Public Function Shell( _
    ByVal PathName As String, _
    Optional ByVal Style As AppWinStyle = AppWinStyle.MinimizedFocus, _
    Optional ByVal Wait As Boolean = False, _
    Optional ByVal Timeout As Integer = -1 _
    ) As Integer

    So the 3rd parameter =true will allow to wait until the process is complete.

    Why when i try this:
    ie = Shell("C:\Program Files\Internet Explorer\iexplore.exe -nosessionmerging -noframemerging", vbNormalFocus, True, 200000)

    I get a message saying that the number of arguments is wrong and only with 2 arguments i can run it:

    ie = Shell("C:\Program Files\Internet Explorer\iexplore.exe -nosessionmerging -noframemerging", vbNormalFocus)

    and the process will not wait until the process is complete.

    Thanks in advance.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: Shell function syntax to wait until process is complete

    Because Shell in VBA is different than Shell in VB.NET or VB6. It only takes two arguments. Look at the intellisense

    https://msdn.microsoft.com/VBA/Langu...shell-function
    https://msdn.microsoft.com/en-us/lib...(v=vs.90).aspx


    Name:  DeleteMe1.jpg
Views: 13814
Size:  12.3 KB
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2017
    Posts
    13

    Re: Shell function syntax to wait until process is complete

    Thanks for your reply RobDog888!
    Now i see what is the problem.
    "If the Shell function successfully executes the named file, it returns the task ID of the started program. The task ID is a unique number that identifies the running program."
    So is there an easy way to wait until the process is complete?
    I don' t like to do it with:
    Application.Wait(Now + TimeValue("0:00:02")) because most of the time is faster but sometimes is not. I would like to use a best solution.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: Shell function syntax to wait until process is complete

    What is your "process"? Is it just starting up IE? Waiting for a particular web page to load completely? Or ?
    To start IE usually doesnt take but a second so I would think there is something more to it here
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2017
    Posts
    13

    Re: Shell function syntax to wait until process is complete

    That's right.
    I have to open more than 1 sessions of IE (looping the macro) but waiting for the opening of IE and the page loading to be complete because then i have to log in in a site.
    The problem is that the only way i found to open a new session of ie is the shell command:

    ie = Shell("C:\Program Files\Internet Explorer\iexplore.exe -nosessionmerging -noframemerging", vbNormalFocus)

    But if i run it and i don't wait until the process is complete the command to get the shell as an object (to be able to navigate ) will not work.

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Shell function syntax to wait until process is complete

    the command to get the shell as an object (to be able to navigate ) will not work.
    loop until it does

    with createobject, you will not have this problem
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    New Member
    Join Date
    Jun 2017
    Posts
    13

    Re: Shell function syntax to wait until process is complete

    I was thinking about it but how i know that the get object action fails?
    To get the object after the shell now i use
    Set sh = CreateObject("shell.application")
    Set ie = sh.Windows(sh.Windows.Count - 1)

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Shell function syntax to wait until process is complete

    Code:
    set ie = createobject("internetexplorer.application")
    then you do not even need the shell application
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9

    Thread Starter
    New Member
    Join Date
    Jun 2017
    Posts
    13

    Re: Shell function syntax to wait until process is complete

    I need the shell because i have to open a new session of ie and the only way i found that works is the shell funcion.
    I use the shell and then i get object to control it.

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Shell function syntax to wait until process is complete

    search on shell and wait
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  11. #11

    Thread Starter
    New Member
    Join Date
    Jun 2017
    Posts
    13

    Re: Shell function syntax to wait until process is complete

    I looked at the shellandwait function but what i want is a function that waits until the shell is complete and not that waits a definite time and says if it is complete or not because i can't loop the shellandwait until the process is complete (it will open a lop of IE i think). Right?
    I am thinking more something like looping until the sh.Windows(sh.Windows.Count - 1) is an internet explorer process.
    How can i write it?
    Last edited by Andreaxs; Jun 26th, 2017 at 04:31 PM.

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Shell function syntax to wait until process is complete

    shell and wait actually waits till the shelled process is closed, so that is not what you want, my error

    I am thinking more something like looping until the sh.Windows(sh.Windows.Count - 1) is an internet explorer process.
    i would try like
    Code:
    cnt = sh.windows.count
    ie = Shell("C:\Program Files\Internet Explorer\iexplore.exe -nosessionmerging -noframemerging", vbNormalFocus)
    do until sh.windows.count = cnt +1: Loop
    set ie = sh.windows(sh.windows.count -1)
    do until ie.readystate = 4: Loop
    note that this example uses the ie variable for 2 different types, you do not specifically need the return value from the shell function
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  13. #13

    Thread Starter
    New Member
    Join Date
    Jun 2017
    Posts
    13

    Re: Shell function syntax to wait until process is complete

    Thanks westconn1!

    As you suggested i solved with:

    Code:
    Set ie = Nothing
    cnt = 0
    Set sh = CreateObject("shell.application")
    cnt = sh.Windows.Count
    ie = Shell("C:\Program Files\Internet Explorer\iexplore.exe -nosessionmerging -noframemerging", vbNormalFocus)
    Do Until sh.Windows.Count = cnt + 1
    Loop
    Set ie = sh.Windows(sh.Windows.Count - 1)

Posting Permissions

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



Click Here to Expand Forum to Full Width