Word automation questions (Using from VB6)

R

Robertico

I'd like to create a new Ms Word document from my VB6 application using a
template.
I'am new using automation, so i have some questions.

1) I set a reference to my project "Microsoft Word 11.0 Object Library".
Is this backwards compatible with older versions of Microsoft Word ?
Is it possible to load it dynamic (depending on a users version of
Microsoft Word)

2) Is it possible to check if Microsoft Word is installed ?

3) Whats the best way to create an instance of Microsoft Word and create a
new document from my template ?
(Must work with and without an existing running instance of Microsoft
Word)

Dim wdApp As Word.Application
Dim docNew As Word.Document

Set wdApp = New Word.Application
Set docNew = wdApp.Documents Add(App.Path & "\report.dot")

-Or -

Dim wdApp As Word.Application
Dim docNew As Word.Document

Set wdApp = GetObject(, "Word.Application")
If wdApp Is Nothing Then
Set WordApp = CreateObject("Word.Application")
End If
Set docNew = wdApp.Documents.Add(App.Path & "\report.dot")

-Or

Better solution

Thanks in advance,

Robertico
 
C

C.S.Farmer

I'd do this

Set objWord = CreateObject("Word.Application")
If objWord Is Nothing Then
msgFrm.MsgAuto_box "MS Word does not appear to be installed on this
system.", vbExclamation, "Document creation cancelled", 8, True
Exit Sub
End If

With objWord
Set ThisDoc = .Documents.Add(APP_Path & "Templates\IR1.dot")
With ThisDoc

'do what you need to do with the document based on the template
'normally this would involve locating bookmarks and writing to
them.

End With
End With

Chris Farmer
 
S

Steve Barnett

Robertico said:
I'd like to create a new Ms Word document from my VB6 application using a
template.
I'am new using automation, so i have some questions.

1) I set a reference to my project "Microsoft Word 11.0 Object Library".
Is this backwards compatible with older versions of Microsoft Word ?
Is it possible to load it dynamic (depending on a users version of
Microsoft Word)

No. I've had this problem in the past, where I developed apps uing the v10
library and the app fell in a heap when I ran it with office 97. What I do
is keep the MSWORD8.OLB file around (Word 97) and include that in my
references in VB. Since doing this, I've not had a problem supporting any
version of Word.

2) Is it possible to check if Microsoft Word is installed ?

Try creating an instance of Word. It's probably not the most technical of
solutions, but it'll do the job and it's easy to setup.
3) Whats the best way to create an instance of Microsoft Word and create
a new document from my template ?
(Must work with and without an existing running instance of Microsoft
Word)

Dim wdApp As Word.Application
Dim docNew As Word.Document

Set wdApp = New Word.Application
Set docNew = wdApp.Documents Add(App.Path & "\report.dot")

-Or -

Dim wdApp As Word.Application
Dim docNew As Word.Document

Set wdApp = GetObject(, "Word.Application")
If wdApp Is Nothing Then
Set WordApp = CreateObject("Word.Application")
End If
Set docNew = wdApp.Documents.Add(App.Path & "\report.dot")

-Or

My preference is to use the GetObject/CreateObject version. That way you
always get to an existing copy of Word first. You should also combine this
with your previous question... if the CreateObject also fails to return a
Word application, then you can assume that Word isn't installed or isn't
configured properly.

Steve
 
R

Robertico

Thank, but i think it's pitiful that Microsoft hasn't a better solution.

Robertico
 
S

Steve Barnett

If Microsoft had all the solutions, we'd all be out of a job. I like it when
they do half a job - makes me money!

Steve
 
T

TC

Solution to what?

If you use early binding, as in your original post, your code may be
faster, but version dependant. If you use late binding, as suggested by
one respondent, your code may be slower, but not version dependent.
It's up to you to make an informed choice between those two
alternatives.

TC
 
S

Steve Barnett

It's not strictly true to say that late binding makes you version
independent. I used late binding for a long time and my apps worked well
until they were installed on a relatively old system.At this point, I found
that my Excel automation failed to work.

Cutting a long and boring subject short, I eventually found that there were
incompatibilities between an app compiled using the Excel 2000 libraries and
running against Excel 97. What had happened was that I was using early
binding during development (to get Intellisense) and changing to late
binding for release. Doing this tempted me in to using a function available
in Excel 2000 that was not available in Excel 97.

I now keep the Excel 97 library for development, so am always constrained to
the abilities of the lowest version of Excel that I want to support.

Steve
 
T

TC

Ok, let's try this: "Late binding makes your code version independent,
<<unless you think that features which do not exist in earlier
versions, WILL exist in earlier versions>>."

Goes without saying, methinks!

TC
 
S

Steve Barnett

You're missing the point. If you use intellisense, the temptation is to use
the options it presents to you. I strongly doubt that you check every
property or method that Intellisense offers to you actually exists for the
target platform.

Now, there is something that needs saying.

Lat binding makes you code version independent PROVIDED you have coded for
ONLY the lowest version of the target platform.

Steve
 
T

TC

Steve said:
Late binding makes you code version independent PROVIDED you have coded for
ONLY the lowest version of the target platform.


Precisely what I said: "Late binding makes your code version
independent, <<unless you think that features which do not exist in
earlier versions, WILL exist in earlier versions>>."

TC
 
J

Jonathan West

Robertico said:
I'd like to create a new Ms Word document from my VB6 application using a
template.
I'am new using automation, so i have some questions.

1) I set a reference to my project "Microsoft Word 11.0 Object Library".
Is this backwards compatible with older versions of Microsoft Word ?
Is it possible to load it dynamic (depending on a users version of
Microsoft Word)

On your development machine, load the oldest version of Word that you want
to support. The the reference to that, and it will be forward-compatible
with *later* versions.
2) Is it possible to check if Microsoft Word is installed ?

Yes.
Determine If Word is Installed
http://www.fawcette.com/vsm/2002_06/magazine/columns/qa/default_pf.aspx
3) Whats the best way to create an instance of Microsoft Word and create
a new document from my template ?
(Must work with and without an existing running instance of Microsoft
Word)

Dim wdApp As Word.Application
Dim docNew As Word.Document

Set wdApp = New Word.Application
Set docNew = wdApp.Documents Add(App.Path & "\report.dot")

That would work, though I would instead recommend that you put the template
in the user templates folder, and make that last line as follows

Set docNew =
wdApp.Documents.Add(wdApp.Options.DefaultFilePath(wdUserTemplatesPath) &
"\report.dot"


-Or -

Dim wdApp As Word.Application
Dim docNew As Word.Document

Set wdApp = GetObject(, "Word.Application")
If wdApp Is Nothing Then
Set WordApp = CreateObject("Word.Application")
End If
Set docNew = wdApp.Documents.Add(App.Path & "\report.dot")

That will also work. But if you are early binding and setting a reference to
Word, I would use the first option.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top