Infinite Loop crashes VBA/Powershell and Win11 - vba

How can I create a Powershell or VBA script to run a command, wait and loop the command and wait infinitely until I close the respective window? I have tried the below in Powershell and VBA but they make the respective Powershell or VBA windows unresponsive and a bit later the whole system becomes unresponsive and I have to reboot.
Sub proc()
Do while $true
Application.Windows("workbook1.xls").Activate
Application.Wait(Now + TimeValue("0:10:00"))
Loop
End Sub
UPDATE:
I think it's obvious what I am trying to do: activate the window of a particular workbook every few seconds indefinitely.
I have tried DoEvents (both below the Application.Windows and the Application.Wait but it does not solve anything. Also I saw the below which says to avoid DoEvents:
https://www.fmsinc.com/microsoftaccess/modules/examples/AvoidDoEvents.asp
I tried Application.OnTime which made things much better but eventually got unresponsiveness too

Related

Delay in closing Powerpoint while doing close via VBA Macro

I have two macros in in a Powerpoint file in Microsoft Office Professional Plus 2016 version.
1. Does saveas work
2. Closes Powerpoint
I run these two macros one by one using PowerPoint /m command line option. It's working fine. However, few times 2 macro close call is intermittently taking more time. When I move mouse on PowerPoint window after "close" macro is called, it closes instantly. I am pretty sure first macro saved the file and finished execution successfully. The close call macro code is below. I don't want to save any work. I don't have any other Powerpoint process running. Please advice how can I speed up this? I wonder what's holding Powerpoint. Thank you.
Sub closeit()
With Application.ActivePresentation
.Saved = True
.Close
End With
Application.Quit
End Sub
The delay in macro 2's execution is most likely caused by macro 1 finishing the save. It's not possible to run two macros at once, so 2 can't run until 1 is done.
What's the logic behind two separate macros? It seems unneccessarily complicated, you could just add this code to the end of #1.

Pause Excel macro while Windows is locked

I have a macro which runs an SQL query, which triggers a long slow recalculation of the spreadsheet based on the new data. When it finishes I've used application.speech to alert me if certain conditions have been met; all this works fine. But:
As I said, the recalculation can take quite a while, so I sometimes lock Windows and go do something else while I wait. Does anyone know of a way to pause the macro so that it doesn't run the last few lines (i.e. the audio alerts) until Windows is unlocked?
Before you call Application.speech you could check if the system is locked and if it is locked you repeat the check every minute until it is unlocked.
Code if a system is locked
Sub SystemLocked()
Do While Check_If_Locked = "Locked"
Application.Wait TimeValue("00:00:01")
Loop
MsgBox "Done"
End Sub
Code for Check_If_Locked can be found here
https://www.mrexcel.com/forum/microsoft-access/646623-check-if-system-locked-unlocked-using-vba.html

Microsoft Excel 2013 ScreenUpdating fails

After running a macro for a prolonged period of time the ScreenUpdating in Excel totally stops working even when the macro finishes. I never set it to false but it behaves as if it is set to false and the only way to make Excel update the screen again is to close it out and reopen it. The VBA screen works however. I have tried to run DoEvents and ScreenUpdating=True but the Excel window does not update visually (it is not frozen). Is there anyway of clearing the ScreenUpdating buffer? I have tried manually setting it to false and true to no avail.
If you want to duplicate this error use this code
Application.OnTime (Now + TimeValue("0:00:05")), "macroName", (Now + TimeValue("0:00:10")), True
then I have the code copy and paste some values and I control when the loop stops with a countdown value in cell A1 for example.
I got this issue in my Excel 2013. For me, it was caused when I loaded a userform. It was very, very strange, however. After a lot of trial and error I found that what caused it was using a Sheets("MySheet").Activate command. I moved this command from running after I had shown and closed the form, to immeiately before I show the form (i.e., immediately when user clicks the 'show form' button.) This fixed it. Try fiddling with your sheets.activate commands.

SendKeys to click on a dialog box button in Access

I am using Access 2013. I searched a variety of online resources. I thought SendKeys was the answer.
Once per quarter, my client will receive an updated Access database. All table names should be identical each quarter. I want them to run a macro, specify the location of the new file, and then the macro updates the linked tables and executes all other queries I’ve built (I have the last part working).
The part I have not be able to get working is to check the “Always prompt for a new location box”, check the “select all” box and click OK (and then click OK and close after the client specifies the new file location). Below is the code I am using.
Function Open_LinkedTableManager()
DoCmd.RunCommand acCmdLinkedTableManager 'this step works fine
'the following lines, up until Application.Run don’t appear to be
'doing anything. The code will run, but I have to manually execute
'each of the steps I am trying to automate before it gets to the
'Application.Run step
SendKeys "%a", True ' also tried SendKeys "%(a)" and "+a", "a", etc,
'True; Alt+a checks the "Always prompt for a new location box”
SendKeys "%s", True ' also tried SendKeys "%(s)", True; Alt+s checks the "select all" 'box
SendKeys "{Enter}" ' then user specifies location of new file
SendKeys "{Enter}" ' click OK after receiving message "All selected linked tables 'were successfully refreshed"
' click Close to close linked table manager and proceed to the next step below (not 'sure how to do this)
Application.Run ("Update_all_queries") ' this is working;
End Sub
If sending to yourself then try DoEvents after each sendkey.
DoEvents Function
Yields execution so that the operating system can process other events.
Syntax
DoEvents( )
Remarks
The DoEvents function returns an Integer representing the number of open forms in stand-alone versions of Visual Basic, such as Visual Basic, Professional Edition. DoEvents returns zero in all other applications.
DoEvents passes control to the operating system. Control is returned after the operating system has finished processing the events in its queue and all keys in the SendKeys queue have been sent.
DoEvents is most useful for simple things like allowing a user to cancel a process after it has started, for example a search for a file. For long-running processes, yielding the processor is better accomplished by using a Timer or delegating the task to an ActiveX EXE component.. In the latter case, the task can continue completely independent of your application, and the operating system takes case of multitasking and time slicing.
Caution Any time you temporarily yield the processor within an event procedure, make sure the procedure is not executed again from a different part of your code before the first call returns; this could cause unpredictable results. In addition, do not use DoEvents if other applications could possibly interact with your procedure in unforeseen ways during the time you have yielded control.
I have solved your dilemma. All I needed to do was place the sendkey statements before the call to to the linked tabled manager. See Below - Worked Great For Me! I was also able to add all of the commands in your order and they worked great. Good luck, hope this helped. Let me know. Adam
PS: If you have many tables to change the path on, this will be painful for the user for every table you are forcing them to have to set the path for.
SendKeys "%s", 1
DoCmd.RunCommand acCmdLinkedTableManager

