Jump to content
kcvinu

How to get WindowTitle from a COM object

Recommended Posts

kcvinu

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. 


My Contributions

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)

 

Share this post


Link to post
Share on other sites
water

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 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
kcvinu

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

 


My Contributions

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)

 

Share this post


Link to post
Share on other sites
water

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

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

 

  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
kcvinu

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


My Contributions

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)

 

Share this post


Link to post
Share on other sites
water

I know ;)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
kcvinu

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

My Contributions

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)

 

Share this post


Link to post
Share on other sites
water

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 :)

  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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

  • Similar Content

    • lavascript
      By lavascript
      I have a Word document containing a 9-column table where row 1 is the column headers. My goal is to read the table into a 2d array, remove some rows, update some fields, and add a few rows to the end. The resulting array will likely be a different length. Next, I want to write the data back into the table. If it's easier, I can write the data to a new document from a template containing the same table header with a blank 2nd row.
      Here's my early attempt:
      Local $oWord = _Word_Create() Local $oDoc = _Word_DocOpen($oWord, $sFile) Local $aData = _Word_DocTableRead($oDoc, 1) $aData[3][5] = "Something else" Local $oRange = _Word_DocRangeSet($oDoc, 0) $oRange = _Word_DocRangeSet($oDoc, $oRange, $wdCell, 9) _Word_DocTableWrite($oRange,$aData) This, unfortunately, writes the entire array into the first cell of row 2. What am I doing wrong?
       
    • Bilgus
      By Bilgus
      ; NetFirewallPolicy2 COM UDF Library for AutoIt3
      ; AutoIt Version : 3.3.14.5
      ; Description ...: Windows Firewall Policy2 Interface, Provides access to the firewall policy for Windows Vista+
      Including Test Script 
      _NetFw_Get_CurrentProfileTypes                           Retrieves the currently active firewall profile(s) _NetFw_Get_FirewallEnabled                               Indicates whether a firewall is enabled locally _NetFw_Put_FirewallEnabled                               Specifies whether a firewall is enabled locally _NetFw_Get_ExcludedInterfaces                            Indicates a list of interfaces on which firewall settings are excluded _NetFw_Put_ExcludedInterfaces                            Specifies a list of interfaces on which firewall settings are excluded _NetFw_Get_BlockAllInboundTraffic                        Indicates whether the firewall should not allow inbound traffic _NetFw_Put_BlockAllInboundTraffic                        Specifies whether the firewall should not allow inbound traffic _NetFw_Get_NotificationsDisabled                         Indicates whether interactive firewall notifications are disabled _NetFw_Put_NotificationsDisabled                         Specifies whether interactive firewall notifications are disabled _NetFw_Get_UnicastResponsesToMulticastBroadcastDisabled  Indicates whether the firewall should not allow unicast responses to multicast and broadcast traffic _NetFw_Put_UnicastResponsesToMulticastBroadcastDisabled  Specifies whether the firewall should not allow unicast responses to multicast and broadcast traffic _NetFw_Get_Rules                                         Retrieves the interface to collection of firewall rules _NetFw_Get_ServiceRestriction                            Retrieves the interface used to access the Windows Service Hardening store _NetFw_EnableRuleGroup                                   Enables or disables a specified group of firewall rules _NetFw_IsRuleGroupEnabled                                Determines whether a specified group of firewall rules are enabled or disabled for the current profile _NetFw_RestoreLocalFirewallDefaults                      Restores the local firewall configuration to its default state _NetFw_Get_DefaultInboundAction                          Indicates the default action for inbound traffic _NetFw_Put_DefaultInboundAction                          Specifies the default action for inbound traffic _NetFw_Get_DefaultOutboundAction                         Indicates the default action for outbound traffic _NetFw_Put_DefaultOutboundAction                         Specifies the default action for outbound traffic _NetFw_Get_IsRuleGroupCurrentlyEnabled                   Determines whether a specified group of firewall rules are enabled or disabled for the current profile _NetFw_Get_LocalPolicyModifyState                        Determines if adding or setting a rule or group of rules will take effect in the current firewall profile  
      UDF:
      Test Script:
       
    • Subz
      By Subz
      Backstory:
      Our Microsoft Office Templates shared folder was changed from a DFS share to an Isilon share. example:
      Old Server: \\Domain.com\Office\Templates
      New Server: \\Templates.domain.com\Office\Templates
      The team making the changes overlooked that several hundred thousand documents, had been attached to the old template documents.  So when you open a document which has been attached, it will take a couple of minutes to open, while it tries to locate the old server path.  I've been asked to come in and fix it, so after several hours found that the data is being held in document.zip\word\_rels\settings.xml.rels, I now need to replace the old server path with the new server path.  I didn't want to use dom as that would take too long and found a tool wtc https://github.com/NeosIT/wtc which  works perfectly, takes about 8 minutes to scan a single directory with 4000 documents and fix them.  The problem is the documents are all held on sharepoint and they want to retain the file timestamp, which is easy enough, but they also don't want to keep the "Modified By" apparently they don't like seeing all the documents appearing as "Modified by: Subz"  Anyone know of way to retain the "Modified By" info,
    • FrancescoDiMuro
      By FrancescoDiMuro
      Good evening everyone
      I am working with Word UDF ( thanks @water! ), and, especially, with the function _Word_DocFindReplace().
      The replace does work everywhere in the document, but, it does not work in Headers or Footers.
      Am I missing something or am I forced to use the code below?
      I have already looked in the Help file ( about _Word_DocFindReplace() ), but there are no mentions about replace text in Headers/Footers.
      Sub FindAndReplaceFirstStoryOfEachType() Dim rngStory As Range For Each rngStory In ActiveDocument.StoryRanges With rngStory.Find .Text = "find text" .Replacement.Text = "I'm found .Wrap = wdFindContinue .Execute Replace:=wdReplaceAll End With Next rngStory End Sub Thanks everyone in advance


      Best Regards.
    • Bilgus
      By Bilgus
      I was Playing around With AutoIt this evening and wondered how hard it would be to get typeinfo like the COM Viewers do only using AutoIt
      Turns out it was pretty easy.
      A Few Notes:
      CAarray info is unfinished I didn't have any objects to test it on so I left it Limited.
      The Object must have IDispatch exposed (ITypeInfo is derivative)
      Its Just a proof of concept Run with it but don't carry scissors
      ITypeInfoCOM.au3
      ITypeInfoTest.au3
       
      Output IWebBrowserApp
       
      Output ObjCreate(MediaPlayer.MediaPlayer.1)
       
×