473,378 Members | 1,475 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Checkfile exits then append

1
Hi I have a program that download files from the email and puts it into the corresponding folder.

What i would like to know is how to sort and then appned if the file already exists i.e i get strString = "9301ARUP" . I'm checking for arup

if strString.instr("ARUP")> 0
if strString.checkfileexist("true") then
add a letter to the end
then save the file to a folder
else
then save the file to a folder

However This is for one type of file I would like to have this through a case statement

Select Case (strString)
Case "ARUP"
Case "BDR"
Case "DDTL" .........

Approximately 30 different files

and saves it to their perspective folders

******************************** HERE IS THE CODE **********************************
Option Explicit


Sub GetAttachments()
On Error GoTo GetAttachments_err
' Declare variables
Dim appOl As New Outlook.Application
Dim ns As Outlook.NameSpace
Dim Inbox As Outlook.MAPIFolder
Dim Item As Object
Dim Atmt As Outlook.Attachment
Dim FileName As String
Dim i As Integer
Set ns = appOl.GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
i = 0
' Check Inbox for messages and exit of none found
If Inbox.Items.Count = 0 Then
MsgBox "There are no messages in the Inbox.", vbInformation, _
"Nothing Found"
Exit Sub
End If
' Check each message for attachments
For Each Item In Inbox.Items
' Save any attachments found
For Each Atmt In Item.Attachments
' This path must exist! Change folder name as necessary.
FileName = "C:\Email Attachments\" & Atmt.FileName
Atmt.SaveAsFile FileName
i = i + 1
Next Atmt
Next Item
' Show summary message
If i > 0 Then
MsgBox "I found " & i & " attached files." _
& vbCrLf & "I have saved them into the C:\Email Attachments folder." _
& vbCrLf & vbCrLf & "Have a nice day.", vbInformation, "Finished!"
Else
MsgBox "I didn't find any attached files in your mail.", vbInformation, "Finished!"
End If
' Clear memory
GetAttachments_exit:
Set Atmt = Nothing
Set Item = Nothing
Set ns = Nothing
Set appOl = Nothing
Exit Sub
' Handle errors
GetAttachments_err:
MsgBox "An unexpected error has occurred." _
& vbCrLf & "Please note and report the following information." _
& vbCrLf & "Macro Name: GetAttachments" _
& vbCrLf & "Error Number: " & Err.Number _
& vbCrLf & "Error Description: " & Err.Description _
, vbCritical, "Error!"
Resume GetAttachments_exit
End Sub

Sub SaveAttachmentsToFolder()

On Error GoTo SaveAttachmentsToFolder_err
' Declare variables
Dim DateCode As String 'Input from user the current date
Dim strText2 As String
Dim strText1 As String 'For Trimming the file name for case statements
Dim strText As String ' For getting the file name
Dim appOl As New Outlook.Application
Dim ns As Outlook.NameSpace
Dim Inbox As Outlook.MAPIFolder
Dim SubFolder As Outlook.MAPIFolder
Dim Item As Object
Dim Atmt As Outlook.Attachment
Dim FileName As String
Dim FileName2 As String
Dim i As Integer
Dim strText5 As String
Dim varResponse As Variant
Set ns = appOl.GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)

Set SubFolder = Inbox.Folders("ADMINLC") ' Enter correct subfolder name.
i = 0
' Check subfolder for messages and exit of none found
If SubFolder.Items.Count = 0 Then
MsgBox "There are no messages in the ADMINLC folder.", vbInformation, _
"Nothing Found"
Exit Sub
End If
' Check each message for attachments
DateCode = InputBox("Please Enter the date for the files")
For Each Item In SubFolder.Items
For Each Atmt In Item.Attachments
strText = Atmt.FileName
strText1 = Left(strText, 3) 'Right(strText, Len(strText) - 3)
strText5 = Right(strText1, 3)




'If strText1 = "AGE" Or strText1 = "API" Or strText1 = "APR" Or strText1 = "ARE" Or strText1 = "ARL" or strText = Case "ARE"
'or strText1= "BDR" or strText1 = "COA" or strText1 = "DDT" OR strText1 = "DEP" OR strText1 = "RDE" OR strText1 = "DIV"
'OR strText1 = "DOC" OR strText1 = "DTR" OR strText1 = "DRO" OR strText1 =
'Case "ARL"
'Case "ARU"
'Case "BDR"
'Case "COA"
'Case "DDT"
'Case "DEP"
'Case "RDE"
'Case "DIV"
'Case "DOC"
'Case "DRO"
'Case "DTR"
'Case "GRN"

'Then


