Assign the Title & the fulname path of the top Word window to vars

M

Marceepoo

I've been trying to figure out how to assign the Title (e.g. "Title$" and the
fulname path (e.g., "Fulname") of the top Word window to variables. I saw
the following text (which I think was) intended for use in a VB script. (It
wouldn't work as a macro "as is" in my Word 2003.)
I wonder if anyone has any tips on how to modify it to make it work in Word
2003 VBA, and where (URL) I could learn about the intersection between vba
and "API" (whatever "API" is). Thanks, Marceepo
Here's the code....

'Win32 API Example: vba Macro to get Active Window Title
'
' THIS text is from page 42 of Scripting for Dragon NaturallySpeaking8 by
Larry V. Allen.
'
'One API function gets the title of the active window. This is needed in
applications
' where commands depend on context. Microsoft has books and web resources
describing
' these API functions in depth. Some functions are dependent upon the
version of Windows.


'declare three Windows functions
Declare Function GetForegroundWindow& Lib "user32" ()
'Declare Function GetWindowTextLengthA& Lib "user32" (ByVal'hwnd&)
Declare Sub GetWindowTextA Lib "user32" _
(ByVal hwnd&, ByVal lpsz$, ByVal cbMax&)


'use Windows functions to obtain a string with the
'title of the current window. The string is returned
'as the value of the ActiveWindowTitle function.


Function ActiveWindowTitle$()


Dim ActiveWindow As Long
Dim TitleLen As Long
Dim Title As String


ActiveWindow = GetForeqroundWindow()
TitleLen = GetWindowTextLengthA(ActiveWindow)
Title$ = Space$(TitleLen)


GetWindowTextA ActiveWindow, Title$, TitleLen + l
ActiveWindowTitle$ = Title$
End Function


'Use the window title -in this case, display it.
'More commonly if would be compared .


Sub Main()
MsgBox ActiveWindowTitle$
End Sub
 
J

Jonathan West

You don't need all this Windows API stuff in Word VBA for this purpose.

The filename can be read from ActiveDocument.Name

The full pathname can be read from ActiveDocument.FullName


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top