AK0101 AK0101 - 1 year ago 138
Javascript Question

javascript code in vba

I want to write some javascript code I have in vba for word

var images = document.getElementsByTagName('img');
var l = images.length;
for (var i = 0; i < l; i++) {

I tested this in the console in chrome and it deletes all images in html document at once

I want to open the html document in word 2010 and remove the images

I have been able to delete images using this

Public Sub PicturesDeleteAll()

Dim objPic As Shape

For Each objPic In ActiveDocument.Shapes
Next objPic

End Sub

but it does not delete them all at once and I have to run it several times to work so what is the vba equivant of the javascript code I have?

Answer Source

I can't see a scenario in which this would happen however, when deleting it can be better to do it in reverse to avoid a changing array size giving misleading results that could be symptomatic of what you are seeing.

Try the below: -

Public Sub PicturesDeleteAll()
Dim LngCounter As Long

For Each LngCounter = ActiveDocument.Shapes.Count to 0 Step -1
End Sub


Public Sub PicturesDeleteAll()
Do Until ActiveDocument.Shapes.Count = 0 
End Sub
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download