PDA

View Full Version : Solved: Casting to Text



Djblois
07-12-2007, 01:27 PM
I recently updated the log file that my add-in uses. Originally, it updated the log file only when the program stopped running. Now it updates during each sub-procedure. Also, now I log all the variables I pass between Sub-procedures and fucntions. The only variables I have not been able to log are Ranges, Worksheets, and Workbooks. I have tried to cast them to as string but it still won't work. Here is the code for one of them that I tried:

Function FinalRow(ByVal shtToCount As Worksheet) As Long

errUpdateLogFile "Function-FinalRow", FinalRow
errUpdateLogFile "shtToCount", shtToCount

End Function

Sub errUpdateLogFile(ByVal stgSetting As String, Optional ByVal stgSetting2 As String)

'Create Log
If stgSetting2 = "" Then
Write #FileNumber, stgSetting
Else
Write #FileNumber, stgSetting & " " & stgSetting2
End If

End Sub


Also, I forgot I have tried to do it for Array's as well:

Sub errUpdateLogFileforArray(ByVal stgSetting As String, ByVal aSetting As Variant)

Dim logFileLine As Long
'Create Log

For logFileLine = LBound(aSetting, 1) To UBound(aSetting, 1)
Write #FileNumber, stgSetting & " " & aSetting(logFileLine, 0) & "#" & _
aSetting(logFileLine, 1)
Next logFileLine

End Sub

Bob Phillips
07-12-2007, 03:04 PM
Use object properties

Function FinalRow(ByVal shtToCount As Worksheet) As Long
errUpdateLogFile "Function-FinalRow", FinalRow
errUpdateLogFile "shtToCount", shtToCount.Name
End Function

Djblois
07-13-2007, 07:27 AM
XLD,

That seems to work perfectly. However, did you get a chance to look at the way I am trying to Log Arrays? I can't get that to work either?

Bob Phillips
07-13-2007, 07:36 AM
You don't say what is not working, so out of context, it is difficult for me to say. Have you defined a value for FileNumber?

Djblois
07-13-2007, 08:05 AM
Sorry. Here is an update:



Sub errUpdateLogFileforArray(ByVal stgSetting As String, ByVal aSetting As Variant)


Dim logFileLine As Long
'Create Log
'I get an error on the next line
For logFileLine = LBound(aSetting, 1) To UBound(aSetting, 1)
Write #FileNumber, stgSetting & " " & aSetting(logFileLine, 0) & "#" & _
aSetting(logFileLine, 1)
Next logFileLine


End Sub


The rest of the code works because I define the fileNumber when the program starts and throughout the program I use it to to log what was done. Only in a few places do I use arrays that I want to log but it crashes on the line that I have indicated. Stgsetting is the name of the passed Array and aSetting is the Array itself.

Here is the code i use to start the logging at the beginning of the program:

Sub errStartDebugSettings(ByVal stgSub As String)

Dim logFile As String

logFile = "H:\@Temp\Daniel B\Log Files\" & Environ("UserName") & "_LogFile.txt"

FileNumber = FreeFile ' Get unused fileno
'Create file name.
Open logFile For Append As #FileNumber
Write #FileNumber, Now & " " & stgSub
End Sub

Then I end the program with this:

Sub errEndLogFile()

Write #FileNumber, Now & " " & "Completed"
Close #FileNumber
End Sub

That is all the code I use in the logging process

Bob Phillips
07-13-2007, 08:46 AM
Show me a call to errUpdateLogFileforArray

Djblois
07-13-2007, 10:48 AM
Sub comSaveAnytingWithX(ParamArray aDataToKeep() As Variant)

Dim keep As Variant

errUpdateLogFile "SaveAnytingWithX"
errUpdateLogFileforArray "aDataToKeep", aDataToKeep

comSaveAnytingWithX "AT", "CRN"

Bob Phillips
07-13-2007, 11:48 AM
You are not pasisng arrays. Each elemenbt of aDataToKeep is a string, so no wonder the errUpdateLogFileforArray chokes.

Djblois
07-13-2007, 12:45 PM
but when I tried to log it like I do the other strings it also crashes

Bob Phillips
07-13-2007, 02:17 PM
What you should be doing is



Sub comSaveAnytingWithX(ParamArray aDataToKeep() As Variant)
Dim keep As Variant
Dim i As Long

For i = LBound(aDataToKeep) To UBounf(aDataToKeep)
errUpdateLogFile "SaveAnytingWithX"
errUpdateLogFile "aDataToKeep", aDataToKeep(i)
Next i