Content by Category
.NET 1.x
.NET 2.0
.NET 3.0
.NET 3.5
.NET 4.0
.NET Assemblies
.NET Framework
.NET Getting Started
Accessibility
ADO.NET
Advertorials
Agile Development
AJAX
Architecture
ASP.NET
ASP.NET MVC
ASP.NET WebForms
B2B (Business Integration)
BizTalk
Book Excerpts
Build and Deploy
C#
C++
Code Contracts
CODE on the Road!
COM+
Community
Conferences
Continuous Integration
Crystal Reports
CSLA.NET
CSS
Data
Design Patterns
Development Process
Display Technologies
Distributed Computing
DotNetNuke
DSL
Dynamic Programming
Editorials
Enterprise Services ("COM+")
Entity Framework
Events
Expression Blend
F#
Fox to Fox
Frameworks
Functional Programming
Graphics
Internet Explorer 8.0
Interviews
iPhone
Java
Java Script
jQuery
LINQ
Linux
Mac OS X
MDX
Microsoft Application Blocks
Microsoft Business Rules Framework
Microsoft Expression
Microsoft Office
Mobile Development
Mobile PC
Mono
Network
NHibernate
Object Oriented Development
Open Source
Opinion
Opinions
Oracle
ORM
Other Languages
Parallel Programming
Patterns
Podcasts
Post Mortem
PowerPoint
Print/Output
Product News
Product Reviews
Project Management
Python
Q&A
Reporting Services
REST
RIA Services
Ruby
Search
Security
Services
SharePoint
Silverlight
SOA
Social Networks
Software & Law
Software Business
Source Control
Speech-Enabled Applications
SQL Server
SQL Server 2000
SQL Server 2005
SQL Server 2008
SQL Server CE/AnyWhere/Mobile/Compact
Subversion
Sync Framework
Tablet PC
TDD
Team System
Techniques
Testing and Quality Control
Tips
UI Design
UML
User Groups
VB Script
VB.NET
VFP and .NET
VFP and SQL Server
Virtual Earth
Vista
Visual Basic
Visual Basic 6 (and older)
Visual FoxPro
Visual Studio .NET
Visual Studio 2005
Visual Studio 2008
Visual Studio 2010
Visual Studio Tools for Office
VSX
WCF
Web Development (general)
Web Services
WF
Whitepapers
Windows 7
Windows Azure
Windows Live
Windows Server
Windows Vista
WinForms
Workflow
WPF
XAML
XML
XNA
XSLT



PyCon 2010


 


CODE TRAINING

Reader rating:
Click here to read 1 comment about this article.
Article source: CoDe (2003 - Vol. 1 - Issue 2 - Microsoft Office System )


Article Pages: < Previous - 1 2  3 


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:

  1. Add reference properties to your class definition.
  2. Cast your reference properties to their respective MSForm object types.
  3. 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.

Click for a larger version of this image.

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 ;-)



Article Pages: < Previous - 1 2  3 

Page 1: Using the Microsoft Visual Studio Tools for the Microsoft Office System
Page 2: Examining the Code (cont.)
Page 3: Creating the Web Service

How would you rate the quality of this article?
1 2 3 4 5
Poor      Outstanding

Tell us why you rated the content this way. (optional)

Average rating:
4.2 out of 5

16 people have rated this article.

      ESDC

 

GiveCamp - Coding for Charity