Forum Discussion

vthomeschoolmom's avatar
vthomeschoolmom
Super Contributor
13 years ago
Solved

MS Word automation and getting to the objects

Does anyone have examples of the very BASICS of interacting with MS Word objects with TC? I would like to know how to get a handle on the Selection object of the current selection in the currently open document, for example. I am not that savvy with Word automation to begin with. Getting there though! Thanks



S
  • HKosova's avatar
    HKosova
    8 years ago

    dasabaja wrote:

    After implementing the line that opens the document I got a different error.

    TypeError
    WordDoc.Variables is not a function
    Error location:
    Unit: "HR\HR SmartTemplates\Script\Unit1"
    Line: 8 Column: 26.

    Any hints on what this might be?


    Change Variables("Group") to Variables.Item("Group"). The former is VBA/VBScript syntax, but in other languages you need to use the .Item() method to access collection items in MS Office COM objects.

19 Replies

  • irina_lukina's avatar
    irina_lukina
    Super Contributor

    Hi Lane,


    Try using the following code instead:



    Set objWordApplication = Sys.OleObject("Word.Application")

    Set objActiveWindow = objWordApplication.ActiveWindow

    objActiveWindow.Selection.GoTo What, , , "\para"


    The fact is that VBA doesn't require certain order of the parameters. You can specify parameters in any order using the Param_Name := "Param_Value" syntax. However, VBScript requires that all the parameters be specified in the order declared in the method's description. If you need to omit some parameters, you can just specify spaces for them and delimit the parameters using commas.


    I hope this information helps :)

  • irina_lukina's avatar
    irina_lukina
    Super Contributor

    Hi Lane,


    I recommend that you perform your validations via COM. All MS Office applications provide very convenient and deep access to their documents this way, so you should be able to check anything you want. To obtain a COM object, use the Sys.OleObject property (for example, to obtain MS Word, use word = Sys.OleObject("Word.Application")).


    To find out which objects, properties and methods you should use to work with the Microsoft Word application, see the http://msdn.microsoft.com/en-us/library/aa221371(v=office.11).aspx MSDN article.

  • I agree, COM Interface and OLE standards are the way to go.



    I have just been working on a OfficeWordCheckpoint that into parameter a word file stored in TestComplete Stores item.

    I planned to make it available within a Script extension but in the meantime, if you want to work with Word document contents, I strongly recommend you to take a look at the Interop API  :



    http://msdn.microsoft.com/en-us/library/ms254954.aspx



    If you are trying to acces text content, you will certainly need to use interfaces such as Document, ActiveDocument, Range, Paragraph, Words.

    Here is a very simple js example. Hope this help :



    var objWordApplication = Sys.OleObject("Word.Application");

    var WordDoc = objWordApplication.ActiveDocument;

    var ParagraphsContent = WordDoc.Paragraphs;

    var objCurrentParagraph = ParagraphsContent.First;

    var idxParagraph = 0;

    while(objCurrentParagraph != null) {

        Log.Message(objCurrentParagraph.Range.Text; )

        objCurrentParagraph = objCurrentParagraph.Next(); 

    }
  • How does one handle the vagaries of non-standard VBA synatx? I have something that in Word VBA would look like



    objActiveWindow.Selection.GoTo What:=wdGoToBookmark, Name:="\para"



    (where I would not have an objActiveWindow object of course. I could go directly to the selection.)



    But TC does not like the := syntax.



    Can someone advise?  Thanks
  • dasabaja's avatar
    dasabaja
    Occasional Contributor

    Hi all.

     

    Sorry for bringing this topic from the dead, but I'm having kinda the same problems like the original poster.

    There's an awesome answer on how to  manipulate document content, but what I'm interested in is reading variables from a .doc file added using VBA.

     

    I have a function that adds variables to a Word file

     

    Private Sub AddVariable()
            
         ThisDocument.Variables.Add "Group", "Group"
    
    End Sub

    So, I have a variable named Group that has a value Group. It's stupid, I know, but just as an example.

     

    I would like to use TC and read the value of the variable Group. The document is open at the same time as TC, it's in windows' processes.

     

    From what I read here (and oh similar topics), this is the function I've come up with in TC.

     

    function myFunc (){
    
    var objWordApplication = Sys.OleObject("Word.Application");
    
    objWordApplication.Visible = true;
    
    var WordDoc= objWordApplication.ActiveDocument;
    var myVariable = WordDoc.Variables("Group").Value;
    
    Log.Message(myVariable)
    
    }

    This doesn't work. When I type ā€žWordDoc.ā€œ , there is no intellisense after the dot. As if there is no Variables property.

     

    Also, the error i get is:

     

    JavaScript runtime error

     

    Cannot read property 'Variables' of undefined.

     

    So, I'm kinda stuck.

    I could really use some help!

    • tristaanogre's avatar
      tristaanogre
      Esteemed Contributor

      The error you're getting means that the WordDoc variable is not getting anything.  Not sure why the ActiveDocument call is not working.

       

      Try the following instead.  Make sure that Word is closed and not running and do the following:

       

      function myFunc (){
      
      var objWordApplication = Sys.OleObject("Word.Application");
      
      objWordApplication.Documents.Open(myDocumentPath);
      
      var WordDoc= objWordApplication.ActiveDocument;
      var myVariable = WordDoc.Variables("Group").Value;
      
      Log.Message(myVariable)
      
      }

      Replace the bold with the actual file path and name of the document.  See if that works.

       

      • dasabaja's avatar
        dasabaja
        Occasional Contributor

        Hi, tristaanogre.

         

        Sorry for the late reply, I wasn't in my office during the weekend.

         

        After implementing the line that opens the document I got a different error.

         

        TypeError
        WordDoc.Variables is not a function
        Error location:
        Unit: "HR\HR SmartTemplates\Script\Unit1"
        Line: 8 Column: 26.

        Any hints on what this might be?

      • dasabaja's avatar
        dasabaja
        Occasional Contributor

        Note with the enable visibility option (second one) doesn't work for me. I'v tried it earlier. I get the same error.

         

        As for the first one, I've tried implementing

        Sys.Process("Winword").Window("Shell_TrayWnd").Activate();

        I'm not sure what this Shell_TrayWnd is. This is the error I get after I insert the above line into my code.

         

        Cannot obtain the window with the window class 'Shell_TrayWnd', window caption '*' and index -1. See Additional Information for details. 9:31:30 Normal