Hello World
I'm trying my hand at error handling
& printing the same to a log file
Issue:
If there is any error while extracting the report path of any file, print the error & file name to a log file
Here's the current macro I have to extract file details
and the associated logger macro
Please help
Thanks
P
I'm trying my hand at error handling
& printing the same to a log file
Issue:
If there is any error while extracting the report path of any file, print the error & file name to a log file
Here's the current macro I have to extract file details
Code:
[COLOR=#333333][IMG]http://icons.iconarchive.com/icons/double-j-design/ravenna-3d/24/File-Copy-icon.png[/IMG]
<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">Sub AgentInfo_Macro()
Application.ScreenUpdating = False 'De-initializes Excel Screen Flicker
'Declaring constants & variables
Dim xRow As Long 'Row ID
Dim xCol As Long 'Col ID
Dim xDirect 'Default Path
Dim xFname 'File Name
Dim InitialFoldr 'Default Folder to start from
Dim EntName 'Path Name + File Name
Dim text As String 'text to compare
Dim textline As String 'Line in text file
Const jobID As String = "jobID=" 'Constant for filtering Agent Files only
Dim i As Integer 'For loop to remove blanks
Dim openPos As Integer
Dim closePos As Integer
InitialFoldr = "D:\Users\pratnimk\Desktop" 'Startup folder to begin searching from
'Prompt window to browse and select desired folder
With Application.FileDialog(msoFileDialogFolderPicker) 'Folder picker
.InitialFileName = Application.DefaultFilePath & "" 'Default path set
.Title = "Please select a folder to list files from" 'Prompt message
.InitialFileName = InitialFoldr 'Initialization of default path
.Show
'Selection of folder, path & file names based on -> jobID= <- keyword
If .SelectedItems.Count <> 0 Then
xDirect = .SelectedItems(1) & ""
xFname = Dir(xDirect)
Sheets("Sheet1").Activate
Range("A2").Select 'Active cell A2
Do While xFname <> "" 'While loop starts when File Names are retrieved under desired folder
EntName = 0 'Initialization of EntName i.e. Path Name + File Name
text = 0 'Initialization of text in files
EntName = xDirect & xFname 'Assigning Path + File Name to EntName
'To check if JobID exists in file -> proving the file is related to Agents
Open EntName For Input As #1 'Opening file
Do Until EOF(1)
Input #1, textline
text = text & textline 'Initializing to 1st line in text
openPos = 0
closePos = 0
'Searching for text -> jobID
If InStr(text, jobID) <> 0 Then
ActiveCell.Offset(xRow, xCol) = Mid(text, InStr(text, jobID) + 7, Len(textline)) 'Extracting JobID to Excel
closePos = InStr(ActiveCell.Offset(xRow, xCol), """")
ActiveCell.Offset(xRow, xCol) = Mid(ActiveCell.Offset(xRow, xCol), openPos + 1, closePos - openPos - 1)
Else 'Exiting File
GoTo SkipAhead
End If
Loop
Close #1 'Closing the text file
xCol = xCol + 3
[B][I]'Error Handling for Report Path
On Error GoTo Error_Report_Path[/I][/B]
'Searching for report path
text = 0
Open EntName For Input As #1 'Opening file
Do Until EOF(1)
Input #1, textline
text = text & textline 'Initializing to 1st line in text
openPos = 0
closePos = 0
'Searching for text -> report path
If InStr(text, "path=") <> 0 Then
ActiveCell.Offset(xRow, xCol) = Mid(text, InStr(text, "path=") + 5, Len(textline)) 'Extracting report path to Excel
openPos = InStr(ActiveCell.Offset(xRow, xCol), """")
closePos = InStr(ActiveCell.Offset(xRow, xCol), ">")
ActiveCell.Offset(xRow, xCol) = Mid(ActiveCell.Offset(xRow, xCol), openPos + 1, closePos - openPos - 3)
Else
ActiveCell.Offset(xRow, xCol) = "No underlying report"
End If
Loop
SkipAhead:
Close #1 'Closing the text file
xRow = xRow + 1
xCol = 0
xFname = Dir
Loop
End If
End With
Application.ScreenUpdating = True 'Re-initializes Excel Screen Flicker
Done:
Exit Sub
[B][I]Error_Report_Path:
Logger "Report Path Error", Err.Number, Err.Description
Resume Next[/I][/B]
End Sub</code>[/COLOR]
and the associated logger macro
Code:
[COLOR=#333333][IMG]http://icons.iconarchive.com/icons/double-j-design/ravenna-3d/24/File-Copy-icon.png[/IMG]
<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">Sub Logger(sType As String, sSource As String, sDetails As String)
Dim sFilename As String
sFilename = "D:\Users\pratnimk\Desktop\AgentInfo_log.txt"
' Archive file at certain size
If FileLen(sFilename) > 20000 Then
FileCopy sFilename _
, Replace(sFilename, ".txt", Format(Now, "ddmmyyyy hhmmss.txt"))
Kill sFilename
End If
' Open the file to write
Dim filenumber As Variant
filenumber = FreeFile
Open sFilename For Append As #filenumber
Print #filenumber, CStr(Now) & "," & sType & "," & sSource _
& "," & sDetails & "," & Application.UserName
Close #filenumber
End Sub
</code>[/COLOR]
Please help
Thanks
P
Last edited: