L
LeAnn
I am using Access 2003 (2000 format) and Excel 2003. I have a procedure
that:
1) imports all text files in a particular directory into Excel; 2) copies a
specific range into a different Excel template; 3) closes the imported file
without saving; and saves template with the copied range into a different
directory. Everything works fine but we discovered that it’s possible to
have the final file name duplicated and windows over writes the first file.
I would like to search the destination directory (different from #1) for the
existence of the file and if found do something else with the second file for
investigation. My code is below. How could I search the UDLOCDIR
directory for the existence of UDLOCDIR & strBox & ".xls" before saving?
Thanks
LeAnn
**********************
Sub cmdSCN_Click()
'Set the environment and variables
Dim lngLFile As Long, strLFile As String 'FreeFile number for
log, Log File name
Dim strFile As String 'File name
Dim objApp As Object 'Excel Application
Dim objBook As Object, objBook2 'Excel Workbooks
Dim objSheet As Object, objSheet2 'Excel Worksheets
Dim lngCount As Long 'Loop counter
Dim strBox As String ‘Box name – first unit in file
On Error GoTo ErrorHandler:
'Create the import log
lngLFile = FreeFile
Open UDLOGDIR & "SCN_LOG" & Format(Now, "mmddyyhhmmss") & ".txt" For Output
As lngLFile
Print #lngLFile, "Convert SCN files"
Print #lngLFile, "Processed By: " & strUName
Print #lngLFile, "Processed Files:"
'For each Sample Locator excel file in the directory do the following:
strFile = Dir(UDSCNDIR) 'return first file name in
the process directory
If strFile = "" Then
MsgBox "There are no files in the SCN directory.", vbOKOnly +
vbExclamation, "Error"
Print #lngLFile, "No files found in directory"
Close
Reset
Exit Sub
End If
lngCount = 0
'Import the *.scn files and copy unit range into locator template
Do Until strFile = ""
'Open excel
Set objApp = CreateObject("Excel.Application")
objApp.DisplayAlerts = False
objApp.Workbooks.OpenText Filename:=UDSCNDIR & strFile, _
Origin:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True,
Semicolon:=False, _
Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(1,
2), _
TrailingMinusNumbers:=True
Set objSheet = objApp.Worksheets(1)
objSheet.Range("A25").Select
objSheet.Range("A25:A280").Select
objApp.Selection.Copy
'Open Locator template and paste selection in cell D2
Set objBook2 = objApp.Workbooks.Open(UDSLOCATOR)
Set objSheet2 = objBook2.Worksheets(1)
objSheet2.Range("D2").Select
objSheet2.Paste
strBox = objSheet2.Range("A2").Value
objSheet2.Range("A1").Select
objBook2.SaveAs UDLOCDIR & strBox & ".xls"
Print #lngLFile, strFile
strFile = Dir
lngCount = lngCount + 1
objBook2.Close
Loop
Print #lngLFile, vbCr & vbCr & lngCount & " SCN Files processed."
MsgBox lngCount & " SCN Files processed. Please check the directory below:"
& Chr(10) & Chr(10) & UDLOCDIR
Exit_Sub:
Close
Reset
objApp.Quit
Set objSheet = Nothing
Set objSheet2 = Nothing
Set objBook2 = Nothing
Set objApp = Nothing
Exit Sub
ErrorHandler:
MsgBox "Error #" & Err.Number & " - Description: " & Err.Description,
vbOKOnly + vbExclamation, "Error"
Resume Exit_Sub
End Sub
that:
1) imports all text files in a particular directory into Excel; 2) copies a
specific range into a different Excel template; 3) closes the imported file
without saving; and saves template with the copied range into a different
directory. Everything works fine but we discovered that it’s possible to
have the final file name duplicated and windows over writes the first file.
I would like to search the destination directory (different from #1) for the
existence of the file and if found do something else with the second file for
investigation. My code is below. How could I search the UDLOCDIR
directory for the existence of UDLOCDIR & strBox & ".xls" before saving?
Thanks
LeAnn
**********************
Sub cmdSCN_Click()
'Set the environment and variables
Dim lngLFile As Long, strLFile As String 'FreeFile number for
log, Log File name
Dim strFile As String 'File name
Dim objApp As Object 'Excel Application
Dim objBook As Object, objBook2 'Excel Workbooks
Dim objSheet As Object, objSheet2 'Excel Worksheets
Dim lngCount As Long 'Loop counter
Dim strBox As String ‘Box name – first unit in file
On Error GoTo ErrorHandler:
'Create the import log
lngLFile = FreeFile
Open UDLOGDIR & "SCN_LOG" & Format(Now, "mmddyyhhmmss") & ".txt" For Output
As lngLFile
Print #lngLFile, "Convert SCN files"
Print #lngLFile, "Processed By: " & strUName
Print #lngLFile, "Processed Files:"
'For each Sample Locator excel file in the directory do the following:
strFile = Dir(UDSCNDIR) 'return first file name in
the process directory
If strFile = "" Then
MsgBox "There are no files in the SCN directory.", vbOKOnly +
vbExclamation, "Error"
Print #lngLFile, "No files found in directory"
Close
Reset
Exit Sub
End If
lngCount = 0
'Import the *.scn files and copy unit range into locator template
Do Until strFile = ""
'Open excel
Set objApp = CreateObject("Excel.Application")
objApp.DisplayAlerts = False
objApp.Workbooks.OpenText Filename:=UDSCNDIR & strFile, _
Origin:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True,
Semicolon:=False, _
Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(1,
2), _
TrailingMinusNumbers:=True
Set objSheet = objApp.Worksheets(1)
objSheet.Range("A25").Select
objSheet.Range("A25:A280").Select
objApp.Selection.Copy
'Open Locator template and paste selection in cell D2
Set objBook2 = objApp.Workbooks.Open(UDSLOCATOR)
Set objSheet2 = objBook2.Worksheets(1)
objSheet2.Range("D2").Select
objSheet2.Paste
strBox = objSheet2.Range("A2").Value
objSheet2.Range("A1").Select
objBook2.SaveAs UDLOCDIR & strBox & ".xls"
Print #lngLFile, strFile
strFile = Dir
lngCount = lngCount + 1
objBook2.Close
Loop
Print #lngLFile, vbCr & vbCr & lngCount & " SCN Files processed."
MsgBox lngCount & " SCN Files processed. Please check the directory below:"
& Chr(10) & Chr(10) & UDLOCDIR
Exit_Sub:
Close
Reset
objApp.Quit
Set objSheet = Nothing
Set objSheet2 = Nothing
Set objBook2 = Nothing
Set objApp = Nothing
Exit Sub
ErrorHandler:
MsgBox "Error #" & Err.Number & " - Description: " & Err.Description,
vbOKOnly + vbExclamation, "Error"
Resume Exit_Sub
End Sub