TALtech Logo
search
Products Support Resources Free Software
  TALtech Home:  Support:  B-Coder Knowledge Base:  B-Coder DDE Examples


DDE Examples for Microsoft Word

Changing Selected Text To A Bar Code:
The following is a simple example of a Microsoft Word macro that copies the currently selected text to the clipboard, opens a DDE link to B-Coder, generates the bar code and finally pastes the bar code into the document replacing the selected text.

Note: B-Coder must be running when the following macro is run from within Word.

Sub MAIN

Selection.Copy ' Copy the currently selected text to the clipboard
' In Word 6.0 use the following EditCopy command instead of Selection.Copy
' EditCopy
Chan = DDEInitiate("B-Coder", "System")
' Initiate DDE link with B-Coder
DDEExecute Chan, "[Paste/Build/Copy]" ' Create the bar code using data in the clipboard
DDETerminate Chan ' Terminate the DDE link
Selection.PasteSpecial DataType:=wdPasteMetafilePicture, Placement:=wdInLine
' In Word 6.0 use the following EditPaste command in place of the above Selection.PasteSpecial line
'EditPaste ' Paste the bar code into the Word document

End Sub

Prompting The User For A Bar Code Message
The following macro pops up a dialog box asking for input of a bar code message. After the user enters a bar code message, the macro launches B-Coder, sends it the bar code message entered by the user, generates the bar code, closes B-Coder and finally pastes the bar code from the clipboard to the open document in the Microsoft Word window.

Sub MAIN

Msg$ = InputBox$("Enter A One Line Bar Code Message", "Bar Code") ' get bar message
If Len(Msg$) Then
Shell "C:\B-CODER3\B-CODER.EXE", 6 ' Run B-Coder Minimized
' Note: make sure that the path for B-Coder is correct in the above command
Chan = DDEInitiate("B-Coder", "System") ' Initiate link with B-Coder
DDEExecute Chan, "[PrintWarnings=off]" ' Turn off printer messages
DDEExecute Chan, "[MessageWarnings=off]" ' Turn off message warnings
DDEExecute Chan, "[Barcode=" + Msg$ + "]" ' Generate the bar code
DDEExecute Chan, "[APPEXIT]" ' Tell B-Coder to quit
Selection.PasteSpecial DataType:=wdPasteMetafilePicture, Placement:=wdInLine
'In Word 6.0 use the following EditPaste command in place of the Selection.PasteSpecial line above
'EditPaste ' Paste the bar code into the Word document

End If

End Sub

Note: B-Coder can be forced to load a specific configuration file when it is launched by specifying a configuration file name on the command line used to activate it. For example, you could first create a B-Coder configuration file that sets up B-Coder to use a specific bar code symbology and/or any other parameters. You could then launch B-Coder with the name of your configuration file on the command line so that it would be already configured for your particular application when it is launched. Suppose you wanted to modify the above macro so that it automatically starts up pre-configured to use the PostNET bar code symbology. You could create a configuration file for B-Coder with the PostNET symbology selected and then save the configuration file with the name POSTNET.BCF. To pre-load the POSTNET.BCF configuration file In the above macro, you would simply change the 4th line to read:Shell

"C:\B-CODER\B-CODER.EXE POSTNET.BCF",0

Adding Bar Codes To Labels or a Mail Merge Document In Word
A typical application for B-Coder is to either add bar codes to mailing labels or to insert bar codes into a mail merge document. The easiest way to do this is to use the "Mail Merge" tool in Word to create your merge documents or labels and then use B-Coder to add the bar codes to the final merged documents containing all of your merge data.

You would first set up your mail merge in Word as normal using the mail merge tool. After you set up your data source and insert your "merge fields" in the main merge document, you could select the merge field (or any static text) in your main merge document that you want converted to a bar code and change its style to a new style named "barcode".

(To create a new style, open the "Format" menu, select "Style" and then click the button marked "New" in the style dialog box. In the "New Style" dialog box, enter the name "barcode" and click the OK button.)

Next, you would complete your mail merge as normal by selecting "Merge to New Document". This will produce a final merge document with all of the data from the data source inserted into your labels or documents. Any merge fields or static text that you assigned the style "barcode" to in your main merge document would also have the style "barcode" in the final merge document. To fill in the bar codes, you could simply run a macro that searches through the final merge document looking for any text with the style "barcode" and replaces that text with a bar code created by B-Coder. The following page contains an example macro that does just this.

The following WordBasic macro (for Word 6 and Word 7) searches through a document looking for any text with the style "barcode". If it finds any, it converts the text to a bar code.

Sub MAIN

Shell "C:\B-CODER3\B-CODER.EXE", 6 ' run B-Coder Minimized
' Note: make sure that the path for B-Coder is correct in the above command
Chan = DDEInitiate("B-Coder", "System") ' open DDE link to B-Coder
' initialize B-Coder here - send dde commands to set up your Bar Codes
' DDEExecute Chan, "[Code128]" ' tell B-Coder to use Code 128
' DDEExecute Chan, "[messagewarnings=off]" ' turn off warning messages
' DDEExecute Chan, "[printwarnings=off]"
' DDEExecute Chan, "[height=.5]" ' set bar code height
StartOfDocument ' move to top of document
On Error Goto bye ' quit if an error occurs
While 1 = 1 ' loop forever
EditFindStyle .Style = "BarCode" ' find style "BarCode"
EditFind .Find = "", .WholeWord = 0, .MatchCase = 0, .Direction = 0, .Format = 1
If EditFindFound() <> - 1 Then Goto bye
' if not found then quit
BCData$ = Selection$() ' otherwise get bar code data
If Right$(BCData$) = Chr$(13) Then BCData$ = Left$(BCData$, Len(BCData$) -1)

' the above line removes the carriage return at the end of the selected text
DDEExecute Chan, "[BARCODE=" + BCData$ + "]" ' generate a bar code
' ----------- option 1 - replace the text with a bar code
EditClear ' delete the current selection
' option 1 works best when adding bar codes to a Merge Document
' ----------- option 2 - put the bar code below the text
' EndOfLine ' move to end of line
' LineDown 1 ' move down one line
' StartOfLine ' move to the start of this line
' option 2 works best when adding PostNET or other bar codes to mailing labels
' you may need to change the above three lines to get the bar code where you need it
EditPaste ' paste in the bar code
Wend ' go find the next one
bye: ' all done
DDEExecute Chan, "[Appexit]" ' tell B-Coder to quit
On Error Goto 0 ' remove error trap

End Sub

Bar Code Merge for Word 97/2000:
The following is a subroutine for Word 97/2000 that searches through a document looking for any text with the style "barcode". If it finds any, it converts the text to a bar code.

Sub MergeBarCodes()

Shell "C:\B-CODER3\B-CODER.EXE", 6 ' run B-Coder Minimized
' Note: make sure that the path for B-Coder is correct in the above command
Chan = DDEInitiate("B-Coder", "System") ' open DDE channel to B-Coder
' initialize B-Coder here- send dde commands to set up Bar Codes
' DDEExecute Chan, "[Code128]" ' tell B-Coder to use Code 128
' DDEExecute Chan, "[messagewarnings=off]" ' turn off warning messages
' DDEExecute Chan, "[printwarnings=off]"
' DDEExecute Chan, "[height=.5]" ' set bar code height
On Error GoTo bye ' quit if an error occurs
Set MyRange = Selection ' define a range object
MyRange.SetRange Start:=ActiveDocument.Content.Start, End:=ActiveDocument.Content.End
' set the range to start at the top of the document and end at the bottom
While 1 = 1 ' loop forever
With MyRange.Find ' set Find object properties
.Forward = True ' search forward
.Wrap = wdFindStop ' stop at end of document
.Style = "barcode" ' look for style "barcode"
.Execute ' do it now
End With
If MyRange.Find.Found = False Then GoTo bye ' quit if style not found
BCData$ = Selection.Text ' otherwise get bar code data
If Right$(BCData$, 1) = Chr$(13) Then BCData$ = Left$(BCData$, Len(BCData$) - 1)
' the above line removes the carriage return at the end of the selected text
DDEExecute Chan, "[BARCODE=" + BCData$ + "]" ' send to B-Coder
' Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdMove
' to put the bar code on the line below the text, un-comment the above line
MyRange.PasteSpecial DataType:=wdPasteMetafilePicture, Placement:=wdInLine
' paste in the bar code
Wend ' go find the next one
bye:
On Error GoTo 0
' remove error trap
DDEExecute Chan, "[Appexit]" ' tell B-Coder to quit

End Sub

Back to Top

TALtech Home  |  Products  |  Resources  |  Free Software  |  Support  |  Buy Now  |  Register Your Products  | 
Site Map  |  Contact TALtech  |  News