Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scripting Runtime Object Library 3

Status
Not open for further replies.

jmrdaddy

Technical User
Jun 10, 2002
31
0
0
US
The MSDN website Logging Errors to a Text File offers the code below as an example of how to use the FileSystemObject.

Two questions (actually three):

1. Is the function GetTempDir a Windows API function and if so what dll file or other thing do I need to reference to use it.

2. I tried a “homemade” GetTempDir function and the code would never write the file. It would skip over the With txsStream part entirely. Can anyone tell mey what I'm doing wrong

Code:
Sub LogError(errX As ErrObject, _
             Optional strProcName As String)
            
   ' This procedure logs errors to a text file. It is used in
   ' this chapter to log synchronization errors.
   '
   ' Arguments:
   ' errX: A variable that refers to the VBA Err object.

   Dim fsoSysObj    As FileSystemObject
   Dim filFile      As File
   Dim txsStream    As TextStream
   Dim lngErrNum    As Long
   Dim strPath      As String
   Dim strErrText   As String
   
   Set fsoSysObj = New FileSystemObject
   
   ' Store error information.
   lngErrNum = errX.Number
   strErrText = errX.Description
   ' Clear error.
   errX.Clear
   ' Return Windows Temp folder.
   strPath = GetTempDir
   If Len(strPath) = 0 Then
      GoTo LogError_End
   End If
   
   On Error Resume Next
   ' See if file already exists.
   Set filFile = fsoSysObj.GetFile(strPath & APP_ERROR_LOG)
   ' If not, then create it.
   If Err <> 0 Then
      Set filFile = fsoSysObj.CreateTextFile(strPath & APP_ERROR_LOG)
   End If
   On Error GoTo 0

   ' Open file as text stream for reading.
   Set txsStream = filFile.OpenAsTextStream(ForAppending)
   ' Write error information and close.
   With txsStream
      .WriteLine lngErrNum
      .WriteLine strErrText
      If Len(strProcName) > 0 Then .WriteLine strProcName
      .WriteLine Now
      .WriteBlankLines 1
      .Close
   End With

LogError_End:
   Exit Sub
End Sub
Sub TestLogError()
   ' This procedure tests the LogError function.

   On Error Resume Next
   ' Raise an error.
   Err.Raise 11
   ' Log it.
   LogError Err, "TestLogError"
End Sub

 
I inputted the text GetTempDir on the page you linked to, and got five hits, most of them using this function, but in this link Building Applications with Microsoft Outlook Version 2002,.I found this function

[tt]Function GetTempDir
Const TemporaryFolder = 2, SystemFolder = 1, WindowsFolder = 0
On Error Resume Next
Dim fso, tfolder
Set fso = CreateObject("Scripting.FileSystemObject")
Set tfolder = fso.GetSpecialFolder(TemporaryFolder)
If Err then
GetTempDir = "Could not obtain temporary folder path."
Exit Function
End If
GetTempDir = lcase(tFolder.Path)
End Function[/tt]

See if you can make it work, else post back with some details.

Roy-Vidar
 
Yes, try Roy's suggestion and let us know. Notice that you are using:

Set fsoSysObj = New FileSystemObject

and Roy is using:

Set fso = CreateObject("Scripting.FileSystemObject")

It also may help to declare your variable filFile as:

filFile As Scripting.File rather than "File". It could be why Textstream does not seem to work.

Let us know.


Gerry
See my Paintings and Sculpture
 
I used the GetTempDir function Roy suggested and it works great. Thanks.
I prefixed all the FSO variables with "Scripting" Every thing seems to work fine until you get to this
Code:
   Set filFile = fsoSysObj.GetFile(strPath & APP_ERROR_LOG)
   ' If not, then create it.
   If Err <> 0 Then
      Set filFile = fsoSysObj.CreateTextFile(strPath & APP_ERROR_LOG)
   End If
It's like it can't or won't create the file. Incidentally, I do have full network rights to that folder.


 
Any errormessages? What is the contents of strPath? What is the contents of (what I assume is the constant) APP_ERROR_LOG?

I guess one possible culprit is that the GetTempDir folder (which I just copy pasted from the reference;-)) returns a path without trailing "\". You may want to check that, and do a

[tt]strPath = GetTempDir
If strPath = "Could not obtain temporary folder path." Then
' that's the returned text from the function if it fails
GoTo LogError_End
else
strpath = strpath & "\"
End If[/tt]

Roy-Vidar
 
I enclosed the APP_ERROR_LOG in quotes with a backslash and added ".txt" and it worked. I'll assume that the APP_ERROR_LOG was some constant or something that I didn’t pick up when I was reading. Problem solved. Thanks! Y'all are great
 
I never got the following row to work in Word 2003:
Set filFile = fsoSysObj.CreateTextFile("C:\TEST.txt")

I solved by using: fsoSysObj.OpenTextFile

I changed the following code:
Code:
   Set filFile = fsoSysObj.CreateTextFile(strPath & APP_ERROR_LOG)
   Set txsStream = filFile.OpenAsTextStream(ForAppending)
   With txsStream
      .WriteLine lngErrNum
   End With

To this new code:
Code:
   Dim sTmp as String
   sTmp = "C:\myfile.txt"
   Set txsStream = fsoSysObj.OpenTextFile(sTmp, ForWriting, True)
        
   With txsStream
       .WriteLine lngErrNum
   End With

Bengt Nilsson, DATAFANT AB, Sweden
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top