Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you set the MSComm ActiveX object in VBA code

Status
Not open for further replies.

saxmaniac

Technical User
Mar 4, 2004
2
US
how do you set the MSComm ActiveX object in VBA code(Excel): I have tried variations on the following:

Dim MSComm1 As Object

Set MSComm1 = New MSComm.Application

OR

Set MSComm1 = CreateObject("MSComm.Object")

I have not been able to find any examples that work.
 
Set MSCOMM = CreateObject("dllname.modulename") is one way of looking at it
';without a ref in excel GUI
Dim MSCOMM As Object
Set MSCOMM = CreateObject("MSCOMM.Application")
 
The MSCOMM control has to be on a form.
The default event is the OnComm event and the code for the OnComm event must be on a form.

Private Sub MSCOMM_OnComm()
if userform1.mscomm1.commevent = comEvReceive then
'Call Something
end if


The rest of the code can be in any module. You do not have to "Set" the object, simply refer to the userform.

BaudRate = 9600
Parity = "n"
Bytes = 8
StopBit = 1
ComOpenParameters = BaudRate & "," & Parity & "," & Bytes & "," & StopBit
With UserForm1.MSCOMM1
.InputLen = 30
.InputMode = comInputModeText
.Settings = ComOpenParameters
.CommPort = 2
.RThreshold = 0
.InBufferSize = 4096
.OutBufferSize = 512
.PortOpen = True
.RThreshold = 1
IF .PortOpen = false then
MsgBox "Try another port"
end if
End With

The above code will set the port parameters and open the port. If the port doesn't open the msgbox pops up. If the port is opened, since the above code sets the RThreshold (receive threshold) to "1", the OnComm event will fire when a single character is received. If set to 0, the Oncomm event will not fire at all. Set RThreshold to the number of characters you want in the port before OnComm fires. There is also a setting for SThreshold (Send Threshold).

There are other settings as well. See the old thread regarding "Excel Com Port Communication" in this forum. You must have a license to program to MSCOMM32.ocx. The license is included with VB but not with VBA. But once the license is in the registry, and MSCOMM32.ocx is in the system folder you can program to it. Create a reference to it in the Visual Basic editor (it will be in there as Microsoft Comm Control 6.0 or something). The tool can then be added to the Userform toolbox and dragged onto a userform from there. Right click on the MSCOMM icon on the form and select View Code. The MScomm_OnComm() event will be created. Hit the F2 key to bring up the search pane and search for OnComm. The other possible events comEvSend etc.) will be listed.

Greg
 
I was curious if you got MSCOMM to work for you.

Greg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top