Solved

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

Posted on 2007-04-05
12
11,902 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Congratulations! You’re Certified – Now What?

Starting a new career can be overwhelming. Becoming certified in your field of expertise is a great start, but where do you go from here?  Here are some tips to help you on your career journey.

 
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
 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
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.
Suggested Courses
Course of the Month10 days, 6 hours left to enroll

628 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