Word Programming - issue with Word versions and automation

J

Jonathan West

Aminc said:
Agreed Jean-Guy, but I guess I am not able to explain. Problem is that
InsertLineBreaks argument with the ActiveDocument.SaveAs is unavailable in
versions prior to 2003. So if I code to pass InsertLineBreaks:=True is
user
is running say 2000 or 97, I am unable to compile the program.

You are quite correct, but there is a workaround.

Place all code that is Word-2003 specific in a separate module. Call it from
the appropriate If-then or Select Case construction.

Do not attempt to compile the code in the VBA editor.

The code will run OK in Word 2000, because the module containing Word 2003
code is never called and therefore never compiled while in Word 2000. It is
called and will compile fine in Word 2003.


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

Michael Bednarek

Sure. I can do that... but problem is when I use InsertLineBreaks:=True (for
Word 2003), I cannot compile it in Word 2000 or Word 97 because that property
is unavailable.

Shot in the dark:

Function WordVersion() As Single
Dim wrdObj As Object

Set wrdObj = CreateObject("Word.Basic")
WordVersion = Val(wrdObj.AppInfo(2)) ' Word Version

End Function
(Found at <http://groups.google.com.au/group/m...aovba/msg/eddf2c0c21e549eb?dmode=source&hl=en>).

Then, in your main code:

If WordVersion = 11 Then
#Const WrdVer = 11
End If
...
...
#If WrdVer = 11 Then
ActiveDocument.SaveAs FileName:=sDocName, FileFormat:=iFormat, InsertLineBreaks:=True
#Else
ActiveDocument.SaveAs FileName:=sDocName, FileFormat:=iFormat
#End If
 
J

Jean-Guy Marcil

Michael Bednarek was telling us:
Michael Bednarek nous racontait que :
Shot in the dark:

Function WordVersion() As Single
Dim wrdObj As Object
...

I am not sure, but I have tried something like that before and found that it
was impossible to declare your own Compiler constant. I took your code and
twisted it around to test it in Word 2003:

'_______________________________________
Function WordVersion() As Single
Dim wrdObj As Object

Set wrdObj = CreateObject("Word.Basic")
WordVersion = Val(wrdObj.AppInfo(2)) ' Word Version

End Function
'_______________________________________

'_______________________________________
Sub test()

If WordVersion = 9 Then
#Const WrdVer = 9 'Word 2000 = 9
End If

#If WrdVer = 9 Then
ActiveDocument.SaveAs FileName:=sDocName, FileFormat:=iFormat
#Else
ActiveDocument.SaveAs FileName:=sDocName, FileFormat:=iFormat,
InsertLineBreaks:=True
#End If

End Sub
'_______________________________________

So, under 2003, the Else statement should get executed, right? Or did I miss
something?

But in fact, it is the If statement that gets executed, as if the value of
WrdVer did not matter.

Or am I doing something wrong?

Finally, is there a reason for using:

'_______________________________________
Function WordVersion() As Single
Dim wrdObj As Object

Set wrdObj = CreateObject("Word.Basic")
WordVersion = Val(wrdObj.AppInfo(2)) ' Word Version

End Function
'_______________________________________

instead of something like:

'_______________________________________
Dim WordVersion As Long
WordVersion = Val(Left$(Application.Version, 2))
'_______________________________________

in the same procedure?

(I am not asking so much about the branching out, which maybe useful if you
have many different module that have to check the Word version, but more
about the CreateObject("Word.Basic") aspect of the function...)

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
E

Edward Thrashcort

I know, have you read the whole thread or just the part where you jumped

Evidently I may have missed some messages.
Apologies. I had not associated that message with you.
Was that really necessary?

Maybe you don't understand English humour? It was meant as a friendly
rebuke - albeit that it turned out I was wrong!

No offense intended.

I'm glad the the problem is solved by compiling only in 2003. However I am
surprised that Jon West's suggestion works - it will still work in earlier
version. I assume it's being distributed as an executable addin?


Eddie
 
M

Michael Bednarek

Michael Bednarek was telling us:
Michael Bednarek nous racontait que :


I am not sure, but I have tried something like that before and found that it
was impossible to declare your own Compiler constant. I took your code and
twisted it around to test it in Word 2003:
[snip]

As I said, it was s shot in the dark. Now, it seems to me that
constructs like
If cond Then
#Const myConst = expr
Else
#Const myConst = Not expr
End If
are illegal (Duplicate definition), and constructs like
If cond Then
#Const myConst1 = expr1
Else
#Const myConst2 = expr2
End If

will cause both myConst1 and myConst2 be set to their respective
expressions. Maybe this sentence in the Help file on the "#Const
Directive" is responsible:
"Conditional compiler constants are always evaluated at the
module level, regardless of their placement in code."

I give up. Upgrade everyone to V-11.
Finally, is there a reason for using:

'_______________________________________
Function WordVersion() As Single
Dim wrdObj As Object

Set wrdObj = CreateObject("Word.Basic")
WordVersion = Val(wrdObj.AppInfo(2)) ' Word Version

End Function
'_______________________________________

instead of something like:

'_______________________________________
Dim WordVersion As Long
WordVersion = Val(Left$(Application.Version, 2))
'_______________________________________

in the same procedure?

(I am not asking so much about the branching out, which maybe useful if you
have many different module that have to check the Word version, but more
about the CreateObject("Word.Basic") aspect of the function...)

I just copied the code from the quoted source.
 
J

Jean-Guy Marcil

Edward Thrashcort was telling us:
Edward Thrashcort nous racontait que :
Evidently I may have missed some messages.
Apologies. I had not associated that message with you.

No problems.
Maybe you don't understand English humour? It was meant as a friendly
rebuke - albeit that it turned out I was wrong!

No offense intended.

None really taken because I wasn't sure on the intention. I slipped
thatquestion in just to make sure.
I have had lots of contacts with British people and their humour and love
most British comedies (one of my favourite nowadays is Father Ted...)
But it has been a while seen I have hung out with the Queen's subjects...
Maybe a smiley would have helped?
I'm glad the the problem is solved by compiling only in 2003. However
I am surprised that Jon West's suggestion works - it will still work
in earlier version. I assume it's being distributed as an executable
addin?

I think he was suggesting exactly the same as me, except that at he
recommended putting each Word version code in its own separate module
(whereas I mentioned using separate Subs/Functions). You can write code for
Word 2003 and run it under Word 2000 as long as the Word 2003 code does not
have to be compiled; which it will if you call the Sub containing the said
code. I believe Jonathan suggested a different module to be sure it will not
be compiled unless you explicitly call the module during execution.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
E

Edward Thrashcort

Glad we see eye to eye
most British comedies (one of my favourite nowadays is Father Ted...)

If you get the chance to see "My Hero" also starring "father Ted" (Ardal
O'Hanlon) then give it a go. You will either love it or hate it but I think
it's really funny!

I guess you realise that's a quote from an 007 where James Bond is being
briefed about his latest gadgets?


Eddie
 
J

Jean-Guy Marcil

Edward Thrashcort was telling us:
Edward Thrashcort nous racontait que :
Glad we see eye to eye


If you get the chance to see "My Hero" also starring "father Ted"
(Ardal O'Hanlon) then give it a go. You will either love it or hate
it but I think it's really funny!

Yeah, saw that on late night BBC-Canada... It does have its funny moments...
(BTW O'Hanlon does not play Father Ted, he plays the "simple" priest whose
character name I can't remember right now...)

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
F

Fletcher James

I deal with issues like this all the time. The easiest thing is to declare
an extra variable as OBJECT.

Dim ww11 As Object

Then, you can say:

If Application.Version > "10.99" Then
Set ww11 = Application
ww11.SomePropertyOnlyAvailableInWord11 = ...

In other words, you're using late binding -- properties of "Object"
variables aren't checked until they're called.

If it's a Document property, then simply declare:

Dim doc11 as Object

If Application.Version > "10.99" Then
Set doc11 = ActiveDocument
doc11.SomePropertyOnlyAvailableInWord11 = ...

and so forth.

--
Fletcher James
President
Levit & James, Inc.

(703)771-1549
MailTo:[email protected]
http://www.levitjames.com
 

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