Tip, October 2005

A Script Button to Open Linked Files

Introduction

This tip shows you how to add a Script Button to a DB/TextWorks or CS/TextWorks report to open files named in one of the textbase fields. The Windows registry is used to select the application to run – e.g. files with extension .DOC will be opened in MS Word, .TXT in Notepad, etc.

Adding a textbase field

Open your textbase and click Maintain>Edit Textbase Structure>Edit Fields to add a field to your textbase structure – e.g. File Name. In one or more records, enter the name of a file associated with that record. Be sure to enter the complete path and filename – e.g. C:\docs\letter.doc.

Using the form designer

Click Display>Design Form to open the form designer either to create a new form or to amend an existing form. Add any boxes you require to display fields from the record. Then add one box containing just the new File Name field.

Add a Button to the report by clicking on Edit>Add>Script Button. In the Box Properties dialogue, change the Caption on the Button to something like Open File.

Adding the script

With the form open in the form designer, click Tools>Form Script.

In the Script window, click on the Names tab.

In the list of Boxes in the form, highlight the box containing the File Name field. In the Box Name line, type a name (one word only) such as filename, then click Change. The name of the box will then appear in the Names column.

Next, place the cursor over the line corresponding to the Script Button and type a name – e.g. openfile.

Click Change.

You should now have something like the following:

You now need to enter the actual script coding. You have two choices: JScript and VBScript. These two script types use slightly different conventions, as follows:

Alternative A: Using JScript

Click on the Script tab. Select JScript (not VBScript) and type the following text in the Script Window. Take care to include all the punctuation.

function openfile_onClick()
{
var file = Form.boxes("filename").content;

     if (file == "")
        Application.message("No documents to open");

     else
        Application.shellExecute(file, "");

}

Alternative B: Using VBScript

Click on the Script tab. Select VBScript (not JScript) and type the following text in the Script Window. Take care to include all the punctuation.

sub openfile_onclick()

file = Form.boxes("filename").content

     if file="" then
        application.message("No documents to open")

     else
        set n=Application.shellExecute(file, "")
     end if

end sub

Testing the form script

Click OK, Save your form, then Close the form designer and test the form as follows:

Perform a search that will retrieve one or more of the records to which you added file names earlier.

View the search results in a Report window.

Use the Select Form For This Window icon to select the form that you have just designed. Your report will look something like this:

Click on one of the buttons that read Open File.

If the script is working, it should open the relevant file in its native application (for example, Word for Windows).

Note

Please note that the script shown above will only work with a single file entered in the File Name field. The script will need considerable modification if several filenames need to be included.



Maxus Australia Pty Ltd - Contact us for any further information or other tips.