TweetFollow Us on Twitter

The REALbasic-Office secret decoder ring

Volume Number: 20 (2004)
Issue Number: 7
Column Tag: Programming

REALBasic Best Practice

by Guyren G Howe

The REALbasic-Office secret decoder ring

How to control Microsoft Office from REALbasic

Last time, I discussed some ways of "breaking out" of REALbasic, to call on external applications to supplement what your REALbasic application can do. I lamented that I was unable to explore controlling Microsoft Office from RB, because although it can do that really well, the documentation for this feature was both inadequate and horribly, egregiously wrong.

Since that time, things have gotten slightly better. There is now a web page (http://www.realsoftware.com/office2004mac/) from which you can download a small number of actual, correct, working examples. But, there is still no documentation to speak of, and what there is, it would lead you to believe that using Office from REALbasic is harder than it really is.

So in this article, I will present the apparently entirely undocumented, super-secret really easy way to control Office from RB.

But First...

Since I can't find anywhere else to put it, a quick aside: there were some errors in my article on REAL World. I had said I thought there were fifty attendees, but it turns out there were over a hundred. I also mistakenly wrote that there were three seminars per day, but what I meant to say was that there were three or four sessions at a time, and a bunch of sessions each day. It was quite a good seminar program.

Ta-Da! The Big Secret

Microsoft Office, on both Macintosh and Windows, incorporates a fairly sophisticated built-in scripting language called Visual Basic for Applications (VBA). The really super secret of controlling Microsoft Office from REALbasic -- the one I had to offer up my first-born and a handful of magic beans for -- is that REALbasic understands the entire VBA namespace. This means that essentially every term you can see in Word's object inspector (more on that in a moment) can be used in REALbasic. Classes from Word can be used in REALbasic just by sticking the letters Word in front of them (and similarly for Excel and PowerPoint), and method and function calls on those classes can (with one type of exception) be used unmodified. So, for example, you can fetch the text of the front most Word document with the following code:

GetTheFrontDocument()
A function that returns the text of the front most Word document
Sub GetTheFrontDocument() As String
dim w As New WordApplication
dim doc As WordDocument = w.ActiveDocument()
Return doc.Content().Text
End Sub

So the Word Document class becomes the WordDocument class in REALbasic. Also, the global methods and properties in VBA become method calls and properties on WordApplication, ExcelApplication, or PowerPointApplication in REALbasic.

There. The secret's out. Not much to it, really. Now I'll just fill in a few details, and explore some code examples.

Named Arguments

The only major difference between VBA and REALbasic is that VBA supports a nice feature called named arguments. This means that rather than having to remember, and write an ordered list of bare values for the arguments to a method call, you can provide them as assignment statements, like the last line of this code snippet (taken from the REALbasic built-in help for the Office class):

Example of Replace in VBA

Example Visual Basic for Applications code to perform a find and replace

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "find this"
.Replacement.Text = "replace with"
.Wrap = wdFindContinue
.Format = false
.MatchCase = false
.MatchWholeWord = false
.MatchWildcards = false
.MatchSoundsLike = false
.MatchAllWordForms = false
End With
Selection.Find.Execute Replace:=wdReplaceAll

This is great, but the translation into REALbasic, given in the built-in documentation, doesn't work. Here's how to actually do it:

The previous example, in REALbasic

Using the secret decoder ring, we translate the last example into REALbasic

Dim word as New WordApplication
Dim find As WordFind = word.ActiveDocument.Content.Find

find.ClearFormatting
find.Replacement.ClearFormatting
find.text = "find this"
find.Replacement.Text = "replace with"
find.Wrap = Office.wdFindContinue
find.Format = False
find.MatchCase = false
find.MatchWholeWord = false
find.MatchWildcards = false
find.MatchSoundsLike = false
find.MatchAllWordForms = false

// Now the fun stuff

Dim replaceParam as New OLEParameter
replaceParam.Value = Office.wdReplaceAll

// according to the docs on Find.Execute the Replace parameter is the 11th
replaceParam.Position = 11

find.Execute replaceParam

If you had more than one named parameter, you would use multiple OLEParameter objects, one for each named parameter, and you just pass them all, in any order, to the method call.

A Larger Example

I'm now going to go through a small, entirely artificial piece of code, intended to cram together a variety of techniques. This article is not remotely large enough to attempt even a quick survey of Office's impressive programming features, but I hope I've presented you with just enough to get you up, and going on your own.

The code will search through all the comments in the front most Word document. It will find all the comments that start with the word test followed by two numbers (all separated by spaces). Any such comment will be replaced by a table with the number of rows and columns given by the two numbers, and then the words "Hello, World" will be placed into the cell in the upper, left corner of the table. I'll go through not only the code, but more importantly how I worked out how to write it. I'll be working with Word X, not Word 2004. I don't consider the new version a worthwhile upgrade, so what you see will probably be slightly different in the newer version (although the code should still work the same).

Note that while the example, and my comments refer to Word, the same ideas apply to Excel and PowerPoint (except that, oddly enough, PowerPoint doesn't support macro recording).

Word's Macro Environment

Most of what you need to know, you can learn from within Word itself. In the Tools menu, you'll find a Macro submenu, which provides access to Word's programming features. In this menu, the REALbasic Editor command just launches REALbasic. The Record New Macro... command opens a dialog for you to name a new Macro, which is what Office calls a program written in VBA.


Figure SEQ Figure \* ARABIC 1: The Record Macro dialog

After you click OK in this dialog, you can execute a series of actions, and Word will translate those actions into a VBA program to do the same thing again. Note that you can't click to move the insertion point while you're recording a macro. There isn't any way for Word to turn that action, that only makes sense in that document (scrolling to that point, in a window of that size) into a set of actions repeatable anywhere.

While you're recording the macro, Word shows a tiny little window with stop and pause buttons:


Figure SEQ Figure \* ARABIC 2: The Macro recording palette

Now, just carry out the sorts of actions you want REALbasic to control, then hit the stop button in the little window. Next, go back to the Macro submenu and choose Macros.... You will then see a window showing the name of the macro you just recorded, along with any other macros you have previously written or recorded:


Figure SEQ Figure \* ARABIC 3: The Macros window

Click on the name of the macro you just recorded, then click Edit. Here is what I got when I recorded a macro in which I created a table along the lines of our little project:


Figure SEQ Figure \* ARABIC 4: The Word Macro editor

There are also other windows showing at this point, but describing this whole programming environment is outside the scope of this document, so I will stick to only what you need most for your REALbasic work.

The Object Browser

Recording a macro will generally give you a good idea of the kind of commands you need to execute to do something. You can also examine every available command, and class in Word by opening the Object Browser. You can do this by clicking a button on the toolbar you see in the macro editing environment:


Figure SEQ Figure \* ARABIC 5: The Object Browser button

Alternately, you can choose Object Browser from the View menu. Any of these will show Word's Object Browser:


Figure SEQ Figure \* ARABIC 6: The Word Object Browser

Note the search box in the middle, at the top (results appear in the blank area). The business part for our purposes is below the blank area. The list of Classes is comprehensive. <globals>, shown in the screen shot, are the methods, and properties available in the WordApplication class in REALbasic.

When you click on a class, the properties, methods, and constants are shown to the right of the class name. If you click on something shown there, its details are shown below, and it is all hyperlinked. This is actually how I developed the code for the example. Well, that, and examining the one working Word example that REAL Software has published.

There are some books on VBA (I understand the O'Reilly book -- ISBN 1-56592-358-8 -- is quite good), and a lot of material online. A bit of drill-down in the Object Browser, and some experimentation, gave me code to do my little example fairly easily.

The Example

To finish up, the example.

ReplaceCommentsWithTables

An example that finds comments in the front most Word document, replacing those that begin with the word table with a table whose dimensions depend on the rest of the comment

Sub ReplaceCommentsWithTables()
  Dim word as New WordApplication
  Dim c As WordComments
  c = word.ActiveDocument.Comments
  
  Dim counter As Integer
  Dim comment As WordComment
  Dim content() As String
  Dim width, height as Integer
  Dim location As WordRange
  if c.Count > 0 then
    counter = 1
    do
      comment = c.Item(counter)
      content = Split(comment.Range.Text(), " ")
      if content(0) = "table" then
        height = content(1).val
        width = content(2).val
        location = comment.Reference
        location.Tables.Add(comment.Reference, width, height)
        location.Tables.Item(1).Cell(1, 1).Range.Text = "hello, world"
      else
        counter = counter + 1
      end if
    loop until counter >= c.Count
  end if
End Sub

The only things that bear explaining here are that a Range in VBA is any point in the document, or any continuous section of text in the document. A Comment object has several Range properties. The one called Range is the actual content of the comment. The one called Reference is the location of the comment in the main text. Also, adding something (in this case, a table) to a range replaces the range's contents (in this case, the comment) with that table. Finally, collections of things in VBA will usually have an Item() function to fetch the collection's contents.

Apart from that, I hope you find that the mostly clear names for things in VBA, the ready availability of example code online, and the ability to record a macro if you want to know how to do something, will make this very useful feature of REALbasic surprisingly easy to use.

Brief aside: Just before I finished this, REAL Software announced a product called Office Power Pack, based on this feature in REALbasic. And Microsoft themselves have written some of the software that ships with Office (the installer and the query tool) in REALbasic. This is a practical feature indeed, and the market for Office add-ons or applications that can work with Office has to be substantial.

Finally, I will note that REALbasic's Office features are almost perfectly cross-platform, without modification other than to path strings and the like.


Guyren G Howe works in artificial intelligence research, after years of work as a technical write,r and developer. He is married with one child, is an Australian, and lives in Austin, Texas. Guyren has been working with REALbasic for several years. Most notably, he wrote the REALbasic Curriculum Project, an extensive computer science curriculum, for REAL Software (available from the REALbasic website). You can contact Mr. Howe at realbasic@mactech.com

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Monterey Cache Cleaner 17.0.4 - Clear ca...
Monterey Cache Cleaner is an award-winning general-purpose tool for macOS X. MCC makes system maintenance simple with an easy point-and-click interface to many macOS X functions. Novice and expert... Read more
Transmit 5.8.5 - Excellent FTP/SFTP clie...
Transmit is an excellent FTP (file transfer protocol), SFTP, S3 (Amazon.com file hosting) and iDisk/WebDAV client that allows you to upload, download, and delete files over the internet. With the... Read more
Xcode 13.4 - Integrated development envi...
Xcode includes everything developers need to create great applications for Mac, iPhone, iPad, and Apple Watch. Xcode provides developers a unified workflow for user interface design, coding, testing... Read more
Paragon Firewall 2.0 - Powerful firewall...
The Paragon Firewall - Network Monitor application gives you the full control over your Mac, protects the privacy of your computer, prevents unwanted network connections. Network Monitor discloses... Read more
Geekbench 5.4.5 - Measure processor and...
Geekbench provides a comprehensive set of benchmarks engineered to quickly and accurately measure processor and memory performance. Designed to make benchmarks easy to run and easy to understand,... Read more
The Unarchiver 4.3.5 - Replacement for t...
The Unarchiver is a much more capable replacement for "Archive Utility.app", the built-in archive unpacker program in OS X. The Unarchiver is designed to handle many more formats than Archive Utility... Read more
WhatsApp 2.2216.8 - Desktop client for W...
WhatsApp is the desktop client for WhatsApp Messenger, a cross-platform mobile messaging app which allows you to exchange messages without having to pay for SMS. WhatsApp Messenger is available for... Read more
ffWorks 3.0.5 - Convert multimedia files...
ffWorks, focused on simplicity, brings a fresh approach to the use of FFmpeg, allowing you to create ultra-high-quality movies without the need to write a single line of code on the command-line.... Read more
Bartender 4.1.49 - Organize your menu-ba...
Bartender lets you organize your menu-bar apps by hiding them, rearranging them, or moving them to Bartender's Bar. You can display the full menu bar, set options to have menu-bar items show in the... Read more
Steam v020 (1642451672) - Multiplayer an...
Steam is a digital distribution, digital rights management, multiplayer and communications platform developed by Valve Corporation. It is used to distribute a large number of games and related media... Read more

Latest Forum Discussions

See All

Major ‘Pokemon Home’ 2.0.0 Update Out No...
A major update for Pokemon Home (Free) for iOS, Android, and Nintendo Switch has just gone live in the form of Pokemon Home 2.0.0. The Pokemon Company recently revealed that this update was coming soon, and it has finally gone live bringing some... | Read more »
SwitchArcade Round-Up: Reviews Featuring...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for May 17th, 2022. In today’s article, we have a little news to check out before we head into two full-sized reviews. Mini Motorways just hit the Switch last week after successful runs... | Read more »
The 10 Best Open World Games on Nintendo...
Open world games have been flying high since Grand Theft Auto III popularized the concept more than twenty years ago, and they show no signs of slowing down with the likes of Elden Ring continuing to chew up the sales charts. While the Nintendo... | Read more »
Roguelike First-Person Looter Shooter ‘G...
One of the most anticipated releases for many has been the mobile port of roguelike first-person looter shooter Gunfire Reborn from Duoyi Games, but it has been kind of a rocky road to get there. Originally launching on PC last fall, a mobile... | Read more »
‘Apex Legends Mobile’ Is Out Now on iOS...
After following the Apex Legends Mobile announcements over the years, it has finally launched on iOS and Android worldwide. Apex Legends Mobile has had early tests, a soft launch, and more leading up to today’s release. While pre-registrations... | Read more »
‘Pokemon Unite’ UNITE Club Membership No...
Pokemon Unite (Free) on iOS, Android, and Nintendo Switch has just launched an optional paid subscription in the form of the UNITE Club membership. This is priced at $9.99 a month and it includes various benefits like gems, trial holowear each week... | Read more »
Hand-Drawn ‘Monkey Island’-Inspired Poin...
Point-and-click adventure games are a perfect fit for mobile devices, and there have been dozens of fantastic adventures released on mobile over the years. Now Short Sleeve Studio is looking to add another to that list with their upcoming title... | Read more »
SwitchArcade Round-Up: Reviews Featuring...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for May 16th, 2022. In today’s article, we’ve got reviews of a few recent releases. Dungeons of Dreadrock, Eiyuden Chronicle: Rising, and Prinny Presents NIS Classics Volume 2 are in the... | Read more »
Best iPhone Game Updates: ‘Fighting Fant...
Hello everyone, and welcome to the week! It’s time once again for our look back at the noteworthy updates of the last seven days. A very spicy assortment of updates have arrived, with plenty of faces we don’t see very often in these hallowed halls.... | Read more »
A Steam Deck Verified Podcast – The Touc...
In this week’s podcast Eli is jumping for joy after spending the past week with his shiny new Steam Deck. We talk about the things we love about the hardware, the games that play well on it, and plenty more. Is this slowly morphing into a Steam Deck... | Read more »

Price Scanner via MacPrices.net

New today at Apple: Clearance 2020 10.9″ iPad...
Apple has Certified Refurbished 2020 10.9″ iPad Airs now available in their online store in a variety of configurations. Models start at $469 and range up to $190 off Apple’s original MSRP. Each iPad... Read more
Prices continue to drop on Apple Refurbished...
Other World Computing has dropped prices again on Apple refurbished, factory-sealed, just-discontinued 27″ 5K iMacs with models now on sale for up to $814 off Apple’s MSRP, starting at only $985.... Read more
Apple has clearance 27″ 5K iMacs available st...
Apple has clearance, Certified Refurbished, 2020 27″ 5K iMacs available starting at $1299 and ranging up to $560 off original MSRP. Apple’s one-year warranty is standard, shipping is free, and each... Read more
Use your Apple Education discount to take $10...
Need a new 13″ Apple MacBook? Whether you’re a student, teacher, or staff member, you can use your .edu email address when ordering at Apple Education to take $100 off the price of a new MacBook. All... Read more
In stock today and on sale: Silver 13″ M1 Mac...
Amazon has Apple’s Silver 13″ MacBook Pros with M1 Apple Silicon processors in stock today and on sale for $150-$250 off MSRP for a limited time. Shipping is free. These prices are the lowest the... Read more
Mac Studio with M1 Max CPU in stock and on sa...
Apple reseller Simply Mac has the new Mac Studio with M1 Max CPU (10-Core CPU) in stock and on sale for $1899.99 including free shipping. Their price is $100 off Apple’s MSRP, and it’s the lowest... Read more
In stock: Open-box 16″ M1 Pro MacBook Pros fo...
Having trouble finding a 16″ M1 Pro MacBook Pro to buy? QuickShip Electronics has open-box return 16″ M1 Pro MacBook Pros in stock and on sale for $350-$450 off MSRP on their eBay store right now,... Read more
Apple Watch SE models are on sale today for u...
Amazon has Apple Watch SE GPS models on sale today for $50 off MSRP including free shipping. Their prices are the lowest currently available for SE Watches: – 40mm Apple Watch SE: $229, save $50 –... Read more
Switch to Red Pocket Mobile and get an Apple...
Switch to Red Pocket Mobile and get an Apple iPhone SE for $100 off MSRP, plus get free 6 months of Unlimited nationwide service with the purchase of any iPhone. Red Pocket Mobile is a nationwide... Read more
8.3″ WiFi iPad minis are in stock today and o...
Amazon is offering sale prices on Apple’s 8.3″ WiFi iPad minis ranging up to $100 off MSRP, depending on configuration. Prices start at $409. Most models are in stock today with free shipping. Amazon... Read more

Jobs Board

Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
Sephora Beauty Advisor - *Apple* Blossom Ma...
Sephora Beauty Advisor - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Systems & Applications Specialist I - *A...
Systems & Applications Specialist I - Apple / Mac Desktop Support Focus **Texas Medical Center-Houston, Texas** Information Technology UTHealth Administration Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.