VBA/Excel/Access/Word/Language Basics/Compiler Directive
Содержание
Use #Const to define the constant
<source lang="vb">
- Const UseEventLog = True
Private Sub DebugPrint(ByVal Source As String, ByVal Message As String)
#If UseEventLog Then Call EventLog.WriteEntry("Source: " & Source & " Message: " & Message) #Else Debug.Print "Source: " & Source & " Message: " & Message #End If
End Sub
</source>
Use #If
<source lang="vb">
- Const Debugging = True
Public Sub Trap(ByVal Source As String, ByVal Message As String)
#If Debugging Then Call DebugPrint(Source, Message) Stop #End If
End Sub
</source>
Use string text value as the compiler directive
<source lang="vb">
- Const Language = "Spanish"
Sub cmdConditionalCompilation_Click()
#If Language = "Spanish" Then msgBox "Hola, Que Tal?" #Else msgBox "Hello, How Are You?" #End If
End Sub
</source>
With the compiler directive
<source lang="vb">
- Const Language = 1
Sub ConditionalIf()
#If Language = 1 Then msgBox "Hola, Que Tal?" #Else msgBox "Hello, How Are You?" #End If
End Sub
</source>