Visual Studio Tools for Office: Using Visual Basic 2005 with Excel, Word, Outlook, and InfoPath
Before you can use VSTO to put code behind InfoPath forms, you must ensure that the following things are installed on your development machine:
The toolkit must be installed last because it has an explicit dependency on both Visual Studio and InfoPath. InfoPath Service Pack 1 contains the primary interop assemblies (PIAs) for InfoPath so that managed code can automate the InfoPath object model. Service Pack 1 also added the OnSave event, improved support for some offline scenarios, and digital signature support. You will read more about the details later in this chapter. After you have the toolkit installed, open Visual Studio, and choose New Project from the File Menu. Open the Visual Basic node in the tree view; choose Office; and then choose the InfoPath Form Template project, as shown in Figure 12.1. Figure 12.1. Creating an InfoPath project in Visual Studio.
Note The location is the location on the development machine, not the final location from which the published form will be accessed by your users.
After you click OK, the Microsoft Office Project Wizard asks whether you want to create a new form or open a form that you have already created with InfoPath, as shown in Figure 12.2. Figure 12.2. Creating the project based on a blank or existing form.
To create a new, blank form, keep the default selection of Create New Form Template, and click Finish. To create a new form template based on an XML data file, XML schema, database connection, or Web service, create the form template in InfoPath first without using Visual Studio and then open it as an existing form template. After you have a form template created in InfoPath, you can select the Open Existing Form Template option and browse to the form template (.xsn) or form template definition (.xsf) file. Note JScript (.js) or VBScript (.vbs) files associated with an existing form template will also be imported, but the script itself will be nonfunctional. Other scripts, such as scripts used for custom task-pane extensibility, are not affected. If you select a form template that already has managed code, the associated code is not included in the import; rather, you should open the Visual Studio project associated with the form template. After you have created your project, you can use Visual Studio as your code editor at the same time as you design the form using the InfoPath designer window, as shown in Figure 12.3. While developing for InfoPath, you will switch frequently between the InfoPath designer window and the Visual Studio window. In addition, when you press F5 in your project, InfoPath starts another InfoPath window, called a "preview" window, to preview what your form would look like at runtime. This makes for three top-level windows you might be juggling at any time. When you close the InfoPath preview window, Visual Studio stops debugging the project. If you close the InfoPath designer window accidentally, you can reopen it by choosing Open InfoPath from Visual Studio's Project menu. Figure 12.3. The Visual Studio window and the InfoPath designer window run as two separate windows.
Take a look at the Solution Explorer window on the right side of Visual Studio. Behind the scenes, a form template consists of many files working together. By default, when you're working with just InfoPath, the XML template, XML schemas, XSL views, and so on are hidden unless you choose Extract Form Files from the File menu and then explore that folder in Windows Explorer. When designing the code behind a form in Visual Studio, you can see and edit all files in the InfoPath project directly. The InfoPath designer, however, "locks" all the forms files. If you want to edit the files in Visual Studio manually, first close the InfoPath designer window to unlock the files. Visual Studio adds the following new commands to facilitate the development of code behind an InfoPath form:
These commands are available in the Visual Studio Project menu, the Tools menu, and in the context menu that displays when you right-click the InfoPath Project node in the Visual Studio Solution Explorer window. The InfoPath Project Properties dialog box, also accessible from the project's right-click menu, has two settings that prove useful when previewing your form, as shown in Figure 12.4. Figure 12.4. Setting InfoPath-specific project properties.
The first text box, called Preview Data File Path, takes a path to a custom XML file. Without a custom XML file, InfoPath previews with the default XML file used when filling out a new form. A custom XML file is useful for simulating what a user would experience if an existing form, saved from a previous editing session, were reopened. You can also use the Preview > With Data File command to achieve the same result. The second text box, called Preview User Role, sets a preview role. You can create forms that have different views depending on the role of the user filling out the form; an administrator might have a different view from an accountant. These roles are defined using InfoPath's User Roles command in the InfoPath Tools menu. You can also use the Preview > With User Role command to achieve the same result. |
Категории