excel - Paste multiple sheets into a single Word document -
i'm trying copy , paste each worksheet in workbook onto new sheet in single word document. unfortunately copying contents of first worksheet, though seem looping through worksheets. thought inserting page break work isn't. won't let me format in word. want contents of a1 have header style.
this code:
sub exceltoword() dim ws worksheet dim wkbk1 workbook set wkbk1 = activeworkbook application.screenupdating = false application.displayalerts = false application.enableevents = false each ws in wkbk1.worksheets wkbk1.activesheet.range("a1:a2").copy dim wdapp object dim wddoc object dim header range 'file name & folder path dim strdocname string on error resume next 'error number 429 set wdapp = getobject(, "word.application") if err.number = 429 err.clear 'create new instance of word application set wdapp = createobject("word.application") end if wdapp.visible = true 'define paths file strdocname = "p:\importeddescriptions.doc" if dir(strdocname) = "" msgbox "the file" & strdocname & vbcrlf & "was not found " & vbcrlf & "p:\importeddescriptions.doc", vbexclamation, "the document not exist " exit sub end if wdapp.activate set wddoc = wdapp.documents(strdocname) if wddoc nothing set wddoc = wdapp.documents.open(strdocname) set header = range("a1") 'must activate able paste wddoc.activate wddoc.range.paste selection.wholestory header.style = activedocument.styles("heading 2") selection.insertbreak type:=wdpagebreak next ws wddoc.save 'wdapp.quit set wddoc = nothing set wdapp = nothing application.screenupdating = true application.displayalerts = true application.enableevents = true end sub
you copying active worksheet, happens first sheet in case. instead of:
for each ws in activeworkbook.worksheets activeworkbook.activesheet.range("a1:a2").copy
use:
for each ws in activeworkbook.worksheets ws.range("a1:a2").copy
this copy each range in turn.
Comments
Post a Comment