How to break and restart code execution automatically in VBA? Error-Handling

I wrote a code to scrape web information from Google Patents in VBA which works pretty well but unfortunately it is pretty susceptible to two errors.
The two most common are
runtime error
Windows OLE is waiting for ...
All that needs to happen when this occurs is just press ok, ctrl break, F5 and execute again. Also for reasons unknown to me the code seems to slow down after x minutes and then breaking and restarting it accelerates the speed.
So as a patch-all solution I'd like to do the following. After x minutes (or even better after let's say 40 iterations), automatically break the execution and start again.
A second error-handling way which would be great is do something like
If Range("A1".End(xlDown)) at time t = Range("A1".End(xlDown)) at time t + 600 Then restart code execution Else continue code execution
I'm basically hoping to automate my error handling so that I can run the code overnight and sleep like a baby :)
I do not think there is a code statement in Excel VBA that allows you to programmatically break code and then continue. However, you can simulate pressing of the Break button using VBA, but I don't know how you can then ask the code to press the Continue button since the code will have paused already...
Application.VBE.CommandBars.FindControl(ID:=189).Execute 'press Break button
DoEvents
The below should press the Continue button, but I couldn't get it to work - perhaps someone knows how?
Application.VBE.CommandBars.FindControl(ID:=186)
Alternatively, you may try letting your routine go to sleep, to wait for IE to respond, in which case you'll need to expose some windows APIs as follows.
In module code, at the very top, you need to put the Declare statement.
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
And then you can use Sleep() like so anywhere:
Sub Test()
Debug.Print "I will now sleep for 1000 milliseconds...."
Sleep(1000)
Debug.Print "I am now awake."
End Sub