OpenFile Method

Applies to TestComplete 15.0, last modified on November 17, 2021

Description

The method is applied to window objects that correspond to standard Open File dialogs. It simulates the selection of the specified file in the dialog and then closes the dialog.

Declaration

TestObj.OpenFile(FileName, FileType)

TestObj A variable, parameter or expression that specifies a reference to one of the objects listed in the Applies To section
FileName [in]    Required    String    
FileType [in]    Optional    Variant Default value: -1   
Result None

Applies To

A window object that corresponds to a standard Open File dialog box:

View Mode

This method is available in the Object Browser panel and in other panels and dialogs in both Basic and Advanced view modes.

Parameters

The method has the following parameters:

FileName

The fully-qualified name of the file to be selected.

FileType

A string or an integer value that specifies the type of the file to be opened. This parameter corresponds to the value that is shown in the Files of type box of the standard Open File dialog.

If FileType is specified, the method sets the appropriate value in this box before opening the file. If FileType is omitted or is equal to -1, the method does not change the value of this box.

If FileType is a string, the method treats it as an item’s text that is shown in the drop-down list of the “Files of type” box. If FileType is an integer, the method treats it as the index of the desired item (indexes are zero-based). If the specified item was not found, the method posts an error message to the test log and does not open a file.

Result Value

None.

Remarks

TestComplete may fail to record the OpenFile method on Windows 7 and later; TestComplete will record interactions with the Open File dialog elements instead. However, you can modify the recorded test and manually replace them with the OpenFile method call.

Example

The following example demonstrates how to use the OpenFile method in scripts:

JavaScript

function WorkWithFiles()
{
  var notepad = Sys.Process("notepad");
  var wndNotepad = notepad.Window("Notepad");

  // Open a file in Notepad
  wndNotepad.MainMenu.Click("File|Open...");
  notepad.Window("#32770", "Open").OpenFile("C:\\MyFile.txt");

  // Save the file to the desktop
  var strDesktop = WshShell.SpecialFolders.Item("Desktop");
  wndNotepad.MainMenu.Click("File|Save as...");
  notepad.Window("#32770", "Save As").SaveFile(strDesktop + "\\MyFile.txt");
}

JScript

function WorkWithFiles()
{
  var notepad = Sys.Process("notepad");
  var wndNotepad = notepad.Window("Notepad");

  // Open a file in Notepad
  wndNotepad.MainMenu.Click("File|Open...");
  notepad.Window("#32770", "Open").OpenFile("C:\\MyFile.txt");

  // Save the file to the desktop
  var strDesktop = WshShell.SpecialFolders("Desktop");
  wndNotepad.MainMenu.Click("File|Save as...");
  notepad.Window("#32770", "Save As").SaveFile(strDesktop + "\\MyFile.txt");
}

Python

def WorkWithFiles():
  notepad = Sys.Process("notepad")
  wndNotepad = notepad.Window("Notepad")

  # Open a file in Notepad
  wndNotepad.MainMenu.Click("File|Open...")
  notepad.Window("#32770", "Open").OpenFile("C:\\MyFile.txt")

  # Save the file to the desktop
  strDesktop = WshShell.SpecialFolders.Item("Desktop")
  wndNotepad.MainMenu.Click("File|Save as...")
  notepad.Window("#32770", "Save As").SaveFile(strDesktop + "\\MyFile.txt")

VBScript

Sub WorkWithFiles
  Dim notepad, wndNotepad, strDesktop

  Set notepad = Sys.Process("notepad")
  Set wndNotepad = notepad.Window("Notepad")

  ' Open a file in Notepad
  wndNotepad.MainMenu.Click("File|Open...")
  notepad.Window("#32770", "Open").OpenFile "C:\MyFile.txt"

  ' Save the file to the desktop
  strDesktop = WshShell.SpecialFolders("Desktop")
  wndNotepad.MainMenu.Click "File|Save as..."
  notepad.Window("#32770", "Save As").SaveFile strDesktop & "\MyFile.txt"
End Sub

DelphiScript

procedure WorkWithFiles;
var notepad, wndNotepad, strDesktop;
begin
  notepad := Sys.Process('notepad');
  wndNotepad := notepad.Window('Notepad');

  // Open a file in Notepad
  wndNotepad.MainMenu.Click('File|Open...');
  notepad.Window('#32770', 'Open').OpenFile('C:\MyFile.txt');
 
  // Save the file to the desktop
  strDesktop := WshShell.SpecialFolders('Desktop');
  wndNotepad.MainMenu.Click('File|Save as...');
  notepad.Window('#32770', 'Save As').SaveFile(strDesktop + '\MyFile.txt');
end;

C++Script, C#Script

function WorkWithFiles()
{
  var notepad = Sys["Process"]("notepad");
  var wndNotepad = notepad["Window"]("Notepad");

  // Open a file in Notepad
  wndNotepad["MainMenu"]["Click"]("File|Open...");
  notepad["Window"]("#32770", "Open")["OpenFile"]("C:\\MyFile.txt");

  // Save the file to the desktop
  var strDesktop = WshShell["SpecialFolders"]("Desktop");
  wndNotepad["MainMenu"]["Click"]("File|Save as...");
  notepad["Window"]("#32770", "Save As")["SaveFile"](strDesktop + "\\MyFile.txt");
}

See Also

SaveFile Method
Working With Open File and Save File Dialogs

Highlight search results