How can i delete an HTML element from a vba macro ?

John_999

New Member
Joined
May 1, 2020
Messages
1
Office Version
  1. 2016
Platform
  1. Windows
Hello Friends,

I am working on a code that uses VBA-Excel(Macros) to navigate to a website and automate a daily task i do . Having said that , am stuck in between of trying to delete an html element . The following js code works perfect i.e. :-

var elmnt = document.getElementById("example");
elmnt.remove();

How could i make the above 2 lines of js code in vba excel ?

I tried the following but with no result . Please help , thanks in advance .

Sub DailyTask()

Dim ie As InternetExplorer
Set ie = New InternetExplorerMedium
Dim url As String
Dim elementToBeDeleted As String
url = "examplewebsite dot com"

ie.Visible = True
ie.Navigate url

While ie.Busy
DoEvents
Wend

ie.Document.getElementsByTagName("select")("toDOList").Value = "TEST1"
ie.Document.getElementsByTagName("select")("scheduleDate").Value = "TEST2"
ie.Document.getElementsByTagName("select")("descriptionTask").Value = "TEST3"

'Many more of data entry until submitting the form works fine .

elementToBeDeleted = ie.Document.getElementById("example")
Call DeleteTagById(elementToBeDeleted)

Set ie = Nothing
End Sub

Function DeleteTagById(elementToBeDeleted As String) As String

On Local Error GoTo MyError

Dim HTMLDoc As HTMLDocument
Dim Node As IHTMLDOMNode

If elementToBeDeleted = "" Then GoTo MyExit

Set HTMLDoc = New HTMLDocument
Set Node = HTMLDoc.getElementById("example") 'Node always equals to nothing , why ? hence i get stuck here and cant move further.

'If Node Is Nothing Then GoTo MyExit
Node.ParentNode.RemoveChild Node 'this line gives me run time error 91 object variable or with block variable not set .

MyExit:
Set Node = Nothing
Set HTMLDoc = Nothing
Exit Function
MyError:
'Handle Error
Resume MyExit

End Function
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hi

So I played with your code, and this works:

VBA Code:
Sub DailyTask()

Dim ie As InternetExplorer
Set ie = New InternetExplorerMedium
Dim url As String
Dim myElement As Object
Dim myElementParent As Object

url = "http://www.example.com"
ie.Visible = True
ie.navigate url

While ie.Busy
DoEvents
Wend

Set myElement = ie.document.getElementsByTagName("h1")(0)
'Debug.Print myElement.outerHTML

Set myElementParent = myElement.ParentNode
'Debug.Print myElementParent.outerHTML
MsgBox "H1 tag will now be deleted"
myElementParent.RemoveChild myElement

Set ie = Nothing
End Sub

As to the rest of your code, I'm not sure that DeleteTagById function was ever going to be of any help, primarily because you are instantiating a new HTMLDocument, but not providing it with the relevant HTML code against which to retrieve any elements. In any event, it's a redundant process because you've already loaded the url in the Internet Explorer browser, and even if you did put the HTML code in the HTMLDocument, and assuming that you did delete the element, what would you do then? Did you just want the code with the element removed from it? My point is that you won't be able to see anything, because the HTMLDocument doesn't render. For that reason, I kept it all in the IE object, so you could see the deletion in real time.

It's a great question - I learnt a few things. I don't usually manipulate the code of rendered sites and I wasn't aware of the RemoveChild method. Interesting that you can't delete the element without reference to its parent node, like Javascript. Thanks.
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,316
Members
452,634
Latest member
cpostell

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top