Assigning the File path to the variable strFilePath. ![]() Declaring the strFilePath variable as String Data Type to store the text file path. Declaring iCntr variable as Long Data Type to use it in for loop to repeat the range A1 to A10. In this case it would be the ones inheriting from IOException. Starting the program and sub procedure to write VBA code to read data from Excel and write the data to a text file. Note: When you catch exceptions, don't catch the base class Exception, catch only the ones that are releveant. Sw.WriteLine("Error Message in Occured at- " & DateTime.Now) Sw.WriteLine("Start Error Log for today") Dim strFile As String = "C:\ErrorLog_" & ("dd-MMM-yyyy") & ".txt" And you forgot to write the error message to the file if it didn't exist. You also forgot to close the StreamWriter in the case where the file didn't exist, so it would most likely still be locked when you would try to write to it the next time. I would suggest that you use the CreateText method instead in this case, as it returns a StreamWriter. ![]() You either write to the file using the FileStream or close it before writing to it. The Create method opens the file and returns a FileStream object. You didn't close the file after creating it, so when you write to it, it's in use by yourself.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |