Solved

Microsoft Word Object Library in VB.NET - Office 2007, Word 2007, Old OLB Versions

Posted on 2007-04-05
12
11,823 Views
Last Modified: 2013-11-26
I am writing a program in Visual Studio 2005 (VB.NET), which will need to open a Word Document and insert data into it from a database.

I have done this with VB6 and Office 2000 with no problem. Problem here is that I have Office 2007 Installed, but I need my compiled VB.NET program to work on machines with lower versions of Office (2000 and up, or even 2003 and up). I don't think I can use the Office 12 Object Library because it's for Office 2007 and won't work with 2000. I am under the impression that if I use an old version of the OLB, it will be "forward compatible" and work with all later versions. Is this true?

If I do need older versions of the OLB, is there anywhere I can download a developer's distributable version so I can reference it in my program? Is there some other way to make this program "Backward compatible"?

Thanks in advance for any help!
0
Comment
Question by:paix120
  • 9
  • 2
12 Comments
 
LVL 7

Accepted Solution

by:
gbahri earned 300 total points
ID: 18862550
I would suggest you to go for a late binding. Please refer these sites:

http://msdn2.microsoft.com/en-us/library/aa188643(office.10).aspx

http://word.mvps.org/FAQs/InterDev/EarlyvsLateBinding.htm

Hope, this helps you a bit,

thanks,

GBahri
0
 
LVL 3

Author Comment

by:paix120
ID: 18862565
OK, thanks for the response. I'll check those sites out.
0
 
LVL 3

Author Comment

by:paix120
ID: 18862579
Well, I tried using:

Dim objWordApp As Object
Set objWordApp = createobject("Word.Application")

but it underlines "Set" and says there is a syntax error. Any ideas how to use this method properly in Visual Studio 2005, or why I'm getting the error?

0
 
LVL 3

Author Comment

by:paix120
ID: 18862601
I found the following in an article here: http://www.excelguru.ca/blog/category/visual-studio/
but I still can't get the syntax error-free
------------------------------------------------------------
Visual Basic:
oWord = CreateObject("Word.Application")

This does not work in VB.Net though, as "Option Strict On disallows implicit conversions from 'Object' to 'Microsoft.Office.Interop.Word.Application'."

Instead, we need to use the following to explicitly cast the Type:

Visual Basic:
'At the top of the module:

Imports Word = Microsoft.Office.Interop.Word

'In your procedure

oWord = CType(CreateObject("Word.Application"), Word.Application)
0
 
LVL 3

Author Comment

by:paix120
ID: 18862701
OK, duh. The "set" wasn't working because it was outside of a subprocedure.

This is what I have now:
(outside sub)
Dim objWordApp As Object
Dim objWordDoc As Object

(in constructor)
   objWordApp = CType(CreateObject("Word.Application"), Microsoft.Office.Interop.Word.Application)
   objWordDoc = CType(CreateObject("Word.Document"), Microsoft.Office.Interop.Word.Document)

I now understand the big downside to doing it this way - When I do objWordDoc.
no methods or procedures come up. I guess that's the point of late binding.

Next step for me is to test this on an older version of Word and see what happens. I just did a find/replace, so it's still using commands from the Office 2007 OLB I was using.

OK it's really late. I'll get on this again tomorrow.
0
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 50 total points
ID: 18864848
'Set' is not used in VB.Net.

You could try simply

objWordApp = createobject("Word.Application")

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 3

Author Comment

by:paix120
ID: 18865792
Yep, I figured that out when I found this solution:
objWordApp = CType(CreateObject("Word.Application"), Microsoft.Office.Interop.Word.Application)

Now, I am trying to figure out how to do all of the commands to output the document on an Office 2003 system without it crashing. When I get to the point where I know this "late binding" solution is the right choice to solve my initial problem, I'll come back and assign points.

Suggestions still welcome! Thanks.
0
 
LVL 3

Author Comment

by:paix120
ID: 18867573
OK still not working. All I had in my program was the command to open the document and insert some text. I created a Setup project and made sure the MS Word DLLs were included. I installed it on my Office XP computer and got this error:

Unable to cast COM object of type System.___ComObject to interface type Microsoft.Office.Interop.Word.Application. This operation failed because the Queryinterface call on the COM component for the interface …. Failed due to the following error: Error loading type library/DLL

So, apparently I still don't understand late binding enough to implement it. Any other links with help? I'm having a hard time finding anything online that I can understand.

Problem still not solved.
0
 
LVL 7

Assisted Solution

by:gbahri
gbahri earned 300 total points
ID: 18874360
Try using the System.Activator class to instantiate

see the following link with heading 'Using the Activator class to create singletons' for more information:

http://edndoc.esri.com/arcobjects/9.1/default.asp?url=/arcobjects/9.1/ArcGISDevHelp/DevelopmentEnvs/DotNet/SystemComObject.htm

thanks,

GBahri
0
 
LVL 3

Author Comment

by:paix120
ID: 18874537
OK - never heard of that - I will check that out soon. Thanks.
0
 
LVL 3

Author Comment

by:paix120
ID: 18874543
I looked at the code, but I don't really understand what it's doing... can you explain the concept a bit? Thanks!
0
 
LVL 3

Author Comment

by:paix120
ID: 18915944
YAY! I figured out the problem, and it turns out something was wrong with my Word installation. I tried to open the VBA macro editor from inside Word on my computer with Office XP and it said
Said "This application is not working properly", so I clicked "Repair" and it asked for Office XP CD. I put it in and then it said "Installing Word Feature" and opened the VB Editor successfully.

So, from advice given on this thread and on my other open question:
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_22497155.html

I at least have it successfully opening and writing to a Word Doc now, and since I'm developing on a computer with Office XP, it should be compatible with later versions of Word. Thanks for the help!

I don't know if my problem is completely solved, but I'm so glad it's even working at all at this point, I'm going to assign points and I'll ask a new question if I come across any other problems.


0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
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…
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

747 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

13 Experts available now in Live!

Get 1:1 Help Now