Hi,
COM
We all know that Word and Excel and all other application that are COM able can be controlled from Alpha Five with Automation. Just create a COM object of the application in Xbasic and start coding and controlling.
But what if you want to do the opposite? You can't create an Alpha Five object in another language and access the Alpha Five object model. It just doesn't exist. Alpha isn't COM able in the way as Word or Excel is.
But there is a way to control Alpha Five from 'any' other application where you can program and select a COM or ActiveX device.
MOM
The technique I describe here looks like message-oriented middleware (MOM). Applications on different systems can exchange messages with each other. They can wait on each other for a reply (synchronous communication), or just going on while awaiting another message (asynchronous communication). Messages are queued on the recieving system (not in the example). MOM is suitable for peer-to-peer-communication, but also client/server-systems who makes use of distributed processing.
Microsoft Word
What if you have a Word document and in that document you have several places where you want to let the user insert some information, an address, yes/no, a selection from a list or whatever. If you have the data in Access or Excel than there is no problem, the integration between these programs is fabulous. Write your code to manage Access or Excel and voila. But what if you are an Alpha Five user and want to establish some integration between Word or Excel and Alpha Five. I mean it in this order. Word to A5, Excel to A5. Word or Excel is here the 'central' application. I haven't seen this until now.
A5 slaved to Word or Excel
Slaved to what? Ok, I just mean turn A5 into a server and let it listen to a tcp/ip port. The information it reads from that port tells it what it must do. If you tell A5 to delete all the records from its tables it would probably do so.
Another possibility is that you could access an A5 server on another computer in another country just from Word and get your information you want, but for now we just refer to the localhost, your computer. It's just like your local WAS version but now with the name "Word Application Server".
What do we need?
First we need Alpha Five and Microsoft Word, the two application we want to communicate with each other. What do we need for connecting them? From Alpha to Microsoft Word we can use COM, but from Word to Alpha we need another technique. Alpha Five has the Xbasic language and that is a very powerful language. It is equipped with a huge command set and we can use a combination of Xbasic commands. For our purpose there is a command EVALUATE_TEMPLATE(). This command executes Xbasic statements in plain ASCII text. This means that we can use Xbasic to control Alpha Five. So how do we get the Xbasic statements executed in Alpha Five? And how do we connect these two programs? For this job I choose tcp/ip and the use of the Xbasic INET::Socket Object.
The situation for Microsoft Word is a little more difficult. It has no tcp/ip commands standard present. In an earlier version of my example I made a Purebasic DLL with API tcp/ip commands, but that introduced a lot of overhead code. By accident I found a little toolbox that does this job just fine in Microsoft Word VBA and its free too. You can download the "Toolsack Baseline 1.1" toolbox from http://www.toolsack.com/. You need to install this toolbox because the example Word file references it.
The Example
This is just an example of what you can do with Alpha Five Xbasic and Microsoft VBA. I hope you enjoyed this example.
What follows is the code under the Start Server button on the form.
The next code is from the OnTimer event of the form. The OnTimer event fires every 500 ms.
The next code is from the Stop Server button.
This is just an example and is not fit for a real appliaction but it show you an "How to do" if you want execute Xbasic code from any place in Alpha Five.
************* Addition ******
I forgot to tell that the thread dumps the xbasic code that is send to the server in file threadlog.txt in the root. There you can see if all things are send well.
Here is also the VBA code that supports the send process.
COM
We all know that Word and Excel and all other application that are COM able can be controlled from Alpha Five with Automation. Just create a COM object of the application in Xbasic and start coding and controlling.
But what if you want to do the opposite? You can't create an Alpha Five object in another language and access the Alpha Five object model. It just doesn't exist. Alpha isn't COM able in the way as Word or Excel is.
But there is a way to control Alpha Five from 'any' other application where you can program and select a COM or ActiveX device.
MOM
The technique I describe here looks like message-oriented middleware (MOM). Applications on different systems can exchange messages with each other. They can wait on each other for a reply (synchronous communication), or just going on while awaiting another message (asynchronous communication). Messages are queued on the recieving system (not in the example). MOM is suitable for peer-to-peer-communication, but also client/server-systems who makes use of distributed processing.
Microsoft Word
What if you have a Word document and in that document you have several places where you want to let the user insert some information, an address, yes/no, a selection from a list or whatever. If you have the data in Access or Excel than there is no problem, the integration between these programs is fabulous. Write your code to manage Access or Excel and voila. But what if you are an Alpha Five user and want to establish some integration between Word or Excel and Alpha Five. I mean it in this order. Word to A5, Excel to A5. Word or Excel is here the 'central' application. I haven't seen this until now.
A5 slaved to Word or Excel
Slaved to what? Ok, I just mean turn A5 into a server and let it listen to a tcp/ip port. The information it reads from that port tells it what it must do. If you tell A5 to delete all the records from its tables it would probably do so.
Another possibility is that you could access an A5 server on another computer in another country just from Word and get your information you want, but for now we just refer to the localhost, your computer. It's just like your local WAS version but now with the name "Word Application Server".
What do we need?
First we need Alpha Five and Microsoft Word, the two application we want to communicate with each other. What do we need for connecting them? From Alpha to Microsoft Word we can use COM, but from Word to Alpha we need another technique. Alpha Five has the Xbasic language and that is a very powerful language. It is equipped with a huge command set and we can use a combination of Xbasic commands. For our purpose there is a command EVALUATE_TEMPLATE(). This command executes Xbasic statements in plain ASCII text. This means that we can use Xbasic to control Alpha Five. So how do we get the Xbasic statements executed in Alpha Five? And how do we connect these two programs? For this job I choose tcp/ip and the use of the Xbasic INET::Socket Object.
The situation for Microsoft Word is a little more difficult. It has no tcp/ip commands standard present. In an earlier version of my example I made a Purebasic DLL with API tcp/ip commands, but that introduced a lot of overhead code. By accident I found a little toolbox that does this job just fine in Microsoft Word VBA and its free too. You can download the "Toolsack Baseline 1.1" toolbox from http://www.toolsack.com/. You need to install this toolbox because the example Word file references it.
The Example
- To work with the example you need to download the MOM example. It contains the database with the server and the Word template. Unpack them and put the template in your Word template folder and the database in another folder whatever you like.
- Download now the Toolsack toolbox and install it.
- Start de Alpha Five MOM database and go to the forms tab in the control panel.
- Start the form with name "Server".
- Push the button "Start server", the button gets red when the server is active. Because the server is running in a background thread it will not block your Alpha Five session.
- Start MSWord and position the windows of both applications in such way that you can see them both and does not overlap each other.
- Create in Word a new document from the MOM template.
- Push on the first button in the Word document. There should be a dialog form pop up.
This is just an example of what you can do with Alpha Five Xbasic and Microsoft VBA. I hope you enjoyed this example.
What follows is the code under the Start Server button on the form.
Code:
'Date Created: 14-Mar-2007 12:04:23 AM 'Last Updated: 27-Dec-2007 10:12:54 PM 'Created By : Alpha Software (source: helpfile) 'Updated By : Marcel Kollenaar option strict dim thread_code as C dim global buffer as C = "" thread_code := <<%code% 'Thread error logging is done to: xbasic_error_log("c:\wordip_thread_error.txt") dim thread_log_file as C = "c:\threadlog.txt" dim this_thread as P this_thread := thread.current() this_thread.set_priority(-2) dim thread_run_flag as L = .T. dim serversocket as INET::socket dim temp as C dim request as C dim nl as C = crlf() dim ip_port as N dim acceptresult as CallResult 'In my case I chose port 10000 for listening but it can be any not 'predefined port. The port must be the same as the port to be send to 'in the Word example. ip_port := 10000 while thread_run_flag 'Your computer is the localhost = 127.0.0.1 if serversocket.Listen(ip_port,"127.0.0.1") then 'save_to_file("Serversocket.listen(1)"+nl,thread_log_file,.t.) 'Accept a blocking socket request. dim clientsocket as INET::socket acceptresult = serversocket.accept(clientsocket,2000) if acceptresult.success then on error resume next 'save_to_file("ClientSocket accepted" + nl,thread_log_file,.t.) clientsocket.read(temp,10000) while len(temp) > 0 request = request + temp temp = "" on error resume next if clientsocket.IsReadable clientsocket.read(temp, 10000) end if save_to_file("Request: " + request + nl,thread_log_file,.t.) end while buffer := "" buffer := request ui_beep(UI_STANDARD_BEEP) save_to_file("Buffer: " + buffer + nl,thread_log_file,.t.) request:="" clientsocket.Close() end if serversocket.close() end if yield control end while %code% 'Pass the thread a pointer of the variable namespace 'of the main thread with main_space. thread_create("wdthread", thread_code)
Code:
'Date Created: 17-Mar-2007 04:55:39 PM 'Last Updated: 27-Dec-2007 11:18:28 PM 'Created By : Marcel Kollenaar 'Updated By : Marcel Kollenaar 'Make the button red when the thread runs. if thread_is_running("wdthread") then :Server:Button1.Fill.Color = "red" else :Server:Button1.Fill.Color = "System-ButtonFace" end if 'The execution interval is 500 ms. If the data recieving doesn't fit 'within the 500 ms than the code in the recieve buffer will not be complete 'and creates probably an error when ececuted. if len(buffer) > 0 then EVALUATE_TEMPLATE( buffer )'Execute the that is send from Word. 'ui_msg_box("INFO",buffer)'Show the code that has been send. 'ui_beep(UI_STANDARD_BEEP) 'for testing buffer := "" 'Clear the recieve buffer. end if
Code:
'Date Created: 15-Mar-2007 07:17:26 PM 'Last Updated: 27-Dec-2007 02:35:54 PM 'Created By : Marcel Kollenaar 'Updated By : Marcel Kollenaar 'This statement assigns a false to the thread_run_flag. 'The thread with name "wdthread" will stop. if thread_is_running("wdthread") then thread_variables("wdthread").thread_run_flag := .F. ui_msg_box("Thread Info","Thread has stopped!") else ui_msg_box("Thread Info","There is no Thread active!") end if
************* Addition ******
I forgot to tell that the thread dumps the xbasic code that is send to the server in file threadlog.txt in the root. There you can see if all things are send well.
Here is also the VBA code that supports the send process.
Code:
Public Sub fromu3() 'Uses Toolsack COM. Dim s As New socket Dim rs As Integer Dim sendstring As String On Error GoTo err_1 s.connect "127.0.0.1", 10000 sendstring = Replace(ActiveDocument.Bookmarks("xdialog").Range.Text, Chr(13), vbCrLf) s.Write sendstring Exit Sub err_1: MsgBox "The Alpha Five MOM server must be started first" End Sub Public Sub fromu4() 'Uses Toolsack COM. Dim s As New socket Dim rs As Integer On Error GoTo err_1 s.connect "127.0.0.1", 10000 sendstring = Replace(ActiveDocument.Bookmarks("xdialogEvent").Range.Text, Chr(13), vbCrLf) s.Write sendstring Exit Sub err_1: MsgBox "The Alpha Five MOM server must be started first" End Sub
Comment