Select Case strText1
Case "AGE"
strText2 = Left(strText, Len(strText) - 4)
FileName = "C:\Email Attachments\AGE\" & strText2 & DateCode & ".TXT"
FileName2 = strText2 & DateCode
'MsgBox ("the file name" & DateCode)
'If LenB(Dir$("C:\Email Attachments\AGE\Filename"))
' FileName = "C:\Email Attachments\AGE\" & strText2 & DateCode & "A" & ".TXT"
Atmt.SaveAsFile FileName
' End If
Case "ARL"
strText2 = Left(strText, Len(strText) - 4)
FileName = "C:\Email Attachments\ARLD\" & strText2 & DateCode & ".TXT"
FileName2 = strText2 & DateCode
'MsgBox ("the file name" & DateCode)
'If LenB(Dir$("C:\Email Attachments\AGE\Filename"))
' FileName = "C:\Email Attachments\AGE\" & strText2 & DateCode & "A" & ".TXT"
Atmt.SaveAsFile FileName
' End If
Case "ARU"
strText2 = Left(strText, Len(strText) - 4)
FileName = "C:\Email Attachments\ARUP\" & strText2 & DateCode & ".TXT"
FileName2 = strText2 & DateCode
'MsgBox ("the file name" & DateCode)
'If LenB(Dir$("C:\Email Attachments\AGE\Filename"))
' FileName = "C:\Email Attachments\AGE\" & strText2 &
DateCode & "A" & ".TXT"
Atmt.SaveAsFile FileName
' End If
Case "BDR"
strText2 = Left(strText, Len(strText) - 4)
FileName = "C:\Email Attachments\BDR\" & strText2 &
DateCode & ".TXT"
FileName2 = strText2 & DateCode
'MsgBox ("the file name" & DateCode)
'If LenB(Dir$("C:\Email Attachments\AGE\Filename"))
' FileName = "C:\Email Attachments\AGE\" & strText2 &
DateCode & "A" & ".TXT"
Atmt.SaveAsFile FileName
' End If
Case "COA"
strText2 = Left(strText, Len(strText) - 4)
FileName = "C:\Email Attachments\COA\" & strText2 & DateCode & ".TXT"
FileName2 = strText2 & DateCode
'MsgBox ("the file name" & DateCode)
'If LenB(Dir$("C:\Email Attachments\AGE\Filename"))
' FileName = "C:\Email Attachments\AGE\" & strText2 & DateCode & "A" & ".TXT"
Atmt.SaveAsFile FileName
' End If
Case "DDT"
strText2 = Left(strText, Len(strText) - 4)
FileName = "C:\Email Attachments\DDTL\" & strText2 & DateCode & ".TXT"
FileName2 = strText2 & DateCode
'MsgBox ("the file name" & DateCode)
'If LenB(Dir$("C:\Email Attachments\AGE\Filename"))
' FileName = "C:\Email Attachments\AGE\" & strText2 & DateCode & "A" & ".TXT"
Atmt.SaveAsFile FileName
' End If
Case "DEP"
strText2 = Left(strText, Len(strText) - 4)
FileName = "C:\Email Attachments\DEPT57\" & strText2 & DateCode & ".TXT"
FileName2 = strText2 & DateCode
'MsgBox ("the file name" & DateCode)
'If LenB(Dir$("C:\Email Attachments\AGE\Filename"))
' FileName = "C:\Email Attachments\AGE\" & strText2 & DateCode & "A" & ".TXT"
Atmt.SaveAsFile FileName
' End If
Case "RDE"
strText2 = Left(strText, Len(strText) - 4)
FileName = "C:\Email Attachments\DEPT57\" & strText2 & DateCode & ".TXT"
FileName2 = strText2 & DateCode
'MsgBox ("the file name" & DateCode)
'If LenB(Dir$("C:\Email Attachments\AGE\Filename"))
' FileName = "C:\Email Attachments\AGE\" & strText2 & DateCode & "A" & ".TXT"
Atmt.SaveAsFile FileName
' End If
Case "DIV"
strText2 = Left(strText, Len(strText) - 4)
FileName = "C:\Email Attachments\Dist Trn\" & strText2 & DateCode & ".TXT"
FileName2 = strText2 & DateCode
'MsgBox ("the file name" & DateCode)
'If LenB(Dir$("C:\Email Attachments\AGE\Filename"))
' FileName = "C:\Email Attachments\AGE\" & strText2 & DateCode & "A" & ".TXT"
Atmt.SaveAsFile FileName
' End If
Case "DOC"
strText2 = Left(strText, Len(strText) - 4)
FileName = "C:\Email Attachments\DOC\" & strText2 & DateCode & ".TXT"
FileName2 = strText2 & DateCode
'MsgBox ("the file name" & DateCode)
'If LenB(Dir$("C:\Email Attachments\AGE\Filename"))
' FileName = "C:\Email Attachments\AGE\" & strText2 & DateCode & "A" & ".TXT"
Atmt.SaveAsFile FileName
' End If
Case "DRO"
strText2 = Left(strText, Len(strText) - 4)
FileName = "C:\Email Attachments\DROP\" & strText2 & DateCode & ".TXT"
FileName2 = strText2 & DateCode
'MsgBox ("the file name" & DateCode)
'If LenB(Dir$("C:\Email Attachments\AGE\Filename"))
' FileName = "C:\Email Attachments\AGE\" & strText2 & DateCode & "A" & ".TXT"
Atmt.SaveAsFile FileName
' End If
Case "DTR"
strText2 = Left(strText, Len(strText) - 4)
FileName = "C:\Email Attachments\DTRN\" & strText2 & DateCode & ".TXT"
FileName2 = strText2 & DateCode
'MsgBox ("the file name" & DateCode)
'If LenB(Dir$("C:\Email Attachments\AGE\Filename"))
' FileName = "C:\Email Attachments\AGE\" & strText2 & DateCode & "A" & ".TXT"
Atmt.SaveAsFile FileName
' End If
Case "GRN"
strText2 = Left(strText, Len(strText) - 4)
FileName = "C:\Email Attachments\GRNI\" & strText2 & DateCode & ".TXT"
FileName2 = strText2 & DateCode
'MsgBox ("the file name" & DateCode)
'If LenB(Dir$("C:\Email Attachments\AGE\Filename"))
' FileName = "C:\Email Attachments\AGE\" & strText2 & DateCode & "A" & ".TXT"
Atmt.SaveAsFile FileName
' End If

