Results 1 to 5 of 5

Thread: Save with a different name

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2000
    Posts
    21

    Question

    I have this code :
    Newdoc.SaveAs TextBox1 & ".doc"
    It works except when the file "TextBox1.doc" already exist.

    Is there a way to say : if the file "TextBox1.doc" exist then SaveAs "TextBox12.doc". ?

  2. #2
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946
    'does file exist using fso

    Dim sFile$

    sFile = "Name And Path Of Your File"

    Dim fs As Object
    Set fs = CreateObject("Scripting.FileSystemObject")

    If fs.fileexists(sFile$) = True Then
    MsgBox "File Exists...Your code here!"
    Else
    MsgBox "Does Not Exist...Your code here!"
    End If
    Set fs = Nothing
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2000
    Posts
    21

    Question

    Thanks HeSaidJoe.
    But what if I want a code that automatically save the document with a +1 at the end.
    I mean if document1.doc exist then saveas document2.doc, if document2.doc exist then saveas document3.doc, and so on.
    Do you have any idea ?

  4. #4
    Fanatic Member r0ach's Avatar
    Join Date
    Dec 1999
    Location
    South Africa
    Posts
    722
    Here's a function (I used code from HeSaidJoe)
    Click on Project|References and add "Microsoft Scripting Runtime"
    Code:
    ' This will return True if saved, and False if not
    Public Function SaveNextInstance(ByVal f as String) as Boolean
        Dim fs As Scripting.FileSystemObject
        Dim docnum as Integer
    
        Set fs = New FileSystemObject
    
        If fs.FileExists(f) then
            On Error Goto ErrorHandler
            docnum = Int(Val(Mid(f, Len(f) - 7, 3)
            docnum = docnum + 1
            f = Mid(f, 1, Len(f) - 7) & Format(docnum, "0##") & ".doc"
        Else
            On Error Goto ErrorHandler
            f = f & "000.doc"
        End if
    
        NewDoc.SaveAs f
        SaveNextInstance = True
        Exit Function
    
    ErrorHandler:
        SaveNextInstance = False
    
    End Function
    I haven't tested it, but it should work.

    r0ach™
    Don't forget to rate the post

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2000
    Posts
    21

    Talking

    Thanks for all this code, it'll help me a lot.

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