using If ... then ... to deal with vba differences between 2003/20

P

Peter

I have an application that people will use under 2003 and 2007

I had hoped to use something like

if Val(Applicaiton.version) >=12 then
options.contextualSpeller = true
end if

But if I run this under 2003 the following error occurs
Compile error: Method of data member now found

How can I take advantage of additional features in 2007 without breaking in
2003

Any assistance gratefully received,

PeterEvans
 
G

Greg

How about:
Sub Test()
If Application.Version >= 12 Then
SetUpForWorr2007
End If
End Sub

Sub SetUpForWorr2007()
Options.contextualSpeller = True
End Sub
 
P

Paul Shapiro

It still won't compile because the line of code with
Options.contextualSpeller will not be recognized by Word 2003. I don't have
a good answer. I've always settled for using the features in the oldest
client version being used.

If this is an application for distribution to multiple clients, some of whom
have Office 2003 and some with Office 2007, you can use a compiler constant
to include all the code in the same module. By changing the value of the
compiler constant you get the appropriate code for the Office version
specified. You still have to manually change the compiler constant to create
the desired version, but it eliminates compiler errors because the
inappropriate code is then ignored. I use this approach for VBA code that
runs in both Access and Word, e.g., so I can maintain a single code base and
still have it work in either environment.

In the module header you define a compiler constant:
#Const ccWORD2007 = 0 (or = 1 when you want to enable that code)

Then in the code you can use:
#If ccWORD2007 = 1 Then
'Word 2007-specific code which will be ignored when the compiler
constant is not =1.
#Else
'Word 2003-specific code if needed
#End If
 
G

Greg Maxey

I don't follow your "It still won't compile." It compiles here for
me. Granted if Iacall run SetUpForWord2007 in Word2003 a compile
error occurs, but if it isn't called by Sub Test then there is no
error. Maybe I am missing something.

Sub Test()
If Application.Version >= 12 Then
SetUpForWorr2007
End If
End Sub

Sub SetUpForWorr2007()
Options.contextualSpeller = True
End Sub
 
P

Paul Shapiro

I took your exact code and created it in a Word 2003 format document using
Word 2007. Then I copied the document to a virtual machine with only Word
2003 installed, not Word 2007. The VBA references were automatically
adjusted to the Office 11 references, which I had not seen before. But when
I try to compile the code, using the Debug menu-> Compile Project, I get the
"Compile error: Method of data member not found" on the
Options.ContextualSpeller line.

Were you testing on a computer with both Office 2003 and 2007 installed? Try
again on a machine with just Office 2003. It won't work. It can't work-
Office 2003 doesn't have that property for the Options object. It might be
that you can successfully run other code and ignore the fact that your code
project will no longer compile, but I never distribute code that wouldn't
compile.

I don't know if both the Word 2007 and 2003 Options objects expose a
properties collection? If so, that might be a way to set that property
without needing the explicit reference. In that case you could use the test
for the application version, or just trap the error if the property is not
in the collection.

I don't follow your "It still won't compile." It compiles here for
me. Granted if Iacall run SetUpForWord2007 in Word2003 a compile
error occurs, but if it isn't called by Sub Test then there is no
error. Maybe I am missing something.

Sub Test()
If Application.Version >= 12 Then
SetUpForWorr2007
End If
End Sub

Sub SetUpForWorr2007()
Options.contextualSpeller = True
End Sub
 
G

Greg Maxey

Paul,

Yes. I do have both Word2003 and Word2007 on the same machine. If I try to
call the SetUpWord2007 procedure then the error you mention does occur
otherwise it doesn't. Sorry if I caused any confusion.
 
T

Tony Jollans

Another way to do this, although not very efficient, is:

If Val(Application.Version) >= 12 Then
CallByName Options, "ContextualSpeller", VbLet, True
End If

This will compile in both 2003 and 2007, but only do anything when run in
2007.
 

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