-
Jun 25th, 2017, 09:18 AM
#1
Thread Starter
New Member
[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.
-
Jun 25th, 2017, 11:10 AM
#2
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
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Jun 25th, 2017, 11:35 AM
#3
Thread Starter
New Member
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.
-
Jun 25th, 2017, 11:39 AM
#4
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Jun 25th, 2017, 11:48 AM
#5
Thread Starter
New Member
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.
-
Jun 25th, 2017, 04:17 PM
#6
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
-
Jun 25th, 2017, 04:41 PM
#7
Thread Starter
New Member
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)
-
Jun 26th, 2017, 04:15 AM
#8
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
-
Jun 26th, 2017, 01:19 PM
#9
Thread Starter
New Member
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.
-
Jun 26th, 2017, 04:09 PM
#10
Re: Shell function syntax to wait until process is complete
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
-
Jun 26th, 2017, 04:27 PM
#11
Thread Starter
New Member
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.
-
Jun 27th, 2017, 02:54 AM
#12
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
-
Jul 8th, 2017, 02:25 PM
#13
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|