Results 1 to 5 of 5

Thread: [RESOLVED] HELP! Save as: (do not overwrite) but new file name

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    2

    Resolved [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!

  2. #2
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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.

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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:
    1. fname = "Y:\...\...\...\" & strNameOC & ".docx"
    2. If Not Dir(fname) = "" Then 'filename exists
    3.     Do
    4.         i = i + 1
    5.         fname = "Y:\...\...\...\" & strNameOC & i & ".docx"
    6.     Loop Until Dir(fname) = "" ' loop till filename not found
    7. End If
    8. 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

  4. #4
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    2

    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
  •  



Click Here to Expand Forum to Full Width