excel - Creating active document in loop? -
i trying pull paragraph multiple documents, getting sub parse through each document looking paragraph between 2 terms. cannot "activedocument" or open document work? have been able use open function fine, object errors each time. folder directory , filename inputted user separate cells, text in cells has combined form full document address. here code
sub findtest() dim firstterm string, filename variant dim secondterm string, j integer dim myrange range dim documenttext string dim mydoc string dim file variant dim startpos long 'stores starting position of firstterm dim stoppos long 'stores starting position of secondterm based on first term's location dim nextposition long 'the next position search firstterm j = 2 nextposition = 1 until j > 600 documents.open filename:="worksheets(1).cells(5, 5) & worksheets(2).cells(j, 1)", readonly:=true ' error here every single time. 'first , second terms defined example. obviously, have more dynamic firstterm = "<firstword>" secondterm = "<secondname>" 'get document text , store in variable. set myrange = activedocument.range 'maximum limit of string 2 billion characters. 'so, document not bigger that. however, expect declining performance based on how big doucment documenttext = myrange.text 'loop documenttext till can't find more matching "terms" until nextposition = 0 startpos = instr(nextposition, documenttext, firstterm, vbtextcompare) stoppos = instr(startpos, documenttext, secondterm, vbtextcompare) debug.print mid$(documenttext, startpos + len(firstterm), stoppos - startpos - len(secondterm)) nextposition = instr(stoppos, documenttext, firstterm, vbtextcompare) loop j = j + 1 file = dir close document loop end sub
don't rely on activedocument. declare word.document object , assign document open that. work object rather activedocument - more reliable!
dim odoc word.document set odoc = documents.open(filename) dim orng word.range set orng = odoc.content 'preferred on odoc.range getting content of entire document odoc.close
note: don't know you're trying close document
, part of problem?
Comments
Post a Comment