Consulting

Results 1 to 2 of 2

Thread: Solved: Saving e-mails on the server if the name already exists?

  1. #1
    VBAX Contributor JeffT's Avatar
    Joined
    Oct 2004
    Location
    Maidenhead, Nr London UK
    Posts
    105
    Location

    Solved: Saving e-mails on the server if the name already exists?

    Hi

    I'm writing some code to save e-mails onto the Server at work, and with all your help have most of the problems solved. I now have the problem that if there is already an e-mail of the same name it just gets overwritten (I think, at least I don't get two with the same name).

    What I want to do is, If the subject of the e-mail is the same as one already in the particular folder on the server, strip off the .msg at the end add (1) or (2), (3) etc with a loop until the name will be different then add the .msg back on the end and save.

    I use the '&' to concatenate the name as a string with the reverse date at the front and .msg at the end so I wondered if there was a similar command to strip the last 4 characters eg ".msg", so I could add the (1) etc then replace the .msg at the end?

    I'm also not sure if my current code actually checks the name on the server before saving and whether there is a completely different way of approaching this?

    In addition I'm not sure how long it'd take to check all the already saved files and e-mails for repeat names when there could be thousands of files in the folder being checked?

    Regards

    Jeff T

  2. #2
    Knowledge Base Approver
    The King of Overkill!
    VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Hi Jeff,

    I'll take a look at your form (other thread) in a couple minutes, but figured I'd knock off this one quickly for you:[vba] 'For this example:
    ' vPath is the path you're saving the message to (ex "N:\folder\")
    ' YourSaveName is the filename you're trying to save to (ex "20070611JeffT.msg")

    Dim vSubj As String, vExt As String, vInc As Long

    'dir(path & file) returns a filename if it exists, blank if not
    'using this, we can see if the file already exists

    vInc = InStrRev(YourSaveName, ".") 'find position of "."
    vSubj = Left(YourSaveName, vInc - 1) 'everything up to final "." (ex "20070611JeffT")
    vExt = Mid(YourSaveName, vInc) 'extension, including "." (ex ".msg")
    vInc = 2 'set initial increment (ex "(2)")

    Do Until Len(Dir(vPath & YourSaveName)) = 0 'loop until non-existing name found
    YourSaveName = vSubj & Format(vInc, " (0)") & vExt
    vInc = vInc + 1
    Loop

    'your code to save the file goes here, using vPath & YourSaveName[/vba]
    Matt

Posting Permissions

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