[RESOLVED] HELP! Save as: (do not overwrite) but new file name
I have a piece of long winded code which saves word files to a folder
Dim strNameOC As String
strNameOC = (Range("E" & i).Value & Range("F" & i).Value)
appWD.ActiveDocument.SaveAs Filename:="Y:\...\...\...\" & strNameOC & ".docx"
if the file name already exists then an error occurs. (i know how to overwrite/ignor that but it overwrites files i want)
What i want to do is save the file as: strNameOC & version whatever1,2 etc.
There is a whole bunch of code before and after this so i really need to know how to save to a new filename without overwriting or breaking the code.
I would be real gratefull for the help!
Re: HELP! Save as: (do not overwrite) but new file name
Just check if planned filename already exists, if it does then try variant with number (and increment if it still exists)... you can use Dir(), FileSystem object. Do a search, their use has been discussed numerous times already.
Re: HELP! Save as: (do not overwrite) but new file name
you would need to test for the filename in the directory, using
if DIR("Y:\...\...\...\" & strNameOC & ".docx") = "" then ' filename does not existyou may need to use a loop with increment to see what first number does not yet exist like
vb Code:
fname = "Y:\...\...\...\" & strNameOC & ".docx"
If Not Dir(fname) = "" Then 'filename exists
Do
i = i + 1
fname = "Y:\...\...\...\" & strNameOC & i & ".docx"
Loop Until Dir(fname) = "" ' loop till filename not found
End If
appWD.ActiveDocument.SaveAs Filename:=fname
didn't test this, but it looks right
Re: HELP! Save as: (do not overwrite) but new file name
Check if you can Dir the folder first... eliminate possibility of folder access error.
Re: HELP! Save as: (do not overwrite) but new file name
That was really helpful cheers!