[Tex/LaTex] How to typeset VBA code in LaTeX

codeprogramming

I have a lot of VBA (Visual Basic for Applications, the programming language in Microsoft Office) code which I have to add to my thesis in the appendix.

I wonder how I can most comfortably list those lines of code (which have to be structured in a certain way, as there are many, many lines) include those in my pdflatex document.

Has anybody here experience with typesetting large amounts of VBA code?


Option Explicit

Sub Signal(strSignalfolge As String, Optional lngTakt As Long = 100)
'Prozedur erzeugt eine Serie von Warntönen.
'Die optionale Variable lngTakt gibt den Takt in Millisekunden vor (Standard: 100 ms)
'Das Muster kann über die String-Variable strSignalfolge beeinflusst werden:
' Stern (*)     -> 1 Warnton
' Ziffern 1..9  -> 1..9 Takte Pause
' Leerzeichen   -> 1 Sekunde Pause
' Minus (-)     -> 1.5 Sekunden Pause
    Dim i As Integer
    Dim b As String
    For i = 1 To Len(strSignalfolge)
        b = Mid(strSignalfolge, i, 1)
        Select Case b
            Case "*": beep
            Case 1 To 9: DELAY CInt(b) * lngTakt
            Case " ": DELAY 1000
            Case "-": DELAY 1500
        End Select
        DELAY lngTakt
    Next i

End Sub

Public Function strParse(Data As String, Trenn As String, Nr As Integer)
'Funktion trennt die Zeichenkette <Data>
    On Error Resume Next
    Dim MainData() As String, SplitData() As String
    MainData = Split(Data, Trenn)
    SplitData = Split(MainData(Nr - 1), Trenn)
    strParse = SplitData(0)
End Function


Public Sub ProtokollZeile(strData As String)
'Gibt eine Protokollzeile im Direktfenster aus: Datum, Uhrzeit und <strData>
    Debug.Print Now & " " & strData
End Sub

Sub NetSend(strmsg As String, Optional strEmpf As String = "imf3beck")
'Prozedur sendet die Kurznachricht <strMsg> über das Intranet an den Rechner <strEmpf>
    Dim a
    a = Shell("cmd.exe /c net send " & strEmpf & " " & strmsg, vbMinimizedFocus)
    MsgBox "(net send message)" & vbCr & strmsg
End Sub

Sub NetSendMessungBeendet(Optional strBem As String = "")
'Sendet über das Netzwerk eine Statusnachricht, dass die Messung beendet wurde und gibt diese
'Nachricht auch lokal als Warndialog aus
Dim strMsgText As String
strMsgText = (Format(Now, "hh:mm:ss") & " Messung beendet" & strBem)
NetSend (strMsgText)
End Sub

Public Function ZellBereichAdresse(strZellber As String) As String
Application.Volatile
ZellBereichAdresse = CStr(Range(strZellber).Address)
End Function

Best Answer

listings packages will help you.

Using style=A

enter image description here

Using style=B

enter image description here

TeX Input File

\documentclass[dvipsnames,cmyk]{article}
\usepackage{listings,xcolor}

\lstset
{
    breaklines=true,
    tabsize=3,
    showstringspaces=false
}


\lstdefinestyle{Common}
{
    extendedchars=\true,
    language={[Visual]Basic},
    frame=single,
    %===========================================================
    framesep=3pt,%expand outward.
    framerule=0.4pt,%expand outward.
    xleftmargin=3.4pt,%make the frame fits in the text area. 
    xrightmargin=3.4pt,%make the frame fits in the text area.
    %=========================================================== 
    rulecolor=\color{Red}
}

\lstdefinestyle{A}
{
    style=Common,
    backgroundcolor=\color{Yellow!10},
    basicstyle=\scriptsize\color{Black}\ttfamily,
    keywordstyle=\color{Orange},
    identifierstyle=\color{Cyan},
    stringstyle=\color{Red},
    commentstyle=\color{Green}
}

\lstdefinestyle{B}
{
    style=Common,
    backgroundcolor=\color{Black},
    basicstyle=\scriptsize\color{White}\ttfamily,
    keywordstyle=\color{Orange},
    identifierstyle=\color{Cyan},
    stringstyle=\color{Red},
    commentstyle=\color{Green}
}

\begin{document}
\begin{lstlisting}[style=A]%please try style=B
Option Explicit

Sub Signal(strSignalfolge As String, Optional lngTakt As Long = 100)
'Prozedur erzeugt eine Serie von Warntönen.
'Die optionale Variable lngTakt gibt den Takt in Millisekunden vor (Standard: 100 ms)
'Das Muster kann über die String-Variable strSignalfolge beeinflusst werden:
' Stern (*)     -> 1 Warnton
' Ziffern 1..9  -> 1..9 Takte Pause
' Leerzeichen   -> 1 Sekunde Pause
' Minus (-)     -> 1.5 Sekunden Pause
    Dim i As Integer
    Dim b As String
    For i = 1 To Len(strSignalfolge)
        b = Mid(strSignalfolge, i, 1)
        Select Case b
            Case "*": beep
            Case 1 To 9: DELAY CInt(b) * lngTakt
            Case " ": DELAY 1000
            Case "-": DELAY 1500
        End Select
        DELAY lngTakt
    Next i

End Sub

Public Function strParse(Data As String, Trenn As String, Nr As Integer)
'Funktion trennt die Zeichenkette <Data>
    On Error Resume Next
    Dim MainData() As String, SplitData() As String
    MainData = Split(Data, Trenn)
    SplitData = Split(MainData(Nr - 1), Trenn)
    strParse = SplitData(0)
End Function


Public Sub ProtokollZeile(strData As String)
'Gibt eine Protokollzeile im Direktfenster aus: Datum, Uhrzeit und <strData>
    Debug.Print Now & " " & strData
End Sub

Sub NetSend(strmsg As String, Optional strEmpf As String = "imf3beck")
'Prozedur sendet die Kurznachricht <strMsg> über das Intranet an den Rechner <strEmpf>
    Dim a
    a = Shell("cmd.exe /c net send " & strEmpf & " " & strmsg, vbMinimizedFocus)
    MsgBox "(net send message)" & vbCr & strmsg
End Sub

Sub NetSendMessungBeendet(Optional strBem As String = "")
'Sendet über das Netzwerk eine Statusnachricht, dass die Messung beendet wurde und gibt diese
'Nachricht auch lokal als Warndialog aus
Dim strMsgText As String
strMsgText = (Format(Now, "hh:mm:ss") & " Messung beendet" & strBem)
NetSend (strMsgText)
End Sub

Public Function ZellBereichAdresse(strZellber As String) As String
Application.Volatile
ZellBereichAdresse = CStr(Range(strZellber).Address)
End Function
\end{lstlisting}
\end{document}

Notes:

  1. It is better to import the VBA source code from within your TeX input file using \lstinputlisting[<options>]{filename.vb}. This way will make your TeX input file clean.
  2. Try my style B by changing style=A to style=B.