excel - VBA - Runtime Error 438 -
i using vba automate mailmerge 3 cases : please see code below :
(1) need generate certificates based on each worksheet.
(2) certificate name should "last thursday" & "aaa" / "bbb" / "ccc" (based on worksheet) respectively. eg. 25062015aaa.docx (for sheet1), 25062015bbb.docx (for sheet2), , 25062015ccc.docx (for sheet3) respectively.
however currently, code either saving 1st generated mailmerge under different names.
or throws runtime error: 438 - object required error
, when code below. kindly tell me i'm going wrong?
thank help, always!
public function lastthurs(pdat date) date lastthurs = dateadd("ww", -1, pdat - (weekday(pdat, vbthursday) - 1)) end function sub generate_certificate() dim wd object dim integer dim wdoc object dim fname string dim ldate string dim strwbname string const wdformletters = 0, wdopenformatauto = 0 const wdsendtonewdocument = 0, wddefaultfirstrecord = 1, wddefaultlastrecord = -16 ldate = format(lastthurs(date), "ddmmyyyy") on error resume next set wd = getobject(, "word.application") if wd nothing set wd = createobject("word.application") end if on error goto 0 'generate report using "mailmerge" if data available sheet1 3 each sheet in activeworkbook.sheets = 1 3 if sheet.name = "sheet" & , isempty(thisworkbook.sheets("sheet" & i).range("a2").value) = false set wdoc = wd.documents.open("c:\temp" & & ".docx") strwbname = thisworkbook.path & "\" & thisworkbook.name wdoc.mailmerge.maindocumenttype = wdformletters wdoc.mailmerge.opendatasource _ name:=strwbname, _ addtorecentfiles:=false, _ revert:=false, _ format:=wdopenformatauto, _ connection:="data source=" & strwbname & ";mode=read", _ sqlstatement:="select * `sheet" & & "$`" wdoc.mailmerge .destination = wdsendtonewdocument .suppressblanklines = true .datasource .firstrecord = wddefaultfirstrecord .lastrecord = wddefaultlastrecord end .execute pause:=false end wd.visible = true wdoc.close savechanges:=false set wdoc = nothing 'saveas using thursday date & inside folder (based on work sheet) if = 1 wd.thisdocument.saveas "c:\" & ldate & "aaa" & ".docx" if = 2 wd.thisdocument.saveas "c:\" & ldate & "bbb" & ".docx" else wd.thisdocument.saveas "c:\" & ldate & "ccc" & ".docx" end if end if next next set wd = nothing end sub
here, new approach problem. modified code clear , understandable.
i tested, work well.
dim wordapplication object dim worddocument object dim lastthursday string dim isinvalid boolean dim statement, filesuffix, datasoure string dim asheet worksheet const wdformletters = 0 const wdopenformatauto = 0 const wdsendtonewdocument = 0 const wddefaultfirstrecord = 1 const wddefaultlastrecord = -16 'getting last thursday lastthursday = format(dateadd("ww", -1, date - (weekday(date, vbthursday) - 1)), "ddmmyyyy") on error resume next 'check word open or not set wordapplication = getobject(, "word.application") if wordapplication nothing 'if not open, open word application set wordapplication = createobject("word.application") end if on error goto 0 'getting datasoure datasoure = thisworkbook.path & "\" & thisworkbook.name 'looping sheet workbook each asheet in thisworkbook.sheets 'if first cell not empty if asheet.range("a2").value <> "" isinvalid = false 'check sheet sqlstatement , save file name. select case asheet.name case "sheet1" statement = "select * `sheet1$`" filesuffix = "aaa" case "sheet2" statement = "select * `sheet2$`" filesuffix = "bbb" case "sheet3" statement = "select * `sheet3$`" filesuffix = "ccc" case else isinvalid = true end select 'if sheet should save word if not isinvalid 'getting new word document set worddocument = wordapplication.documents.add worddocument.mailmerge .maindocumenttype = wdformletters .opendatasource name:=datasoure, addtorecentfiles:=false, _ revert:=false, format:=wdopenformatauto, _ connection:="data source=" & datasoure & ";mode=read", _ sqlstatement:=statement .destination = wdsendtonewdocument .suppressblanklines = true .datasource .firstrecord = wddefaultfirstrecord .lastrecord = wddefaultlastrecord end .execute pause:=false end worddocument.saveas "c:\" & lastthursday & filesuffix & ".docx" worddocument.close savechanges:=true end if end if next asheet
Comments
Post a Comment