Using the Microsoft Visual Studio Tools for the Microsoft Office System (Cont.) Creating the Web Service The first step is to create a Web service called ZipService with a WebMethod called GetZipInfo. To create a Web service, open the New Project dialog box in Visual Studio .NET. Select Create New ASP.NET Web Service and call it ZipService. Add the following code to your Web service: <WebMethod()> _ Public Function GetZipInfo(ByVal cZip As String) _ As String
If cZip = "98335" Then Return "Gig Harbor, WA" ElseIf cZip = "90210" Then Return "Beverly Hills, CA" Else Return "???" End If
End Function
Build the Web service by pressing F5. You can now return to your .NET/Office 2003 code. Interfacing with Controls The next step is to add code to interface with the controls you added to your Word 2003 document. This is a multi-step process: - Add reference properties to your class definition.
- Cast your reference properties to their respective MSForm object types.
- Add an event sink for the click event on the button.
Referencing and Importing the Microsoft Forms Library The first step to integrating with the Microsoft Forms 2.0 library is to add a reference to that library. To do this, right click on the References section of the Solutions Explorer and then choose References from the shortcut menu. Now select the COM tab, select the first Microsoft Forms 2.0 reference from the dialog box, and press the Select and OK buttons. You can now use this library in your applications. | " | One of the great things about Visual Studio .NET is its ability to create and consume Web services.
| " |
The next step is to add an Imports statement to the top of your class. The following code adds the Forms library to your class: Imports myForms = Microsoft.Vbe.Interop.Forms
As you can see, this library is imported and aliased to the name myForms. This is a cool .NET feature that allows you to import a library and access its properties and classes via an alias. Adding Reference Properties to Your Controls The next step is to add Reference properties to the controls on your Word 2003 document. The following code adds reference properties for each control: Friend WithEvents myButton _ As myForms.CommandButton Friend WithEvents myZip As myForms.TextBox Friend WithEvents myResult As myForms.TextBox
One item to note is the inclusion of the WithEvents option. These controls send events to your .NET code, which requires use of the WithEvents option. Casting Your Controls After adding the reference variables to your code, you need to cast these controls as the proper object types. The following code uses the FindControl() (which can be found in the code created by the Office Project Wizard) and the Ctype() function to cast the reference properties as the proper object types: myButton = _ CType(FindControl("cmdGetZip"), myForms.CommandButton)
myZip = _ CType(FindControl("txtZipCode"), myForms.TextBox)
myResult = _ CType(FindControl("txtResult"), myForms.TextBox)
The Ctype() function is used to cast an object from one type to another. The FindControl() call returns a handle to the object you wish to cast. Creating the Event Sink The final step in this process is to add code to your class to trap the Click event sent from the button on your Word 2003 document. To do this, add the following code to your class: Dim cZip As String = myZip.Text
Dim oWS As New wsZipService.Service1
Dim cResult As String = oWS.GetZipInfo(cZip)
myResult.Text = cResult
From this code, you can see the Handles statement. This control handles the Click() event of the myButton object. The rest of the code simply instantiates the Web service, sends the proper Zip Code to the Word 2003 document, and returns the information to the Results text box. Running Your Example Now you can run your example using one of two methods. You can press F5 and run the example directly from Visual Studio .NET or from Word 2003. Running your application from the F5 option is how you debug these applications.Figure 6 shows the code above running in debug mode.  Figure 6. A Visual Studio Tools for Office application has been loaded in the Visual Studio .NET debugger.The other mechanism for running this code is to simply open the document in Word 2003. Word 2003 takes care of loading the assembly and calling the code in it. Conclusion As you can see, integrating Visual Studio .NET and Office 2003 is pretty simple. You can use the user-friendliness of Office 2003 along with the power of Visual Studio .NET in your applications without having to resort to VBA code. That's pretty cool. Thanks go out to Ken Getz for helping with the research for this article. Rod Paddock | Lori Turner

Support Professional Developer Support Office Integration Microsoft Corporation What could be cooler than the marriage between all the functionality and security that .NET has to offer and the great data analysis, data entry, and documentation platform that Office provides? That's what Visual Studio Tools for Office is all about; the new possibilities for Office solutions are phenomenal! Peter Torr

Program Manager Visual Studio .NET Microsoft Corporation Visual Studio Tools for Office is the first release from a new team at Microsoft, and has been a great way to get to know everybody in Office development and solidify relationships. We have some pretty cool plans for the future, and I hope our customers will see the potential in the Visual Studio Tools for Office and provide the feedback we need to help improve Office programmability over the coming years. One final thing: keep your machines up to date and try not to run as Administrator ;-) |