Jump to content

How to get WindowTitle from a COM object


kcvinu
 Share

Recommended Posts

Hi all,

I have an object from "ObjGet("Word.Application")". Can i get the word document's window title with this object. Thanks in advance. 

PS. Searched a lot in MSDN page for Application Members of word object. 

Spoiler

My Contributions

Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language.

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Link to comment
Share on other sites

The old _WordAttach (Autoit 3.3.8.1) function might give you an idea:

; #FUNCTION# ====================================================================================================================
; Name...........: _WordAttach
; Description ...: Attach to the first existing instance of Microsoft Word where the
;                  search string matches based on the selected mode.
; Parameters ....: $s_string    - String to search for
;                  $s_mode      - Optional: specifies search mode
;                                   FileName    = Name of the open document
;                                   FilePath    = (Default) Full path to the open document
;                                   HWND        = hwnd of the word window
;                                   Text        = Text from the body of the document
;                                   Title       = Title of the word window
; Return values .: On Success   - Returns an object variable pointing to the Word.Application object
;                  On Failure   - Returns 0 and sets @ERROR
;                   @ERROR      - 0 ($_WordStatus_Success) = No Error
;                               - 1 ($_WordStatus_GeneralError) = General Error
;                               - 5 ($_WordStatus_InvalidValue) = Invalid Value
;                               - 7 ($_WordStatus_NoMatch) = No Match
;                   @Extended   - Contains invalid parameter number
; Author ........: Bob Anthony
; ===============================================================================================================================
Func _WordAttach($s_string, $s_mode = "FilePath")
    $s_mode = StringLower($s_mode)

    Local $o_windows, $return, _
            $i_Extended, $s_ErrorMSG = "", $i_ErrorStatusCode = $_WordStatus_Success

    ; Setup internal error handler to Trap COM errors, turn off error notification
    Local $status = __WordInternalErrorHandlerRegister()
    If Not $status Then __WordErrorNotify("Warning", "_WordAttach", _
            "Cannot register internal error handler, cannot trap COM errors", _
            "Use _WordErrorHandlerRegister() to register a user error handler")
    Local $f_NotifyStatus = _WordErrorNotify() ; save current error notify status
    _WordErrorNotify(False)

    Local $o_Result = ObjGet("", "Word.Application")
    If @error = $_WordStatus_ComError And $WordComErrorNumber = -2147221021 And $WordComErrorWinDescription = "Operation unavailable" Then
        $i_ErrorStatusCode = $_WordStatus_NoMatch
    EndIf

    If $i_ErrorStatusCode = $_WordStatus_Success Then
        $o_windows = $o_Result.Application.Windows
        If Not IsObj($o_windows) Then
            $i_ErrorStatusCode = $_WordStatus_NoMatch
        EndIf
    EndIf

    If $i_ErrorStatusCode = $_WordStatus_Success Then
        For $o_window In $o_windows

            Switch $s_mode
                Case "filename"
                    If $o_window.Document.Name = $s_string Then
                        $i_ErrorStatusCode = $_WordStatus_Success
                        $o_window.Activate()
                        $return = $o_window.Application
                    EndIf
                Case "filepath"
                    If $o_window.Document.FullName = $s_string Then
                        $i_ErrorStatusCode = $_WordStatus_Success
                        $o_window.Activate()
                        $return = $o_window.Application
                    EndIf
                Case "hwnd"
                    Local $h_hwnd = __WordGetHWND($o_window)
                    If IsHWnd($h_hwnd) Then
                        If $h_hwnd = $s_string Then
                            $i_ErrorStatusCode = $_WordStatus_Success
                            $o_window.Activate()
                            $return = $o_window.Application
                        EndIf
                    EndIf
                Case "text"
                    If StringInStr($o_window.Document.Range().Text, $s_string) Then
                        $i_ErrorStatusCode = $_WordStatus_Success
                        $o_window.Activate()
                        $return = $o_window.Application
                    EndIf
                Case "title"
                    If ($o_window.Caption & " - " & $o_window.Application.Caption) = $s_string Then
                        $i_ErrorStatusCode = $_WordStatus_Success
                        $o_window.Activate()
                        $return = $o_window.Application
                    EndIf
                Case Else
                    ; Invalid Mode
                    $i_ErrorStatusCode = $_WordStatus_InvalidValue
                    $s_ErrorMSG = "Invalid Mode Specified"
                    $i_Extended = 2
            EndSwitch
        Next
        If Not IsObj($return) Then
            $i_ErrorStatusCode = $_WordStatus_NoMatch
        EndIf
    EndIf

    ; restore error notify and error handler status
    _WordErrorNotify($f_NotifyStatus) ; restore notification status
    __WordInternalErrorHandlerDeRegister()

    Switch $i_ErrorStatusCode
        Case $_WordStatus_Success
            Return SetError($_WordStatus_Success, 0, $return)
        Case $_WordStatus_NoMatch
            __WordErrorNotify("Warning", "_WordAttach", "$_WordStatus_NoMatch")
            Return SetError($_WordStatus_NoMatch, 0, 0)
        Case $_WordStatus_InvalidValue
            __WordErrorNotify("Error", "_WordAttach", "$_WordStatus_InvalidValue", $s_ErrorMSG)
            Return SetError($_WordStatus_InvalidValue, $i_Extended, 0)
        Case Else
            __WordErrorNotify("Error", "_WordAttach", "$_WordStatus_GeneralError", "Invalid Error Status - Notify Word.au3 developer")
            Return SetError($_WordStatus_GeneralError, 0, 0)
    EndSwitch
EndFunc   ;==>_WordAttach

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

@water , Thank you. Infact, i found it in my own. "$WordObject.ActiveDocument.Name" is what i want. :)

 

Spoiler

My Contributions

Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language.

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Link to comment
Share on other sites

Yes, but the window title is a bit different. It is

$WordObject.ActiveDocument.Name & " - Microsoft Word"

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

@water , yes. but i just need file name only. Anyway, i am playing with the methods and properties of Word object. It's interesting. 

Spoiler

My Contributions

Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language.

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Link to comment
Share on other sites

I know ;)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I know what do you mean by the "I know". You did the re-writing of the Word.au3 :) Thanks for such a nice UDF

 

Edited by kcvinu
Spoiler

My Contributions

Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language.

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Link to comment
Share on other sites

When getting your feet wet with the Word Developer Reference you find a lot of useful information. Sometimes it gets too much so you do not know where to start and where to end.
Happy reading :)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...