How to save an ActiveDocument descriptor in a variable?

How to get a Windows ActiveDocument handle in Microsoft Word?

I want to save the value of a variable descriptor.

+3
source share
2 answers

You can use the FindWindow function to get the window handle in the current active text application:

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

The class name for MS Word is "OpusApp". If you have only one Word application open, the following returns to this window:

Public Function Test1()
Dim lhWnd As Long
lhWnd = FindWindow("OpusApp", vbNullString)
End Function

Note that if you have multiple active Word applications, you can add a title for the window title. For example, if you save it as "Stanigator.doc", the title will be considered "Stanigator - Microsoft Word". So:

Public Function Test2()
Dim lhWnd As Long
lhWnd = FindWindow(vbNullString, "Stanigator - Microsoft Word")
End Function

Another API that may come in handy is:

Private Declare Function GetActiveWindow Lib "user32" () As Long

Edit:

VSTO, , : Connect Issue: VSTO API MS Word

+3

, Set myDoc = ActiveDocument.

0

All Articles