|
-
Jan 27th, 2009, 07:00 AM
#1
Thread Starter
New Member
[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!
-
Jan 27th, 2009, 07:10 AM
#2
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.
-
Jan 27th, 2009, 07:17 AM
#3
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
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jan 27th, 2009, 07:41 AM
#4
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.
-
Jan 27th, 2009, 11:54 AM
#5
Thread Starter
New Member
Re: HELP! Save as: (do not overwrite) but new file name
That was really helpful cheers!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|