Pregunta

I have a .dot file with one line of code that must be executed in a Microsoft Word macro if and only if the version of Word 2003 or greater is installed, otherwise it shuold be ignored. I tried to implement it like this, hoping that Visual Basic for Word compiles a line only if it needs to execute it. The code in question is the following (Word 2003 is 11.0)

If Val(Application.Version) >= 11 Then
    ActiveWindow.View.ReadingLayout = False
End If

I still want that the .dot file with the macro is usable in earlier versions of Microsoft Word, such as Microsoft Word 2000.

However, if I try to run the .dot file, it fails on Word 2000 with a compile error because ActiveWindow.View.ReadingLayout is not a valid method in Word 2000. That is, even when the line will never be executed on Word 2000 because Application.Value will be 9.0, Word still tries to compile that line.

Is there any way in Visual Basic for Word to add compiler directives so that some code is not compiled depending on the Word version?

¿Fue útil?

Solución

We ended up implementing it with late binding, which turned out to be a very small tweak:

If Val(Application.Version) >= 11 Then
    Dim obj As Object
    Set obj = GetObject(, "Word.Application")
    obj.ActiveWindow.View.ReadingLayout = False
End If

Otros consejos

Their are two tricks to get it working:

1) Add a module for functions that work only in specific office version and add your subroutines to it.

2) In places where you have to call your functions, add a test for the specific Office Version and then call your subroutine with Application.Run

The original Module would have:

If Val(Application.Version) >= 11 Then
    Application.Run "office11module.disableReadingLayout"
End If

And your specific Module office11module would have

Public Sub disableReadingLayout()
    ActiveWindow.View.ReadingLayout = False
End Sub

This works because Word is not compiling the office11module until it is needed, and we make sure through the Application.Run call that Word does not know that we need it before runtime.

Our Word VBA Products require a Digital Signature.

We utilize MS Office 2000 as a base development application in order to be as backward compatible as possible with some of our clients.

We utilized the solution provided by jonas_jonas (Jan 16 '14 at 17:59) for years while using Word 2000 on Windows XP development platforms.

However - after upgrading to Windows 7 Pro development platforms, the solution no longer worked, giving us "Disk full" errors when attempting to save the Word files after having digitally signed them.

We then tried, and successfully used, the answer (above) provided by Pep (Jan 18 '14 at 23:23) which works when you need to Digitally Sign the Word file.

We figured the "disk full" error was a result of the attempt to compile the project before saving.

If Val(Application.Version) >= 11 Then
    Dim obj As Object
    Set obj = GetObject(, "Word.Application")
    obj.ActiveWindow.View.ReadingLayout = False
End If
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top