+ Reply to Thread
Results 1 to 3 of 3

How can i delete HTML element in vba excel ?

  1. #1
    Registered User
    Join Date
    05-01-2020
    Location
    Bahrain
    MS-Off Ver
    Microsoft Office Professional Plus 2016 Version 16.0.4266.1001
    Posts
    2

    Question How can i delete HTML element in vba excel ?

    Hi ,

    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

  2. #2
    Valued Forum Contributor ranman256's Avatar
    Join Date
    07-29-2012
    Location
    Kentucky
    MS-Off Ver
    Excel 2003
    Posts
    1,176

    Re: How can i delete HTML element in vba excel ?

    ActiveCell.Hyperlinks.Delete

    or
    range("A4").Hyperlinks.Delete

  3. #3
    Registered User
    Join Date
    05-01-2020
    Location
    Bahrain
    MS-Off Ver
    Microsoft Office Professional Plus 2016 Version 16.0.4266.1001
    Posts
    2

    Re: How can i delete HTML element in vba excel ?

    Thanks for the reply . But note that i am not trying to delete from a cell or a range . What i am trying to achieve is that delete an html tag for instance an html table element or html p element .

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to select webpage HTML Input element list item
    By wotadude in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-09-2017, 02:43 PM
  2. Import data from specific HTML element based URLs in a column
    By whatever61 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-04-2016, 08:04 AM
  3. Cannot find username element Id in fedex.com/us html code.
    By Locrian in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-27-2015, 02:53 PM
  4. NEED HELP: How to code correctly an HTML element to excel vba
    By blackorchids2002 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-12-2015, 04:15 PM
  5. [SOLVED] Parsing HTML element into excel
    By chococ in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-11-2014, 05:20 PM
  6. Inspect document and delete hidden rows with VBA
    By sabutler4 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-02-2013, 04:51 PM
  7. Delete String Element
    By John in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-08-2005, 05:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1