Link to home
Start Free TrialLog in
Avatar of paix120
paix120Flag for United States of America

asked on

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

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!
ASKER CERTIFIED SOLUTION
Avatar of gbahri
gbahri

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of paix120

ASKER

OK, thanks for the response. I'll check those sites out.
Avatar of paix120

ASKER

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?

Avatar of paix120

ASKER

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)
Avatar of paix120

ASKER

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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of paix120

ASKER

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.
Avatar of paix120

ASKER

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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of paix120

ASKER

OK - never heard of that - I will check that out soon. Thanks.
Avatar of paix120

ASKER

I looked at the code, but I don't really understand what it's doing... can you explain the concept a bit? Thanks!
Avatar of paix120

ASKER

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:
https://www.experts-exchange.com/questions/22497155/output-text-to-different-MS-Word-versions-from-VB-NET.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.