简体   繁体   中英

Clear Javascript Message Box from VBA

In controlling a website (with VBA), and attempting to delete all of the selected items in a table, I get a Javascript pop-up which asks me if I really want to delete all of them (OK or Cancel). When this pops up, my vba code stops, and will not do anything else, until a human clicks the button.

I have researched this quite a bit, and it seems that changing the onClick event for the HTML button would in theory work, but I have been unable to get it to work as of yet. Below is the HTML code for the button, the subsequent Javascript code, and then my VBA code.

HTML Button

 <p>Delete:&nbsp;<input type="button" name="button_Delete" value="Delete" onclick="doDelete('c');">&nbsp;<span class="note">Deletes all selected charts.</span></p> <p>Move to:&nbsp;<select id="select_Lists" name="select_Lists"> 

Javascript after HTML Button

 <a href="javascript:doDeleteList('169', 'C');">Delete List</a> | 

VBA Code to Delete all selected from HTML Table

Dim ieDelete as HTMLInputButtonElement
SetieDelete = IE.Document.getElementsByName("button_Delte")(0)
Sleep(500)
ieDelete.removeAttribute("onClick")
Sleep(500)
ieDelete.setAttribute "onClick", "return true;"
Sleep(500)
ieDelete.Click

When running the VBA as is, it continues to bring up the Javascript message box. I have attempted adding ieDelete.setAttribute "onClick", "" , but that ends up deleting the entire event, I was not able to do anything to it after that.

And... For the record, I also saw a post describing another option, but I couldn't figure out how to use it in my scenario. Respond to Website Message Box by Automating InternetExplorer with VBA

Thanks in advance for the assistance.

You could possibly hack the confirmation dialog itself: Overwrite the builtin javascript confirm function using the following VBA code before calling ieDelete.Click :

IE.Document.parentWindow.execScript "confirm = function() { return true; }"

(Insert it before your call to ieDelete.Click )

This will cause the browser to automatically 'click' OK whenever a confirmation dialog is requested.

PS: This answer assumes that the following:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM