Forum Discussion

vidya_sathyanar's avatar
10 years ago

Unable to find object Form("Word")

Hi I am very new to Testcomplete. I recorded a script using jscript to open a word doc and type some text in, save the file to desktop and close it. This works fine.



I wanted to achieve, editing the recorded script, creating 5 word files with incremental names e.g. xyz0, xyz1...xyz4. So I added a for loop and changed the file name to accept a numerical at the end.



When I run the test, sometimes the script has managed to create 2 files xyz0, xyz1. But sometimes stops after 1 file. The error I get is: Unable to find the object Form("Word").



OS: Windows 8.1



The script is using coordinates to click, that is a different issue I have to solve.



Below is the edited script, any help is greatly appreciated, thanks!



function Test2()


{



var winword;



var form;



var netUIHWND;



var dlgSaveAs;



for (var i = 0; i < 5; i++)



{



 



 



Sys.Process("explorer").Window("Shell_TrayWnd").Window("ReBarWindow32").Window("MSTaskSwWClass", "Running applications").Window("MSTaskListWClass", "Running applications").Click(214, 27);



winword = Sys.Process("WINWORD");



form = winword.Form("Word");



form.Window("FullpageUIHost").Window("NetUIHWND").Click(548, 228);



form.Window("_WwF").Panel("Document1").Panel("Microsoft Word Document").Keys("Today is Thursday.");



form.Panel("MsoDockTop").ToolBar("Ribbon").Window("MsoWorkPane", "Ribbon").Window("NUIPane").Window("NetUIHWND").Click(38, 35);



netUIHWND = form.Window("FullpageUIHost").Window("NetUIHWND");



netUIHWND.Click(40, 201);



netUIHWND.Click(286, 283);



netUIHWND.Click(536, 210);



dlgSaveAs = winword.Window("#32770", "Save As");



dlgSaveAs.Window("DUIViewWndClassName").UIAObject("Explorer_Pane").Window("FloatNotifySink", "", 1).Window("ComboBox").SetText("xyz".concat(i));



dlgSaveAs.Window("Button", "&Save").ClickButton();



winword.Window("#32770", "Microsoft Word").Window("Button", "&Yes").ClickButton();



form.Close();



}



}


1 Reply

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Word automation is so much easier when using the Word COM object, Word.Application.



    For your task, something like this should do the trick:



    function Create5WordFiles()

    {

      // 1. Get the Desktop folder path

      var oShell = Sys.OleObject("WScript.Shell");

      var strDesktop = aqFileSystem.IncludeTrailingBackSlash(oShell.SpecialFolders("Desktop"));



      // 2. Create 5 Word files on the desktop

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

      var oDoc, strFileName;

      for (var i = 0; i < 5; i++)

      {

        // Create a blank document

        oDoc = oWord.Documents.Add();



        // Enter text into the document

        oDoc.Range(0, 0).Text = "Today is Thursday.";



        // Save the file to Desktop

        strFileName = strDesktop + "xyz" + i + ".docx";

        oDoc.SaveAs(strFileName);



        oDoc.Close();

      }

      oWord.Quit();

    }