作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何在 Microsoft Word 中获取 ActiveDocument 的 Windows 句柄?
我希望将句柄值保存到一个变量中。
最佳答案
您可以使用 FindWindow 函数获取当前事件的 Word 应用程序中的窗口句柄:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
MS Word 的类名是“OpusApp”。如果您只打开了一个 Word 应用程序,则以下命令将返回该窗口的句柄:
Public Function Test1()
Dim lhWnd As Long
lhWnd = FindWindow("OpusApp", vbNullString)
End Function
请注意,当您有多个事件的 Word 应用程序时,您可以添加窗口标题的名称。例如,如果它保存为“Stanigator.doc”,那么标题将显示为“Stanigator - Microsoft Word”。所以:
Public Function Test2()
Dim lhWnd As Long
lhWnd = FindWindow(vbNullString, "Stanigator - Microsoft Word")
End Function
另一个可能派上用场的 API 是:
Private Declare Function GetActiveWindow Lib "user32" () As Long
编辑:
我打算推荐 VSTO 方法,但基于此链接似乎并不明智: Connect Issue: VSTO API support for MS Word Main Window Handle
关于vba - 如何将 ActiveDocument 的句柄保存到变量中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10159405/
我是一名优秀的程序员,十分优秀!