vbscript - vb script single line mail merge -


i have peculiar need. need able read single line csv file, merge data mailmerge document, save document unique name, proceed next line, rinse , repeat.

i complete unaware of how use/merge single line of data extracted file data source mail merge. can give me insight odoc.mailmerge... line below should like?

i know open , closes in code below isn't efficient, through point across. can work rest out. need merge line.

datafile = "test.txt" docfile  = "mydoc.doc" listfile  = fso.opentextfile(datafile).readall listlines = split(listfile, vbcrlf) each line in listlines    wscript.echo line     'my stuff    dim objword, odoc, wdname    set objword = createobject("word.application")     wdname = "d:\my documents\docfile"     set odoc = objword.documents.open (wdname)    objword.visible = true     odoc.mailmerge.opendatasource ?line?, , ,true, , , , , , , , , "idontknowwhatgoeshere"    odoc.mailmerge.execute     odoc.printout    'redefine savename , execute save here    odoc.close()    objword.quit (0) next 

opendatasource doesn't let specify individual rows except via sql. typically use loop increment (say) irecord use

obj.mailmerge.datasource.firstrecord = irecord obj.mailmerge.datasource.lastrecord = irecord 

you either open data source every time, efficient way open it, have loop sets these 2 values , execute. either way, need realise "odoc" object reference mail merge main document, not document results merge. access that document vbscript think need objword.activedocument.

every other approach seems me more complicated.

however, if want use opendatasource line control merge, opendatasource (a) needs work disk file, not in-memory structure such listlines array , (b) need issue sql retrieve single line (which in turn requires data source has unique key field , (probably) know name , type.

if have that, opendatasource line like

odoc.mailmerge.opendatasource "the full path , name of data file",,,,,,,,,,,, _ "select * x [fieldname] = " & cstr(thefieldvalue) 

where relevant thefieldvalue appropriate record in listlines array. or, if know field contains row number, generate number , might not need array @ all.

if thefieldvalue string type rather numeric type, you'll need more like:

odoc.mailmerge.opendatasource "the full path , name of data file",,,,,,,,,,,, _ "select * x [fieldname] = '" & thefieldvalue & "'" 

"x" there satisfy word's requirement have clause (word has crude internal sql dialect). otoh, if word decides open file using ole db provider, need pay bit more attention "table name"

another way go use vbscript , filesystemobject create fresh data file (consisting of header row + 1 data row) each data row in data source. mailmerge entire file.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -