Time for McLaren to finally give something back to the community. Figured I'd post it here in case someone does a search. I spent probably the past week trying to figure out how to send a file through COM1 to a CNC machine and didn't have any luck until today. It took a lot of searching and trying out other's code, but this worked for me.
1. Get MSCOMM32.OCX if you don't have it. I got it here:
http://www.martin2k.co.uk/vb6/ocxs/mscomm32.zip
2. Put it in C:\Windows\System32
3. Go to (START)(RUN) type without quotes "regsvr32.exe C:\Windows\system32\mscomm32.ocx"
5. Open Excel
6. (TOOLS)(MACRO)(VISUAL BASIC EDITOR)
7. (INSERT)(USERFORM)
8. (VIEW)(TOOLBOX)
9. RightClick Toolbox, select (ADDITIONAL CONTROLS)
10. Scroll down to "Microsoft Communications Control, version 6.0"
11. Check the box and hit (OK)
12. There should now be a icon of a telephone on the Toolbox. Drag it onto your userform
13. Set the Properties of it however you need.
14. (VIEW)(CODE)
15. My code is written so that when CommandButton2 is clicked the file is sent. I'm not entirely sure how much of this you need, but this is what worked for me. I have a Excel sheet that gets updated with the information I need, then it gets pasted into a word file, saved onto the desktop, then posted out of the COM1 port. I have no idea what "& Chr(13)" is doing(actually don't understand most of the sending code), but without it I didn't have line breaks in the output. The relevant transmitting code is in between the spaces
Well, I think that about covers it. Enjoy!
1. Get MSCOMM32.OCX if you don't have it. I got it here:
http://www.martin2k.co.uk/vb6/ocxs/mscomm32.zip
2. Put it in C:\Windows\System32
3. Go to (START)(RUN) type without quotes "regsvr32.exe C:\Windows\system32\mscomm32.ocx"
5. Open Excel
6. (TOOLS)(MACRO)(VISUAL BASIC EDITOR)
7. (INSERT)(USERFORM)
8. (VIEW)(TOOLBOX)
9. RightClick Toolbox, select (ADDITIONAL CONTROLS)
10. Scroll down to "Microsoft Communications Control, version 6.0"
11. Check the box and hit (OK)
12. There should now be a icon of a telephone on the Toolbox. Drag it onto your userform
13. Set the Properties of it however you need.
14. (VIEW)(CODE)
15. My code is written so that when CommandButton2 is clicked the file is sent. I'm not entirely sure how much of this you need, but this is what worked for me. I have a Excel sheet that gets updated with the information I need, then it gets pasted into a word file, saved onto the desktop, then posted out of the COM1 port. I have no idea what "& Chr(13)" is doing(actually don't understand most of the sending code), but without it I didn't have line breaks in the output. The relevant transmitting code is in between the spaces
Code:
Private Sub CommandButton2_Click()
Dim appWD As Word.Application
Set appWD = CreateObject("Word.Application.8")
appWD.Visible = False
Sheets("Calculator").Select
Sheets("Template").Range("A10").Value = Range("J2").Value
Sheets("Template").Select
Range("A1:A42").Copy
appWD.Documents.Add
appWD.Selection.Paste
appWD.ActiveDocument.SaveAs FileName:="C:\Documents and Settings\All Users\Desktop\temp.tap", FileFormat:=wdFormatText
appWD.ActiveDocument.Close
appWD.Quit
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
FreeNum = FreeFile
Open "C:\Documents and Settings\All Users\Desktop\temp.tap" For Input As #FreeNum
Do While Not EOF(FreeNum)
Line Input #FreeNum, TextLine
MSComm1.Output = TextLine & Chr(13)
Loop
Close #FreeNum
MSComm1.PortOpen = False
End If
Sheets("Calculator").Select
End Sub
Well, I think that about covers it. Enjoy!