Solved

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

Posted on 2007-04-05
12
11,836 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
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…

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

21 Experts available now in Live!

Get 1:1 Help Now