Visual Basic - 如何使用另一个函数中的变量

时间:2014-11-18 00:46:02

标签: function vb6 basic

我已经检查了谷歌以及建议的答案,但遗憾的是没有运气。

我需要做的最后一件事就是让电子邮件将rateNbr变量读入电子邮件正文,但它只是空白。

我尝试将Public Function FuncRateCheckFile读为Public Function FuncRateCheckFile(ByVal rateNbr As String),尝试将其设置为在函数外部调用,但这会在其他地方调用时中断函数。 :(

以下是代码,并附有我所指的位置的评论:

Public Function FuncRateCheckFile()

    Dim blnContinue As Boolean
    Dim strLine As String
    Dim strSearchFor, strSearchWrd, LineCount, objFSO, objTextFile, arrLines
    Dim dteNow As Date
    Dim newDate As String
    '//==============================================================================================
    '//  DECLARED
    Dim rateNbr As String
    '//==============================================================================================

    FuncRateCheckFile = False
    blnContinue = True


    If blnContinue Then

        Const ForReading = 1
            'Get todays date and reformat it
            dteNow = DateValue(Now)
            newDate = Format(dteNow, "dd/MM/yy")
            strSearchWrd = newDate
            'Read the whole file
            Set objFSO = CreateObject("Scripting.FileSystemObject")
            Set objTextFile = objFSO.OpenTextFile(m_RateCheckFile, ForReading)

            LineCount = 0
            Do Until objTextFile.AtEndOfStream

                strLine = objTextFile.ReadLine()

                If InStr(strLine, strSearchWrd) <> 0 Then

                   arrLines = Split(strLine, vbCrLf)
                   LineCount = LineCount + 1

                End If
            Loop

                'Log a message to state how many lines have todays day, and if there are none, log an error
                If LineCount <> 0 Then
                    '//==============================================================================================
                    '//  "rateNbr" IS WHAT I AM TRYING TO GET TO PUT IN THE EMAIL
                    LogMessage "Rate file date is correct"
                    rateNbr = "Number of rates for " & newDate & " in the file recieved on " & newDate & " is " & LineCount
                    LogMessage rateNbr
                    EmailAdvice2
                    objTextFile.Close
                    '//==============================================================================================

                Else

                    blnContinue = False
                    LogError "Failed to retrieve Current Rate date, please check rate file.."
                    EmailAdvice
                    objTextFile.Close

                End If

    End If

    FuncRateCheckFile = blnContinue

    LogMessage "Completed Check Rate file"

End Function


Private Function EmailAdvice2()


Dim strSMTPFrom As String
Dim strSMTPTo As String
Dim strSMTPRelay As String
Dim strTextBody As String
Dim strSubject As String
Dim oMessage As Object
'//==============================================================================================
'//  DECLARED AGAIN
Dim rateNbr As String
'//==============================================================================================

Set oMessage = CreateObject("CDO.Message")
strSMTPFrom = "no-reply@work.com.au"
strSMTPTo = "me@work.com.au"
strSMTPRelay = "smtp.relay.com"
'//==============================================================================================
'//  THIS MAKES THE TEXT BODY BLANK, BUT THE EMAIL STILL SENDS
strTextBody = rateNbr
'//==============================================================================================
strSubject = "Todays rates"
'strAttachment = "full UNC path of file"

oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strSMTPRelay
oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
oMessage.Configuration.Fields.Update

oMessage.Subject = strSubject
oMessage.From = strSMTPFrom
oMessage.To = strSMTPTo
oMessage.textbody = strTextBody
'oMessage.AddAttachment strAttachment


oMessage.Send

End Function

我很肯定它是空白的,因为我在rateNbr下声明了EmailAdvice2(),然后没有给它任何东西来填充变量。但我不知道如何让它在FuncRateCheckFile()下调用变量。

感谢所有人的帮助。

1 个答案:

答案 0 :(得分:1)

正如Plutonix所说,这是一个范围问题。

移动&quot; rateNbr&#39;的声明。变量到类级别,并删除函数内的局部声明:

Dim rateNbr As String ' <-- out at class level it will be accessible from both functions

Public Function FuncRateCheckFile()
    ...
    ' REMOVE both the decalarations of "rateNbr" that are INSIDE your functions
    ...
End Function 

Private Function EmailAdvice2()
    ...
    ' REMOVE both the decalarations of "rateNbr" that are INSIDE your functions
    ...
End Function