The function always returns False. What for?

I have a VBA script for the mail item for ThisOutlookSessionand ignoring non-essential parts of the code for my current problem, I call the following function:

Function WriteBatFile(inVar As String) As Boolean
On Error GoTo err_handle:

    Dim sFile As String
    sFile = "C:\Users\ME\Desktop\myScript.bat"

    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Dim oFile As Object
    Set oFile = fso.CreateTextFile(sFile)

    oFile.WriteLine "sleep 2"
    oFile.WriteLine "./myScript.sh " & inVar
    oFile.WriteLine "exit"
    oFile.Close

    Set fso = Nothing
    Set oFile = Nothing

    'MsgBox "Setting True", vbInformation
    WriteBatFile = True

err_handle:
    'MsgBox "Setting false. Code: "
    WriteBatFile = False
    Exit Function
End Function

I call this function and check if it returns Trueor False:

result = WriteBatFile(match.Value)
    If result = True Then
        retval = Shell("""C:\Program Files (x86)\PuTTY\plink.exe"" -ssh ME@MYSERVER -m C:\Users\ME\Desktop\runThese.bat", vbNormalFocus)
    End If

However, when a function is called, it MsgBoxindicates that it is installing True, but then another MsgBoxindicates that the function is installing False. Perhaps the problem is the line WriteBatFile = True?

Of course, a team Shellnever starts. I would like your help to show me why?

Thank.

+4
source share
3 answers

, error_handle . . Exit Function WriteBatFile = True

+1

err_handle - lable, , , , X err_handle GoTo . , . - Exit Function , WriteBatFile True. , :

'MsgBox "Setting True", vbInformation
WriteBatFile = True
Exit Function

err_handle:
    'MsgBox "Setting false. Code: "
    WriteBatFile = False
End Function

err_handle WriteBatFile False, natrually, End Function. , !

+5

, WriteBatFile true, .

 WriteBatFile = True

err_handle:
    'MsgBox "Setting false. Code: "
    WriteBatFile = False

-

If err.number <> 0 then

    WriteBatFile = False

Else

   WriteBatFile = True

End If
0

Source: https://habr.com/ru/post/1539776/


All Articles