调用ROBOT的VBA函数
调用ROBOT的VBA函数
经过几天的调试,终于将EXCEL下调用ROBOT的VBA函数写出来了,主要功能是要取得errorlevel的值,根据errorlevel值来判断短信是否发送成功,值为0 则成功。在此要感谢常堂主的精巧的软件ROBOT,呵呵~~
ROBOT的全部程序在放在EXCEL文件同目录的ROBOT子目录下:
Function SendMSG(CmdStr As Variant, Mode As Integer) As String
'调用ROBOT
'取返回值——文件方式
ExitCodeFile = ThisWorkbook.Path & "\"
BatchFile = ThisWorkbook.Path & "\Robot\FETION.bat"
Set fs = CreateObject("Scripting.FileSystemObject")
If fs.FileExists(BatchFile) Then fs.DeleteFile (BatchFile) '如果有FETION批处理文件,则删除
Set a = fs.CreateTextFile(BatchFile, True) '覆盖创建FETION.bat
ROBOT的全部程序在放在EXCEL文件同目录的ROBOT子目录下:
Function SendMSG(CmdStr As Variant, Mode As Integer) As String
'调用ROBOT
'取返回值——文件方式
ExitCodeFile = ThisWorkbook.Path & "\"
BatchFile = ThisWorkbook.Path & "\Robot\FETION.bat"
Set fs = CreateObject("Scripting.FileSystemObject")
If fs.FileExists(BatchFile) Then fs.DeleteFile (BatchFile) '如果有FETION批处理文件,则删除
Set a = fs.CreateTextFile(BatchFile, True) '覆盖创建FETION.bat
a.WriteLine "@ECHO OFF"
a.WriteLine CmdStr & " | Find ""280 Send SMS OK""" 'Robot并检查是否成功,然后马上将返回值ERRORLEVEL写到文件,值为0表示成功
a.WriteLine "ECHO %ERRORLEVEL% > " & ExitCodeFile '将返回值输入到中
a.WriteLine "EXIT" '必须在批处理中加入EXIT,否则不能自动退出
a.Close
ProcessID = Shell(" /k " & BatchFile, Mode) '执行批处理文件,返回ERRORLEVEL值
'判断SHELL是否在运行,是的话就等待
While IsRunning(ProcessID)
DoEvents
Wend
Shell " /k ECHO . >" & BatchFile, vbHide '将Fetion.bat清空,
a.WriteLine CmdStr & " | Find ""280 Send SMS OK""" 'Robot并检查是否成功,然后马上将返回值ERRORLEVEL写到文件,值为0表示成功
a.WriteLine "ECHO %ERRORLEVEL% > " & ExitCodeFile '将返回值输入到中
a.WriteLine "EXIT" '必须在批处理中加入EXIT,否则不能自动退出
a.Close
ProcessID = Shell(" /k " & BatchFile, Mode) '执行批处理文件,返回ERRORLEVEL值
'判断SHELL是否在运行,是的话就等待
While IsRunning(ProcessID)
DoEvents
Wend
Shell " /k ECHO . >" & BatchFile, vbHide '将Fetion.bat清空,
writeline函数因为bat中有飞信密码
'取返回值——文件方式
Open ExitCodeFile For Input As #1
Input #1, Result '从中取出ErrorLevel的返回值
Close #1
If Result = 0 Then
SendMSG = "短信已发送"
Else
SendMSG = "未发送"
End If
End Function
Function IsRunning(ByVal ProcessID) As Boolean
'用以判断程序是否在运行
'取返回值——文件方式
Open ExitCodeFile For Input As #1
Input #1, Result '从中取出ErrorLevel的返回值
Close #1
If Result = 0 Then
SendMSG = "短信已发送"
Else
SendMSG = "未发送"
End If
End Function
Function IsRunning(ByVal ProcessID) As Boolean
'用以判断程序是否在运行
Dim hProcess As Long '程序进程句柄
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, ProcessID)
If hProcess <> 0 Then
IsRunning = True
Else
IsRunning = False
End If
CloseHandle hProcess
End Function
VBA语句调用例子:
Mobile = "13888xxxxxx"
PWD = "xxxxxx"
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, ProcessID)
If hProcess <> 0 Then
IsRunning = True
Else
IsRunning = False
End If
CloseHandle hProcess
End Function
VBA语句调用例子:
Mobile = "13888xxxxxx"
PWD = "xxxxxx"
CmdStr = ThisWorkbook.Path & "\ --mobile=" & Mobile & " --pwd=" & PWD & " --msg-type=1 --to=" & Mobile & " --msg-gb=测试 --debug "
MsgBox SendMSG(CmdStr, vbNormalFocus) ‘vbNormalFocus显示发送过程,vbHide是隐藏显示
MsgBox SendMSG(CmdStr, vbNormalFocus) ‘vbNormalFocus显示发送过程,vbHide是隐藏显示
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论