excel - Save generated Word file with unique name (mailmerge) -
i need macro. need save generated word file via mail merge.
sub runmerge() dim wd object dim wdocsource object dim strworkbookname string on error resume next set wd = getobject(, "word.application") if wd nothing set wd = createobject("word.application") end if on error goto 0 set wdocsource = wd.documents.open("c:\users\admin\desktop\new folder (2)\g706014 ver.7.0.docx") strworkbookname = thisworkbook.path & "\" & thisworkbook.name wdocsource.mailmerge.maindocumenttype = wdformletters wdocsource.mailmerge.opendatasource _ name:=strworkbookname, _ addtorecentfiles:=false, _ revert:=false, _ format:=wdopenformatauto, _ connection:="data source=" & strworkbookname & ";mode=read", _ sqlstatement:="select * `mailing$`" wdocsource.mailmerge .destination = wdsendtonewdocument .suppressblanklines = true .datasource .firstrecord = wddefaultfirstrecord .lastrecord = wddefaultlastrecord end .execute pause:=false end wd.visible = true wdocsource.close savechanges:=false set wdocsource = nothing set wd = nothing end sub
this macro generate file doesn't save it.
can update it?
but name of save file has value of excel file, worksheet mailing
, cell a2
destination saving is: c:\users\admin\desktop\new folder (2)\docs
added in code :
dim pathtosave string pathtosave = "c:\users\admin\desktop\new folder (2)\docs\" & sheets("mailing").range("a2").value2 & ".docx" 'pathtosave = "c:\users\admin\desktop\new folder (2)\docs\merge_mail_" & replace(replace(now(), "/", "-"), ":", ".") & ".docx" if dir(pathtosave, 0) <> vbnullstring wd.filedialog(filedialogtype:=msofiledialogsaveas).show else wd.activedocument.saveas2 pathtosave, wdformatdocumentdefault end if
here full code :
sub runmerge() dim wd object, _ wdocsource object, _ pathtosave string dim strworkbookname string on error resume next set wd = getobject(, "word.application") if wd nothing set wd = createobject("word.application") end if on error goto 0 set wdocsource = wd.documents.open("c:\users\admin\desktop\new folder (2)\g706014 ver.7.0.docx") strworkbookname = thisworkbook.path & "\" & thisworkbook.name wdocsource.mailmerge.maindocumenttype = wdformletters wdocsource.mailmerge.opendatasource _ name:=strworkbookname, _ addtorecentfiles:=false, _ revert:=false, _ format:=wdopenformatauto, _ connection:="data source=" & strworkbookname & ";mode=read", _ sqlstatement:="select * `mailing$`" wdocsource.mailmerge .destination = wdsendtonewdocument .suppressblanklines = true .datasource .firstrecord = wddefaultfirstrecord .lastrecord = wddefaultlastrecord end .execute pause:=false end pathtosave = "c:\users\admin\desktop\new folder (2)\docs\" & sheets("mailing").range("a2").value2 & ".docx" 'pathtosave = "c:\users\admin\desktop\new folder (2)\docs\merge_mail_" & replace(replace(now(), "/", "-"), ":", ".") & ".docx" if dir(pathtosave, 0) <> vbnullstring wd.filedialog(filedialogtype:=msofiledialogsaveas).show else wd.activedocument.saveas2 pathtosave, wdformatdocumentdefault end if wd.visible = true wdocsource.close savechanges:=false set wdocsource = nothing set wd = nothing end sub
Comments
Post a Comment