Option ExplicitPrivate Const NORMAL_PRIORITY_CLASS = &H20& Private Const STARTF_USESTDHANDLES = &H100& Private Const STARTF_USESHOWWINDOW = &H1Private Type SECURITY_ATTRIBUTES nLength As Long lpSecurityDescriptor As Long bInheritHandle As Long End TypePrivate Type STARTUPINFO cb As Long lpReserved As Long lpDesktop As Long lpTitle As Long dwX As Long dwY As Long dwXSize As Long dwYSize As Long dwXCountChars As Long dwYCountChars As Long dwFillAttribute As Long dwFlags As Long wShowWindow As Integer cbReserved2 As Integer lpReserved2 As Long hStdInput As Long hStdOutput As Long hStdError As Long End TypePrivate Type PROCESS_INFORMATION hProcess As Long hThread As Long dwProcessID As Long dwThreadID As Long End TypePrivate Declare Function CreatePipe Lib "kernel32" (phReadPipe As Long, phWritePipe As Long, lpPipeAttributes As SECURITY_ATTRIBUTES, ByVal nSize As Long) As Long Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, ByVal lpBuffer As String, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Long) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongPublic Function retunCmdResult(strCommand As String) As String Dim Proc As PROCESS_INFORMATION '进程信息 Dim Start As STARTUPINFO '启动信息 Dim SecAttr As SECURITY_ATTRIBUTES '安全属性 Dim hReadPipe As Long '读取管道句柄 Dim hWritePipe As Long '写入管道句柄 Dim lngBytesRead As Long '读出数据的字节数 Dim strBuffer As String * 256 '读取管道的字符串buffer Dim Command As String 'DOS命令 Dim ret As Long 'API函数返回值 Dim lpOutputs As String '读出的最终结果
'设置安全属性 With SecAttr .nLength = LenB(SecAttr) .bInheritHandle = True .lpSecurityDescriptor = 0 End With
'创建管道 ret = CreatePipe(hReadPipe, hWritePipe, SecAttr, 0) If ret = 0 Then MsgBox "无法创建管道", vbExclamation, "错误" Exit Function End If
'设置进程启动前的信息 With Start .cb = LenB(Start) .dwFlags = STARTF_USESHOWWINDOW Or STARTF_USESTDHANDLES .hStdOutput = hWritePipe '设置输出管道 .hStdError = hWritePipe '设置错误管道 End With
'启动进程 Command = strCommand 'DOS进程以ipconfig.exe为例 ret = CreateProcess(vbNullString, Command, SecAttr, SecAttr, True, NORMAL_PRIORITY_CLASS, ByVal 0, vbNullString, Start, Proc) If ret = 0 Then MsgBox "无法启动新进程", vbExclamation, "错误" ret = CloseHandle(hWritePipe) ret = CloseHandle(hReadPipe) Exit Function End If
'因为无需写入数据,所以先关闭写入管道。而且这里必须关闭此管道,否则将无法读取数据 ret = CloseHandle(hWritePipe)
'从输出管道读取数据,每次最多读取256字节 Do ret = ReadFile(hReadPipe, strBuffer, 256, lngBytesRead, ByVal 0) lpOutputs = lpOutputs & Left(strBuffer, lngBytesRead) DoEvents Loop While (ret <> 0) '当ret=0时说明ReadFile执行失败,已经没有数据可读了
'读取操作完成,关闭各句柄 ret = CloseHandle(Proc.hProcess) ret = CloseHandle(Proc.hThread) ret = CloseHandle(hReadPipe)
retunCmdResult = lpOutputs End FunctionPrivate Sub Command1_Click() Text1.Text = retunCmdResult("tasklist") If InStr(1, Text1.Text, "POWERPNT.EXE", vbTextCompare) <> 0 Then Debug.Print "PPT 运行!" Else Debug.Print "PPT 没有运行!" End If End Sub
我的是可以找到一个PPT文件的,但是如果有多个运行PPT的话,我需要找出指定的PPT是否运行的话就会出错。 For Each doc In Presentations 'ActievX部件不能创建对象 If doc.Name = File Then Found = True Next doc就不知道为什么会出现这样的情况,我用相同的办法解决了Word、Excel的问题,PowerPoint就不知道为什么不行的
winhwnd = FindWindow("PP11FrameClass", 0) If winhwnd = 0 Then Set pptApp = CreateObject("PowerPoint.Application") '创建POWERPOINT对象 pptApp.Visible = True '显示PowerPoint窗口 pptApp.Presentations.Open (app0 & "\PowerPoint\" & File) '打开PowerPoint文档 Else Set pptApp = CreateObject("PowerPoint.Application") For Each doc In pptApp.Presentations If doc.Name = File Then Found = True Next doc If Found = True Then MsgBox "文件" & File & "已打开", vbOKOnly, "提示" Else pptApp.Presentations.Open (app0 & "\PowerPoint\" & File) End If End If Exit Sub 问题终于解决了,原来在Office成员PowerPoint与其他两成员Word、Excel的运行机制不一样的。即使你找到已运行的PowerPoint应用程序,但仍需要Set pptApp = CreateObject("PowerPoint.Application")重新获得对象。同样是Office成员,怎么PowerPoint就那么不一样呢…
Option ExplicitPrivate Const NORMAL_PRIORITY_CLASS = &H20&
Private Const STARTF_USESTDHANDLES = &H100&
Private Const STARTF_USESHOWWINDOW = &H1Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End TypePrivate Type STARTUPINFO
cb As Long
lpReserved As Long
lpDesktop As Long
lpTitle As Long
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End TypePrivate Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End TypePrivate Declare Function CreatePipe Lib "kernel32" (phReadPipe As Long, phWritePipe As Long, lpPipeAttributes As SECURITY_ATTRIBUTES, ByVal nSize As Long) As Long
Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, ByVal lpBuffer As String, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongPublic Function retunCmdResult(strCommand As String) As String
Dim Proc As PROCESS_INFORMATION '进程信息
Dim Start As STARTUPINFO '启动信息
Dim SecAttr As SECURITY_ATTRIBUTES '安全属性
Dim hReadPipe As Long '读取管道句柄
Dim hWritePipe As Long '写入管道句柄
Dim lngBytesRead As Long '读出数据的字节数
Dim strBuffer As String * 256 '读取管道的字符串buffer
Dim Command As String 'DOS命令
Dim ret As Long 'API函数返回值
Dim lpOutputs As String '读出的最终结果
'设置安全属性
With SecAttr
.nLength = LenB(SecAttr)
.bInheritHandle = True
.lpSecurityDescriptor = 0
End With
'创建管道
ret = CreatePipe(hReadPipe, hWritePipe, SecAttr, 0)
If ret = 0 Then
MsgBox "无法创建管道", vbExclamation, "错误"
Exit Function
End If
'设置进程启动前的信息
With Start
.cb = LenB(Start)
.dwFlags = STARTF_USESHOWWINDOW Or STARTF_USESTDHANDLES
.hStdOutput = hWritePipe '设置输出管道
.hStdError = hWritePipe '设置错误管道
End With
'启动进程
Command = strCommand 'DOS进程以ipconfig.exe为例
ret = CreateProcess(vbNullString, Command, SecAttr, SecAttr, True, NORMAL_PRIORITY_CLASS, ByVal 0, vbNullString, Start, Proc)
If ret = 0 Then
MsgBox "无法启动新进程", vbExclamation, "错误"
ret = CloseHandle(hWritePipe)
ret = CloseHandle(hReadPipe)
Exit Function
End If
'因为无需写入数据,所以先关闭写入管道。而且这里必须关闭此管道,否则将无法读取数据
ret = CloseHandle(hWritePipe)
'从输出管道读取数据,每次最多读取256字节
Do
ret = ReadFile(hReadPipe, strBuffer, 256, lngBytesRead, ByVal 0)
lpOutputs = lpOutputs & Left(strBuffer, lngBytesRead)
DoEvents
Loop While (ret <> 0) '当ret=0时说明ReadFile执行失败,已经没有数据可读了
'读取操作完成,关闭各句柄
ret = CloseHandle(Proc.hProcess)
ret = CloseHandle(Proc.hThread)
ret = CloseHandle(hReadPipe)
retunCmdResult = lpOutputs
End FunctionPrivate Sub Command1_Click()
Text1.Text = retunCmdResult("tasklist")
If InStr(1, Text1.Text, "POWERPNT.EXE", vbTextCompare) <> 0 Then
Debug.Print "PPT 运行!"
Else
Debug.Print "PPT 没有运行!"
End If
End Sub
它要判断的是ppt文件,如果同时运行多个ppt文件那么POWERPNT.EXE始终是存在的,这个检测命令参数应该可以,但是打开多个ppt的话就无效了,因为不管打开多少个ppt文件最终进程只有一个,而所带的参数就是第一个ppt文件的路径。楼主用ppt的com接口提供的方法应该是可以的,可以在doc.Name所在多行设置断点,看看它的值的变化,确认它记录的是文件名还是文件路径。
我觉得调用API的FindWindow函数更简单…
我的是可以找到一个PPT文件的,但是如果有多个运行PPT的话,我需要找出指定的PPT是否运行的话就会出错。
For Each doc In Presentations 'ActievX部件不能创建对象
If doc.Name = File Then Found = True
Next doc就不知道为什么会出现这样的情况,我用相同的办法解决了Word、Excel的问题,PowerPoint就不知道为什么不行的
winhwnd = FindWindow("PP11FrameClass", 0)
If winhwnd = 0 Then
Set pptApp = CreateObject("PowerPoint.Application") '创建POWERPOINT对象
pptApp.Visible = True '显示PowerPoint窗口
pptApp.Presentations.Open (app0 & "\PowerPoint\" & File) '打开PowerPoint文档
Else
Set pptApp = CreateObject("PowerPoint.Application")
For Each doc In pptApp.Presentations
If doc.Name = File Then Found = True
Next doc
If Found = True Then
MsgBox "文件" & File & "已打开", vbOKOnly, "提示"
Else
pptApp.Presentations.Open (app0 & "\PowerPoint\" & File)
End If
End If
Exit Sub
问题终于解决了,原来在Office成员PowerPoint与其他两成员Word、Excel的运行机制不一样的。即使你找到已运行的PowerPoint应用程序,但仍需要Set pptApp = CreateObject("PowerPoint.Application")重新获得对象。同样是Office成员,怎么PowerPoint就那么不一样呢…