Conditional Compilation

J

jkorchok

I am aware that there are no conditional compilation constants after
VBA6 to distinguish between versions of Word. I have also read items
like the following workarounds:
What you can do is place all the office 2007-specific code into separate modules, If no code in that module is ever called when the template is used in Word 2003, then the template will run OK.

The problem I'm having is that either this doesn't work or I'm missing
something. I have a module called Word2003 that contains only the
following:

Sub TurnOffReading()
Options.AllowReadingMode = False
End Sub

I call it like this:

If Left(Application.Version, 2) >= "11" And
Left(System.OperatingSystem, 3) = "Win" Then
Word2003.TurnOffReading
End If

I still get the message "Compilation error in hidden module" when
running this in earlier versions of Word.
 
J

Jonathan West

I am aware that there are no conditional compilation constants after
VBA6 to distinguish between versions of Word. I have also read items
like the following workarounds:


The problem I'm having is that either this doesn't work or I'm missing
something. I have a module called Word2003 that contains only the
following:

Sub TurnOffReading()
Options.AllowReadingMode = False
End Sub

I call it like this:

If Left(Application.Version, 2) >= "11" And
Left(System.OperatingSystem, 3) = "Win" Then
Word2003.TurnOffReading
End If

I still get the message "Compilation error in hidden module" when
running this in earlier versions of Word.

It looks like you may have neglected to put a line continuation character at
the end of the first line

You may want to investigate using the CallByName function to get round these
compile errors instead.

Have you tried running the code with the VBA editor open so you can see
which line causes the compile error?


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

jkorchok

Hi Jonathan,

If Left(Application.Version, 2) >= "11" And
Left(System.OperatingSystem, 3) = "Win" Then

is all on one line in the original code.

Options.AllowReadingMode = False

is the line that causes the compile error in version of Word earlier
than 2003.

Do you know of any source of information about CallByName? Most of
what I find by Googling refers to VB and the search engine seems to be
broken at the Word MVP site.

Thanks for any help!

John Korchok
 
J

Jonathan West

Hi Jonathan,

If Left(Application.Version, 2) >= "11" And
Left(System.OperatingSystem, 3) = "Win" Then

is all on one line in the original code.

Options.AllowReadingMode = False

is the line that causes the compile error in version of Word earlier
than 2003.

I suspect that the compile error is also happening somewhere else. Try
opening the template in Word 2000, opening the VBA editor, commenting out
that line and compiling the code. See which line the editor complains about.
Do you know of any source of information about CallByName? Most of
what I find by Googling refers to VB and the search engine seems to be
broken at the Word MVP site.

Type CallByName into the immediate window in the VBA editor and press F1 to
bring up the Help topic on that keyword.

The CallByname version of that particular line is as follows

CallByName Options, "AllowReadingMode", VbLet, False

That won't cause a compile error. If you try to run that line in a version
of Word older than 2003, you will get a runtime error because the
AllowReadingMode property doesn't exist.

CallByName is slower than using the objects directly, but can be useful for
cases like this.
 
S

Scott Holmes

What you're doing works for me in VBA code that works with Word 97, 2000,
20002, 2003 and 2007, but with a couple of differences:

1. I use the Call statement. Example:
Call Word2003.TurnOffReading
This probably makes absolutely no difference, but I include it for
completeness.

2. Running your code will not work with Word 97, which uses VBA5. To get
around this problem, I use conditional compilation to completely hide code
within each Sub in the hidden module. My suggestion for what to put inside
your module "Word2003":

**************************
'Subroutines that require Word 2003 or later.
' Calling anything in this module with an earlier version of Word will cause
an error.
' Conditional compilation allows compilation by Word 97.

'Following line makes this code public, but only within the project and
' not available using Word menu: Tools > Macro > Macros.
Option Private Module

Sub TurnOffReading()
'Must hide the internal code from Word 97
#If (VBA6) Then
Options.AllowReadingMode = False
#End If
End Sub

**************************

Good luck with MS Weird!
 

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