vba - Transfer data from excel to pre-existing Word table? -
hello trying use code export data excel pre-existing word table.however, once code reaches each wdcell in wddoc.tables loop run-time error '91' object variable or with block variable not set appears. there better way can code transfer data 7 columns?
sub exportdatawordtable() const stworddocument string = "c:\users\jfournier\desktop\vba macro files\testquote.docm" dim wdapp word.application dim wddoc word.document dim wdcell word.cell dim long dim j long dim wbbook workbook dim wssheet worksheet dim vadata variant set wbbook = thisworkbook set wssheet = wbbook.worksheets("sheet2") redim vadata(1 10, 1 5) wssheet vadata = .range("b3:h20") end 'here instantiate new object. set wdapp = new word.application 'here target document resides in same folder workbook. set wddoc = wdapp.documents.open(wbbook.path & "\" & stworddocument) 'import data first table , in first column of table in microsoft word. j = 1 5 = 0 each wdcell in wddoc.tables(2).columns(j).cells = + 1 wdcell.range.text = vadata(i, j) next wdcell next j 'save , close document. wddoc .save .close end 'close hidden instance of microsoft word. wdapp.quit 'release external variables memory set wddoc = nothing set wdapp = nothing msgbox "the data has been transferred test.doc", vbinformation end sub
i have observed following points in code correction.
you setting full path of your file testquote.docm
const stworddocument string = "c:\users\jfournier\desktop\vba macro files\testquote.docm" later on setting path target document resides in same folder workbook.
set wddoc = wdapp.documents.open(wbbook.path & "\" & stworddocument) this give conflict in path string. should mention only.
const stworddocument string = "testquote.docm" you want code transfer data 7 columns. have taken range b3:h20 have set variant vadata 5 columns only.
redim vadata(1 10, 1 5) also looping 5 columns only.
'import data first table , in first column of table in microsoft word. j = 1 5 thse 2 lines need changed :-
redim vadata(1 10, 1 7) j = 1 7 other points ensure are:-
- you have set reference microsoft word object library corresponding version of excel. have excel 2016 have set reference microsoft word 16.0 object library.
- your word file should pre exist in same directory have stored thisworkbook macro file. have worked macro in excel macro file , not in word vba editor.
- your testquote word document should have correct table structure corresponding range b3:h20 18 rows , 7 columns. should in closed condition when run vba program excel file.
finally corrected code follows.
sub exportdatawordtable() const stworddocument string = "testquote.docm" dim wdapp word.application dim wddoc word.document dim wdcell word.cell dim long dim j long dim wbbook workbook dim wssheet worksheet dim vadata variant set wbbook = thisworkbook set wssheet = wbbook.worksheets("sheet2") redim vadata(1 10, 1 7) wssheet vadata = .range("b3:h20") end 'here instantiate new object. set wdapp = new word.application 'here target document resides in same folder workbook. set wddoc = wdapp.documents.open(wbbook.path & "\" & stworddocument) 'import data first table , in first column of table in microsoft word. j = 1 7 = 0 each wdcell in wddoc.tables(1).columns(j).cells = + 1 wdcell.range.text = vadata(i, j) next wdcell next j 'save , close document. wddoc .save .close end 'close hidden instance of microsoft word. wdapp.quit 'release external variables memory set wddoc = nothing set wdapp = nothing msgbox "the data has been transferred testquote.docm", vbinformation end sub i have tested program on sample data , appending snapshot of excel sample data , results obtained on word document.


Comments
Post a Comment