pdiddo
Goto Top

Vbs in exist - wenn ein Fenster existiert dann

Hi Leute...

Ich versuch auf vbs zu realisieren, sobald ein Fenster (Programm was zuvor gestartet wird) existiert, dass eine Anweisung folgt.
Wie bekomme ich das hin ?

Ifexist „Notepad“
then sendkey „abc“
Else loop von vorne (10 mal)

Geht das ?

Es kommt mir vor allem auf das if exist an.


Liebe Grüße

Content-Key: 369818

Url: https://administrator.de/contentid/369818

Printed on: April 19, 2024 at 05:04 o'clock

Member: emeriks
emeriks Mar 31, 2018 updated at 13:14:45 (UTC)
Goto Top
Hi,
so in etwa
dim WindowName : WindowName = "datei.txt - Editor"    

Dim WshShell : Set WshShell = CreateObject("Wscript.Shell")  

dim i : i = 0
do while i < 10
  If WshShell.AppActivate(WindowName) Then
     WshShell.Sendkey(„abc“)
     exit do
  end if
  i = i + 1
  Wscript.Sleep 1000
loop

E.
Member: mayho33
mayho33 Apr 26, 2019 at 14:26:32 (UTC)
Goto Top
Damit kannst du schon mal auf einen Process warten:

WaitForProcess "notepad.exe", "100"  

Function WaitForProcess(ByVal Process, secondsToWait)
	Dim startTime : startTime = Timer()
	Dim endTime : endTime = startTime + CInt(secondsToWait)
	Dim objItem, PidStatus : PidStatus = 1
	
	On Error Resume Next
	
	Do While PidStatus <> 0
		Dim caption : caption = ""  
		startTime = Timer()
		Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")  
		Set colItems = objWMIService.ExecQuery("Select * from Win32_Process where name ='" & Process & "'",,48)  
		For Each objItem in colItems
			If UCase(Process) = UCase(objItem.Name) Then
	    		Exit Function
	    	End If
		Next
	
		varNow = Now
		While DateDiff("s", varNow, Now) < 1  
		Wend
		WScript.Echo "running..."  
		If startTime > endTime Then
			varNow = Now
			While DateDiff("s", varNow, Now) < 3  
			Wend
			Exit Function
		End if
	Loop
End Function

Für SendKeys müsstest du vorher den Focus auf diese App setzen. Das funktioniert aber eher selten. Außerdem: Sobald eine andere App aktiviert wird (durch Klick darauf) hat diese den Focus. Sehr buggy.