End Select

'Select Case strText5
' Case "-REJ"
' Case "ERO"
'Case "END"
'Case "GL"
'Case ""
' End Select



' This path must exist! Change folder name as necessary.
FileName = "C:\Email Attachments\" & Atmt.FileName
Atmt.SaveAsFile FileName
i = i + 1

Next Atmt
Next Item
' Show summary message
If i > 0 Then
varResponse = MsgBox("I found " & i & " attached files." _
& vbCrLf & "I have saved them into the C:\Email Attachments folder." _
& vbCrLf & vbCrLf & "Would you like to view the files now?" _
, vbQuestion + vbYesNo, "Finished!")
' Open Windows Explorer to display saved files if user chooses
If varResponse = vbYes Then
Shell "Explorer.exe /e,C:\Email Attachments", vbNormalFocus
End If
Else
MsgBox "I didn't find any attached files in your mail.", vbInformation, "Finished!"
End If

' Clear memory
SaveAttachmentsToFolder_exit:
Set Atmt = Nothing
Set Item = Nothing
Set ns = Nothing
Set appOl = Nothing
Exit Sub


' Handle Errors
SaveAttachmentsToFolder_err:
MsgBox "An unexpected error has occurred." _
& vbCrLf & "Please note and report the following information." _
& vbCrLf & "Macro Name: SaveAttachmentsToFolder" _
& vbCrLf & "Error Number: " & Err.Number _
& vbCrLf & "Error Description: " & Err.Description _
, vbCritical, "Error!"
Resume SaveAttachmentsToFolder_exit
End Sub
Aug 26 '08 #1
0 902

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Hank | last post by:
Please help me diagnose the following problem. I have a Swing program (running under 1.4.2_04) that uses JNI to get some hardware-level information and return that info as a csv String. The...
29
by: keredil | last post by:
Hi, Will the memory allocated by malloc get released when program exits? I guess it will since when the program exits, the OS will free all the memory (global, stack, heap) used by this...
2
by: lovecreatesbeauty | last post by:
Hello experts, When I allocate memory use malloc() and forget to call a corresponding free(), does this allocated memory lose forever after program exits (Can i regard it same as process...
4
by: Deadly_M | last post by:
I have a basic VB.NET "windows application" it does not need a form and is purely event driven. At the moment when its is ran it performs the Sub Main startup then simply EXITs because all the...
4
by: Tad Marshall | last post by:
Hi, I'm having limited luck getting an ApplicationException to work right in my code. This is VB.NET, VS 2003, Windows XP SP2, .NET Framework 1.1. I thought it would be convenient to take...
2
by: Jeff | last post by:
Hello, I assigned a new object to a local variable ("req") in a function (see below). The local variable "req" is obviously destroyed when the function exits, but should the object referenced by...
0
by: Petra | last post by:
Hi! I don't know how to handle the following problem: When a User clicks on the detail view of an item, I lock the item in the database. If the user exits with clicking the cancel or ok button...
8
by: Lonifasiko | last post by:
Hi, Using Process class I asynchronously launch an executable (black box executable) file from my Windows application. I mean asynchronously because I've got an EventHandler for "Exited" event....
0
by: vamsioracle | last post by:
hi all I just want to know what are user exits. Please provide me the detailed info about user exits in oracle reports thank you in advance
3
by: Brainsludge | last post by:
Hello, I am running WinXP SP2 with Python 2.5.1 and encountered the following issue: I wrote a script that logs into my mail server, and checks for new messages every 20 seconds. When a new...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.