4.1. Microsoft Office

Microsoft Office programs use VBA (Visual Basic for Applications) functions to access external objects. For creating VBA functions, the interfaces are almost identical. You usually access the VBA code through a module, or macro. In Access, you can have the Visual Basic Editor pop up by creating a new module or click on an existing module. In Word or Excel, the Visual Basic Editor pops up when you select Macro followed by Visual Basic Editor.

Microsoft Office 2003 increased the default security settings to high, which does not allow any VBA code. To work with VBA functions, enable macros or lower the security setting.

The Excel sample (used as Word Mail data source) and Access sample we provided contain a VBA function called EncodeDM. The function is just a wrapper around the Encoder object. It is necessary because VBA function is the only way to enter into the definition of a field. To use it you need to have it in your own database or spreadsheet. You can export it to a file and then import it into your spreadsheet or database. Or you can create a new module, and paste the code from the sample, or from this manual.

The code below is taken from our Access sample:

' To use this function you must have Morovia DataMatrix Fontware 
' installed properly in your computer. This can be verified by 
' running Morovia DataMatrix Encoder (GUI) program.
' if you can successfully generate DataMatrix symbols from the 
' encoder program you have all the files needed.
' Refer to documentation for meanings of each field.
' Note that the ActiveX version Encoder's ProgID is 
' Morovia.DataMatrixFontEncoder
Public Function EncodeDM(DataToEncode As String, 
                         format As Integer, 
                         line_feed_string As String) As String
    Dim encoder As Object
    Dim output As String
    Set encoder = CreateObject("Morovia.DataMatrixFontEncoder")
    encoder.DataMatrixTargetSizeID = format
    If (line_feed_string = "") Then
        line_feed_string = Chr(13) & Chr(10)
    End If
    
    encoder.LineFeedString = line_feed_string
    output = encoder.Encode(DataToEncode)
    
    EncodeDM = output
End Function

Once you have the function defined, you are ready to add bar code field to your document by formatting a text field with data matrix font and setting the data source of the field to the result of the function. For example, the following formula sets the text object to hold the encoder result applied to a database field [TestData.Data]:

=EncodeDM([TestData.Data],0,"")