函数总是返回 False.为什么? [英] Function always returns False. Why?

查看:79
本文介绍了函数总是返回 False.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为 ThisOutlookSession 的每个邮件项目都有一个 VBA 脚本,并忽略了与我当前问题无关的代码部分,我正在调用以下函数:

I have a VBA script per mail item for ThisOutlookSession and ignoring the irrelevant parts of the code to my current problem, I am calling 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

我调用这个函数并测试它是否相应地返回TrueFalse:

I call this function and test if it returns True or False accordingly:

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

然而,当函数被调用时,MsgBox 显示它正在设置 True 但是另一个 MsgBox 显示该函数正在设置 .也许问题在于 WriteBatFile = True 行?

However, when the function is called the MsgBox shows it is setting True but then the other MsgBox shows the function is setting False. Perhaps the problem is the line WriteBatFile = True?

当然,Shell 命令永远不会运行.我需要你的帮助,告诉我为什么?

Of course, the Shell command is never run. I would like your help showing me why?

谢谢.

推荐答案

我的想法是总是可以到达 error_handle 标签.您需要在成功运行代码后退出该函数.WriteBatFile = True

My thought is that the error_handle label is always being reached. You need to exit the function after the successful code has been run. Add Exit Function after WriteBatFile = True

这篇关于函数总是返回 False.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