如何知道WORD是否已经打开了

sunfw 2003-09-29 04:40:01
还有一个问题就是我录了一段宏在VB里面
当WORD应用程序是可见的时候很容易就通过了,当不可见的时候老是出错.
为什么会这样呢
有没有办法解决啊
...全文
89 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Micro_Sheng 2003-09-29
  • 打赏
  • 举报
回复
这个很简单!
判断Word是否打开的方法如下:
1、先编写一个函数查找桌面上的所有窗口标题!
2、然后在查得的标题中查找包含“- Microsoft Word”的标题,如果有说明Word已打开,否则没有打开!(这是因为任何一个Word文档的标题都包含 - Micorsoft Word)

函数如下:
'声明
Public Const GW_HWNDFIRST = 0 '子窗口的第一兄弟窗口,其第一个顶层窗口
Public Const GW_HWNDLAST = 1 '子窗口的最后一个兄弟窗口,或最后一个顶层窗口
Public Const GW_HWNDNEXT = 2 '后继窗口
Public Const GW_HWNDPREV = 3 '先前窗口
Public Const GW_OWNER = 4 '窗口拥有者

Public Declare Function GetWindow Lib "user32" (ByVal HWnd As Long, ByVal wCmd As Long) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal HWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal HWnd As Long) As Long

'函数
Public Sub FindTitle()
'查找桌面上的所有窗口标题
On Error GoTo ErrHandle:
Dim currwnd As Double
Dim ListItem As String

Dim buf As String
Dim buflen As Long
Dim child_hwnd As Long
Dim ret As Long

currwnd = GetWindow(HWnd, GW_HWNDFIRST)

While currwnd <> 0
Length = GetWindowTextLength(currwnd)
ListItem$ = Space$(Length + 1)
Length = GetWindowText(currwnd, ListItem$, Length + 1)

if instr(ListItem,"- Microsoft Word")<>0 then '或者用 if right(ListItem,16)="- Microsoft Word") then
msgbox"Microsoft Word已打开!"
else
msgbox"Microsoft Word没有打开!"
end if

currwnd = GetWindow(currwnd, GW_HWNDNEXT)
Wend

ErrHandle:
Msgbos "程序发生异常!"
Exit Sub

End Sub

把以上的代码放入一个单独的模块中!在窗体中调用过程 FindTitle 即可!
以上代码绝对可行,如还有问题请与我联系: zxs_001@163.net

最后别忘了结贴时给我加分哦!!~_~
sunfw 2003-09-29
  • 打赏
  • 举报
回复
汗~~~```
还是没有人来
sunfw 2003-09-29
  • 打赏
  • 举报
回复
自己UP一下

7,759

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