Handling multiple instances of Word with multiple documents when Automating Word

Experts,

I need to generate a word document with data stored in SQL Server 2005 using VB.Net.  

I use to do this sort of thing a lot in VB6 and I'm new to .Net.  I remeber that having different instances of Word open and multiple documents per Word instance always was a n problem until I found some VB6 code to take care of it. Never really understood the code though ....

My question is:  what is the correct way handling this problem in .Net?

Thanks
PantoffelSlippersAsked:
Who is Participating?
 
nirojexpertConnect With a Mentor Commented:
try getObject first, if it generates error at runtime, then use the createObject
on error goto err_getObj

set s = getObject("...")
goto withOBJ
err_getObj:
on error resume 0 'cancel on error
'we will have to create it
set s = createObject("....")

withObj:


about olb for .net, i think the same.
0
 
PantoffelSlippersAuthor Commented:
Doubled the points!

Any suggestions?

Thanks
0
 
nirojexpertCommented:
there are two methods createObject and getObject
use the getObject to get a previous instance of the application.

or create a word application at global level

open each doument using the same word application object.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
nirojexpertCommented:
you should also close the objects after creating it


set wordApp = nothing
0
 
PantoffelSlippersAuthor Commented:
OK but how do I know if an existing instance of Word is already running?  If Word is already open I would not like to open another instance.  

Also, in VB6 I would add a reference to the Word OLB file; is there a replacement in .Net for this or do I still add a COM object / OLB file?
0
 
PantoffelSlippersAuthor Commented:
Any idea what goes inside the brackets?

set s = CreateObject("xxx")  :  what should xxx be?  Would the value of xxx change depending on the version of Word installed?  (2000, 2002, 2003, 2007).

Also, if I'm correct, you are using VB6 syntax above- will it be the same for .Net?

Thanks
0
 
nirojexpertCommented:
the object name is independent of the class name. so do not worry about it.
0
All Courses

From novice to tech pro — start learning today.