Solved

Accessing VB.NET class from Word 2007 VBA

Posted on 2009-07-16
7
500 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
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 …
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now