Thursday, October 6, 2011

VBA: Compile error when line breaks used

I originally found this bug on GP 9.0, and just this week found that it is still a problem in GP 2010.  VBA developers beware!

It is a common practice in VBA development to use a line continuation character so you can wrap a long single line of code onto multiple lines.  This continuation character is an underscore (_) preceeded by a space.  Here’s an example:

Private Declare Function SHBrowseForFolder Lib "shell32.dll" _
    Alias "SHBrowseForFolderA" (lpBrowseInfo As BrowseInfo) As Long


In GP, if you use line continuation characters in any “Declare” statements at the top of your VBA code window, everything will compile and work correctly.  However, when you export your code to a .package file, then re-import it into GP, it will add an additional “()” to the bottom of the imported code, causing a compile error when you choose "Compile" from the Debug menu.

Declare line break error
Compile Error:
Syntax Error

To prevent these additional characters from being added to your code when the package file is imported, remove the line continuation characters from your declare statements.

Below is a VBA module I've created that opens the windows folder browser window and returns the selected path.  The declare statements in this example have line continuation characters, so when you import this package and compile, the issue will happen to you, too!

modBrowse.package

No comments:

Post a Comment