Excel VBA Not Saving in Current Directory -
here code below excel vba batch save excel files in directory pdf. uses msofiledialogfolderpicker user's input.
everything works except doesn't save in current directory original files, saves in folder above.
please let me know need add or change saves in same folder.
thanks.
sub batchprocessing_exceltopdf() application.filedialog(msofiledialogfolderpicker) .title = "select folder location" .buttonname = "select" .show .allowmultiselect = false cmdselectinput = application.filedialog(msofiledialogfolderpicker).selecteditems (1) & "\" end mypath = cmdselectinput mytemplate = "*.xls*" ' set template. myname = dir(mypath & mytemplate) 'retrieve first file while myname <> "" workbooks.open mypath & myname pdfsaveas workbooks(myname).close (true) 'close myname = dir 'get next file loop msgbox "finished excel batch processing" end sub sub pdfsaveas() ' ' save active excel sheet pdf ' ' activesheet.exportasfixedformat type:=xltypepdf, filename:= _ mypath & myname, quality:= _ xlqualitystandard, includedocproperties:=true, ignoreprintareas:=false, _ openafterpublish:=false end sub
in sub pdfsaveas "mypath" not exist variable.
as result passed in empty string. therefore default saves file existing name .pdf in active directory.
you need pass in variables mypath , myname sub, or else declare them module level variables.
e.g.: option 1: sub pdfsaveas(mypath string, myname string)
called pdfsaveas mypath, myname
or option2: declare mypath , myname @ top of module private mypath string
etc. in scope pdfsaveas.
always use option explicit
@ top of module. ensure issues on phantom variables not arise.
Comments
Post a Comment