+ Reply to Thread
Results 1 to 4 of 4

searching folder for existing file

Hybrid View

  1. #1
    Registered User
    Join Date
    11-26-2008
    Location
    New England
    MS-Off Ver
    2007
    Posts
    35

    searching folder for existing file

    Here's another one:
    I need to check if a file already exists in a folder, and prompt the user to choose if they want to overwrite it.

    I've found some posts that dal with getting filenames, but haven't found one that addresses this.

    Thanks again!
    Ted

    If <<<folder contains file>>> Then
        Dim DuplicateMsg, ResponseDuplicate
        DuplicateMsg = "This file already exists." & Chr(13) & Chr(13) _
            & "Do you want to overwrite it?"
        ResponseDuplicate = MsgBox(DuplicateMsg, vbYesNo + vbInformation)
    If ResponseDuplicate = vbYes Then
        ActiveWorkbook.Save
            
    End If
    Else
    ActiveWorkbook.SaveAs filePath & Application.PathSeparator & namePN & " PrtRev_ " _
                             & nameRev & " SO_" & nameSO & " ILRev2.xls"
    End If

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    If Len(Dir(somePathAndFile)) > 0 Then
        ' do this
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    try this code
    The ActiveWorkbook.SaveAs command id from your code the rest is from me so you will probally need to tinker with the variable names, file paths

    The msgbox is your wording with buttons set by me - you may want to change it to suit

    Dim sPath As String
    Dim sFName As String
    Dim sResp As String
    
    sPath = "c:\temp\"
    sFName = "Meter Sheet - Email.xls"
    
    If Dir(sPath & sFName, vbNormal) = "" Then
      ' File does not exist - add normal save commands here
    Else
       If MsgBox("This file already exists." & Chr(13) & Chr(13) _
          & "Do you want to overwrite it?", _
          vbYesNo + vbDefaultButton2 + vbQuestion) = vbYes Then
             ActiveWorkbook.Save
       Else
          ActiveWorkbook.SaveAs filePath & Application.PathSeparator & namePN & " PrtRev_ " _
             & nameRev & " SO_" & nameSO & " ILRev2.xls"
       End If
    End If
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  4. #4
    Registered User
    Join Date
    11-26-2008
    Location
    New England
    MS-Off Ver
    2007
    Posts
    35

    Re: searching folder for existing file

    Mudraker:
    (just getting back to this....)

    This code works EXCEPT when the file already exists in the folder. I'd like be able to choose not to overwrite the file without it kicking me out of the macro.

    This was my attempted solution, but it didn't work. I'm still getting the "regular" saveas dialog prompt. Is there anyway to skip this? I didn't see anything in the help file about using true/false....

    Ted

        Dim sPath As String
        Dim sFName As String
        Dim sResp As String
    
        sPath = "F:\QC RECORDS\411 INSPECTION LAYOUT\" & nameCust & Application.PathSeparator
        sFName = namePN & " PrtRev_ " & nameRev & " SO_" & nameSO & " ILRev2.xls"
        Range("p2") = sPath
    If Dir(sPath & sFName, vbNormal) = "" Then
        Range("p3") = "Dir(sPath & sFName, vbNormal) is empty"
        ' File does not exist - add normal save commands here
        BookRev2.SaveAs filePath & Application.PathSeparator & namePN & " PrtRev_ " _
             & nameRev & " SO_" & nameSO & " ILRev2.xls"
    Else
       If MsgBox("This file already exists. REV1" & Chr(13) & Chr(13) _
          & "Do you want to overwrite it?" & Chr(13) & Chr(13) & "No will put it in the review folder", _
            vbYesNo + vbDefaultButton2 + vbQuestion) = vbYes Then
            Range("p3") = "Dir(sPath & sFName, vbNormal) is full"
            BookRev2.SaveAs filePath & Application.PathSeparator & namePN & " PrtRev_ " _
                             & nameRev & " SO_" & nameSO & " ILRev2.xls"
             
        Else
        BookRev2.SaveAs "F:\QC RECORDS\411 INSPECTION LAYOUT\review files\" & namePN & " PrtRev_ " _
                             & nameRev & " SO_" & nameSO & " REVIEW DUPLICATE ILRev2.xls"
        End If

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1