Solved

Accessing VB.NET class from Word 2007 VBA

Posted on 2009-07-16
7
505 Views
Last Modified: 2013-11-10
I have a VB.NET (VS2008) application that, in part, performs some automation in a Word 2007 document.  However the VB.NET application does not recieve events from the Word application, so I have written some code in a template within Word to respond to events.  The problem is that I now need to access some classes in my VB.NET application from the Word VBA, or else duplicate all that code within the Word template.

Thanks very much.
0
Comment
Question by:AbeFisher
  • 4
  • 3
7 Comments
 
LVL 8

Expert Comment

by:rseabird
ID: 24872330
Hi, why not using VSTO for Visual Studio 2008? You can easily create you Word Add-on (or whatever you like) with full ability to the functions of Word.
0
 

Author Comment

by:AbeFisher
ID: 24873606
I'm sorry - I don't quite understand your suggestion.  What do you mean by "using VSTO for Visual Studio 2008"?
Perhaps some additional clarification is in order.  I have written an app in VB.NET that runs independently and has several functions.  Only one of these functions is to insert formatted text into MS Word.  So I don't want to migrate the whole project to VBA; rather I am just trying to accomplish some specific communication between the code written in Word VBA/VSTO and the code written in Visual Studio:  either raise events in the Word VBA code that can be handled by the VB code, or allow the VBA code to access classes in the VB project.
Does that help?
0
 
LVL 8

Expert Comment

by:rseabird
ID: 24876284
VSTO = Visual Studio Tools for Office.
With these tools you can easily create Office Add-in solutions in .NET. In your situation I suggest to create an Word Add-in with VSTO, create a reference to your other .NET app, and you can just use the advantage of Word functionality as well as your own .NET app. No need to migrate code to VBa.
By the way, VSTO is free to download at Microsoft.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:AbeFisher
ID: 24878980
Thank you rseabird.
My question, however, is HOW to do the very thing you suggest.  I believe with VS 2008 VSTO are included and already installed - at least I am able to create a Word Add-In project in VB.NET.  Within this project I can see the application level events, but not the document level events.  So my question is twofold, I guess:
1.  How do I get the document level events to show up?
2. How do I go about creating a reference to my other .NET app?  Is that some sort of "include" statement?  What is the syntax?
Thanks very much!
0
 
LVL 8

Accepted Solution

by:
rseabird earned 250 total points
ID: 24886744
Just a brief answer for this moment,
You have your application object, inside you can find your current document. I am not complete sure, but I thought there you can find events etc.
About creating a reference, that's the normal way you do it in any ordinary VS2008 solution. Add a reference in your project properties and use the imports statement at the top of the class where you want to use it.
0
 

Author Comment

by:AbeFisher
ID: 24891944
Thanks for giving this a shot.  I finally got it to work --
In the VB.NET Add-in application, I created a document class and instantiated it WithEvents, then was able to access the document level events.  As for accessing the class that is part of my original VB.NEt application, I just loaded that class module into the Add-In application.  This works, but is not ideal, as I now need to maintain it in two locations.  Eventually I'll try to make this class stand-alone, and compile it, so I can import it into each project as a .dll.
0
 

Author Closing Comment

by:AbeFisher
ID: 31604286
This started me in the right direction, but lacked the specifics necessary to understand what I really needed to do.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question