Scraping Word with Excel VBA -
i have word document full of newspaper articles. each newspaper article preceded article title , string "length:", followed number of words in article (i.e. "length: 1500 words"). need excel macro comb word document , extract length value each article - placing these values in excel column.
through googling, found this: extract data word document excel spreadsheet
this almost need, returns first article length value found search. how modify code find every article length value, return these values excel column , terminate?
the code link not particularly robust. i've extracted assignment cell in excel (exr(1, 1) = wdr ' place @ excel cursor
) , built more robust word code around it.
the code uses word range
object instead of selection
. more efficient, more predictable , screen won't jump around. find uses wildcard search specific text, plus digits between "length " , " words". since successful find includes found range, that's necessary assign range's text cell in excel.
the find plus assignment built loop, runs long find.execute successful. cell assignment in excel counter incremented in each loop don't need hard-code target cell indices.
dim strfind string dim rngfind word.range 'or object if don't set reference word object library dim bfound boolean dim icellcounter long strfind = "length: [0-9]{1;} words" bfound = false icellcounter = 1 set rngfind = wapp.activedocument.content rngfind.find .clearallfuzzyoptions .clearformatting .clearhithighlight .format = false .matchwildcards = true .text = strfind .wrap = wdfindstop '0 if don't use reference word object library bfound = .execute if bfound exr(1, icellcounter) = rngfind.text icellcounter = icellcounter + 1 end if loop while bfound end
Comments
Post a Comment