Solved

Accessing VB.NET class from Word 2007 VBA

Posted on 2009-07-16
7
496 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

706 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

18 Experts available now in Live!

Get 1:1 Help Now