-2

I have this javascript/jquery code that deletes sections of a html file

    var divs = document.getElementsByTagName('div');
var d = divs.length;
for (var i = 1; i < d; i++) {
    if( $('.section'+i).innerHTML.includes("textfind")==true || $('.section'+i).innerHTML.includes("textfind2")==true){
    $('.section'+i).remove();
    }
}

I would like write the same function as vba code for word

I have this code so far

Dim i as Long
    For each objSect in ActiveDocument.sections
        if objSect.Range.Text like "textfind +i" Or "textfind2+i" then objSect.range.delete
    Next objSect

how would I got about deleting those particular section where i find the strings

0101
  • 1,036
  • 2
  • 14
  • 24

2 Answers2

0

Can you please elaborate the question here?? And, you want to remove html or word elements using VBA??

EDIT :- You can create a variable and store an array of strings in it to find and replace. (note: If you want to just clear the text then, use an empty string in place of the 'replace' string.)

Refer to this link for VBA find and replace code VBA To find and replace a string in MS Word

Community
  • 1
  • 1
Rohit Nair
  • 628
  • 3
  • 7
  • 22
0

RemoveTextfinders: will remove both of there elements

<div class="section1">textfind2</div>
<div class="section2"><input name="textfind2" value="" /></div>

But neither of these elements

<div class="section1" name="textfind2"></div>
<div class="">textfind2</div>

Sub RemoveTextfinders()
    Dim d, divs
    Set divs = document.getElementsByTagName("div")
    For Each d In divs
        If InStr(d.className, "section") And InStr(d.innerHTML, "textfind2") Then d.ParentNode.RemoveChild d
    Next
End If

Update I combined the two if statements they where redundant.
InStr(d.className, "section") will evaluate to true if any of the div's class contain the "section".
This code ran fine for me. I must be mistaken about the structure of your html. Could you please post a sample of the html?

  • that did not work, also forgot to add section numbers see edited op – 0101 Jun 13 '16 at 22:44
  • its html file opened in microsoft word so it doesnt have any html tags like divs, so need to use activedocument.sections vba code – 0101 Jun 14 '16 at 04:02