Logo 
Search:

MS Office Answers

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds
  Question Asked By: Navin Gada   on Jan 02 In MS Office Category.

  
Question Answered By: Cais Nguyen   on Jan 02

An easy technique we use a lot here is to create (empty) text files  with
specific names as different steps in a long process are completed. For example:

Sub CreateCSV(WhichOne As String, OK As Boolean)
Dim fs As Object, a As Object
Set fs = CreateObject("Scripting.FileSystemObject")
Application.DisplayAlerts = False
If WhichOne = "PL" Then
If OK = True Then
Set a = fs.CreateTextFile("C:\temp\diffpl_dfs_iszero.csv", True)
a.Close ' Close the text file
Else
Set a = fs.CreateTextFile("C:\temp\diffpl_dfs_isnotzero.csv", True)
a.Close ' Close the text file
End If
ElseIf WhichOne = "BS" Then
If OK = True Then
Set a = fs.CreateTextFile("C:\temp\diffbs_dfs_iszero.csv", True)
a.Close ' Close the text file
Else
Set a = fs.CreateTextFile("C:\temp\diffbs_dfs_isnotzero.csv", True)
a.Close ' Close the text file
End If
End If
Set a = Nothing
Set fs = Nothing
End Sub

WhichOne identifies the process (in my example, either BS or PL); OK is a
good/bad status indicator. Your first process can create the appropriate text
file before it ends, and your second process can check for the expected file
when it starts. You will need to delete the files created the previous day
first.

The above sub might be called like this:
Call CreateCSV("PL", True)
Call CreateCSV("PL", False)
Call CreateCSV("BS", True)
Call CreateCSV("BS", False)

Then I could use code  like the following to check for the existence of any of
them:

Sub BBB()
Dim Fyle As String, FylePath As String
Fyle$ = "diffpl_dfs_iszero.csv"
FylePath$ = "C:\Temp\"
If Not FileExists(FylePath$, Fyle$) Then
MsgBox Fyle$ & " was not found", vbExclamation, "ERROR"
Exit Sub
End If
End Sub

Public Function FileExists(InPath As String, InFile As String) As Boolean
'Use Dir function to see if the desired file is found in the specified path.
'Declare variables for this macro.
Dim FyleName As String
'Get the names of all the files in the specified folder. Have to get the first
'one outside of the loop (quirk of the Dir function).
FyleName$ = Dir(InPath$ & "*.*")
Do While FyleName$ <> ""
If FyleName$ = InFile$ Then
FileExists = True
Exit Function
End If
FyleName$ = Dir()
Loop
FileExists = False
End Function

Share: 

 

This Question has 3 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Specifying Excel Return Code Or get search suggestion and latest updates.


Tagged: