How to Access a Serial Port from Excel, Word and Visual Basic

Table of Contents

These code examples are suitable for use in Visual Basic, Word, Excel and Access. The serial port ActiveX object can be instantiated directly from VBA code (see the first example) or be manually inserted into a form or worksheet. The embedded ActiveX control icon is only visible at design time and hidden when user program is running.

The first example demonstrates an ActiveX set up process and how to tune up serial port settings. All other examples are focused on serial port event notifications and data receiving. The data reception process is asynchronous and you don't need to poll the port for incoming data.

How to Run the Serial Port ActiveX in Microsoft Word

The part of VBA code intended to create an ActiveX object has slightly different realization in Word and Excel. Other parts of code are completely similar. Use this example code to insert the serial port interface object into Word document and then look to the Excel VBA code for full featured example.

Dim sh As Shape Set sh = Shapes.AddOLEControl("STROKESCRIBE.StrokeReaderCtrl.1") Dim reader As StrokeReader Set reader = sh.OLEFormat.Object

How to Use the Serial Port ActiveX in Excel

Before running this sample code, please check the Reader.Port property and specify your serial port number.
Call the connect procedure from your code to open the serial port.
Additionally, you need to add a reference to the StrokeReader ActiveX into your VBA project.

You need to update StrokeReader ActiveX to the latest version to use this example. The code is compatible with v2.6 and newer.

Private Sub connect() ' Call this procedure to open the serial port '---------------Excel-specific part of code---------------- Dim sh As Shape Dim Reader As StrokeReader ' Go to the Tools->References and add the reference to StrokeReader ActiveX ' This will insert the ActiveX into the current document Set sh = ActiveSheet.Shapes.AddOLEObject("STROKESCRIBE.StrokeReaderCtrl.1") Set Reader = sh.OLEFormat.Object.Object '---------------Common code for Excel and Word---------------- ' The shape's name must match to CommEvent handler procedure name, see below sh.Name = "Reader" Reader.Port = 9 ' Please specify YOUR port number here Reader.Connected = True ' Checks the port is available If Reader.Error Then MsgBox Reader.ErrorDescription ' or, show the error description Exit Sub End If Reader.BaudRate = 9600 ' The data transfer speed Reader.Send "123ABCD" ' Sends the data to the serial port ' Shorten the pins 2 and 3 of the serial port connector to receive the sent data back End Sub ' Simple serial port event handler. ' Expects only incoming data notifications and ignores all other notification types Private Sub Reader_CommEvent(ByVal Evt As StrokeReaderLib.Event, _ ByVal data As Variant) If Evt = EVT_DATA Then s = Reader.Read(TEXT) MsgBox s End If End Sub

Note: Modems, some Bluetooth devices and weighing scales may require you to properly configure DTR/RTS lines of the serial port.

If your device doesn't respond, try following:

StrokeReader1.DTR = True

StrokeReader1.RTS = True

How to Write a Full Featured Serial Port Event Handler

This CommEvent handler expects connection/disconnection events from RS232/RS485 USB adapters, data reception events and serial port state change notifications.

' The advanced serial port event handler. Private Sub Reader_CommEvent( _ ByVal Evt As StrokeReaderLib.Event, _ ByVal data As Variant) Select Case Evt ' Can be EVT_DISCONNECT or EVT_DATA or EVT_SERIALEVENT. Case EVT_DISCONNECT MsgBox "Disconnected" ' This event is reported when USB serial port adapter is disconnected from PC. Case EVT_CONNECT ' Do something if connected to serial port MsgBox "Connected" ' or reconnected to RS232/RS485 adapter just attached to USB bus. Case EVT_DATA s = Reader.Read(TEXT) MsgBox s ' If you need an array of bytes, use this code: ' s = Reader.Read(BINARY) ' MsgBox Hex(s(0)) Case EVT_SERIALEVENT ' CTS, DSR or RING line events If CLng(data) And EV_CTS Then MsgBox "CTS=" + Str(Reader.CTS) ' The .CTS property allows to get the current serial port line state End If If CLng(data) And EV_DSR Then MsgBox "DSR=" + Str(Reader.DSR) End If End Select End Sub

Note: When a Bluetooth device goes offline, the EVT_DISCONNECT will not fire. This is because Bluetooth serial ports bound to a Bluetooth dongle instead of a remote device. These serial ports disappear only when the dongle is removed from the USB port.

In most cases, you can detect disconnection by listening for the EVT_SERIALEVENT events and checking the DSR line state. DSR goes low when the device goes offline.

Transmitting Large Files Using Serial Port Events

This method is based on the EV_TXEMPTY notification that user program receives when TX FIFO of the serial port is empty. The transmit_file procedure reads a binary file by small parts and sends them to the port. The process is asynchronous and does not prevent the user to interact with Excel worksheet or Access database.

Dim txfile As Integer Dim data() As Byte Private Sub CommandButton1_Click() ' The button starts the file transmission txfile = FreeFile Open "1.bin" For Binary As txfile transmit_file End Sub Sub transmit_file() ' This Sub reads the binary file by small parts If txfile = 0 Then Exit Sub ReDim data(1 To 1000) Get txfile, , data If EOF(txfile) Then rm = LOF(txfile) Mod UBound(data) ReDim Preserve data(1 To rm) Close txfile txfile = 0 End If StrokeReader1.Send data End Sub ' The serial port event handler Private Sub StrokeReader1_CommEvent(ByVal Evt As StrokeReaderLib.Event, ByVal data As Variant) If Evt = EVT_SERIALEVENT Then If CLng(data) And EV_TXEMPTY Then transmit_file ' This will read and transmit the next part of the file if the TX buffer of the port is empty End If End If End Sub

If you don't need to track the progress of the file transmission or if your files are relatively small, use this code:

Open "1.bin" For Binary As 1 Dim data() As Byte l = LOF(1) ReDim data(1 To l) Get 1, , data Close 1 StrokeReader1.Send data

The code reads entire file into a byte array and passes it to the ActiveX. The EV_TXEMPTY will fire only once when the transmission is finished. You may ignore the EV_TXEMPTY or use the event to inform user about finished background operation.

Facebook X Bluesky Youtube Contacts

© 2026 StrokeScribe. All rights reserved. Use of any portion of this site constitutes acceptance of our Terms of Use and Privacy Policy. The website material may not be reproduced, except with the prior written permission.