Programming Windows: Hello, OLE 2.0 (Premium)

In the early 1990s, Microsoft evolved Windows to include component-based inter-process communications capabilities such as OLE Automation. This was an improvement on its first such efforts, called Dynamic Data Exchange (DDE), which was hard to use and only offered object linking and embedding capabilities, the inspiration for the name of its successor, OLE.

I wrote about the transition from DDE to OLE 1.0 and then OLE 2.0 in Programming Windows: OLE (Premium). Here, I’d like to offer up a quick hello, world style application, written in Visual Basic 4, that takes advantage of OLE 2.0’s OLE Automation feature to interact with Microsoft Excel 5.0. Running on Windows NT 3.51 because why not. (This would work identically in Windows 3.x and probably the next several versions of Windows too.)

OLE Automation, as you may recall, was (and still is) a feature that made OLE’s inter-application communications capabilities available programmatically, first to scripting languages like Visual Basic for Applications (VBA), which debuted in Excel 5.0 in 1993. But it was also brought to Visual Basic 3.0 and other standalone development environments as well.

In the Programmer’s Guide for Microsoft Visual Basic 3.0, Microsoft notes that OLE Automation allows application developers to “remotely access and manipulate object linking and embedding objects that are supplied by other applications. And while this reference inexplicably uses a fictional Windows application called “Spreadsheet” to explain the basics of this work, I decided to use a real application, Excel 5.0, instead.

The setup is simple enough. I created a folder for the Visual Basic project I’d be making and then saved a blank Excel spreadsheet, called HELLO.XLS, to that folder. Then, I created the Visual Basic project and saved its project and form files to that folder as well. I changed the main form’s Caption to "" (blank) because I wanted to set that later programmatically.

My goal was to write information to the Excel spreadsheet and to read information back from it as well. So, I created a new global variable in the (general) area of the form:
Dim MyObj As Object
Then, in the form’s Form_Load procedure, which runs when the application’s main form appears, I added the following code:
Set MyObj = CreateObject("Excel.Sheet")
Set MyObj = GetObject("C:\VB\APPS\OLE2\HELLO.XLS")

MyObj.Application.Visible = True

For i = 1 To 10
    MyObj.Cells(i, 1).Value = "Hello, OLE 2.0!"
Next i

Set MyObj = Nothing
Basically, this configures the object variable as an Excel spreadsheet, points it to the Excel file, opens Excel if it isn’t already displayed, and then writes the text “Hello, OLE 2.0!” to first ten cells in the first column in Excel.

And just to be sure that the communication was two-way, I then added this line of code right before the object variable is destroyed. This takes the value from the first cell in the spreadsheet and sets ...

Gain unlimited access to Premium articles.

With technology shaping our everyday lives, how could we not dig deeper?

Thurrott Premium delivers an honest and thorough perspective about the technologies we use and rely on everyday. Discover deeper content as a Premium member.

Tagged with

Share post

Please check our Community Guidelines before commenting

Windows Intelligence In Your Inbox

Sign up for our new free newsletter to get three time-saving tips each Friday

"*" indicates required fields

This field is for validation purposes and should be left unchanged.

Thurrott © 2024 Thurrott LLC