Skip to content

Communicating with Microsoft Office Automation in Xojo

Microsoft Office for Windows can be controlled using something called the VBA Office Object Model. You can use Xojo to communicate with this object model so that you can control Word, Excel and PowerPoint from your Xojo apps on Windows.

To use Office Automation with Xojo, you have to first ensure that the Office Automation plugin is in the Xojo Plugin folder before you start Xojo. You can find the plugin in the Extras/Office Automation folder of your Xojo installation. Simply copy it to the main Plugins folder in the Xojo installation folder.

controlling_Microsoft_Windows.png

After doing this, you’ll find that there is a new module and many new classes that are available to you. In particular, you can now use these:

  • Office module
  • ExcelApplication class
  • PowerPointApplication class
  • WordApplication class

You can use these to communicate with the Office Object model so that you can control Excel, PowerPoint and Word.

Note: Office Automation only works on Microsoft Windows.

Here a quick example to show you how you can send text to Microsoft Word.

  1. Create a new Xojo Desktop project.
  2. Drag the WordApplication control from the Library onto the Window1 layout. This will put the control into the Shelf at the bottom of the Layout Editor. Change its name to WordApp.
  3. Add a TextArea to the layout. Change its name to WordArea.
  4. Add a Button to the layout.
  5. Add the Action event handle to the Button and add this code:
    Dim doc As WordDocument
    WordApp.Visible = True
    doc = WordApp.Documents.Add
    doc.Range.Text = WordArea.Text
    Exception err As OleException
     MsgBox(err.Message)
  6. Run the project, enter some text in the TextArea and click the button.
  7. Microsoft Word will start (if it’s not already running) and a new document will be created with your text added.

The above code uses the WordDocument class, which maps to the Office Object Model’s Document class. In that class, it uses the Document class’s Range method to set the text for the document.

You can refer to the official Microsoft docs for specifics on the Office Object Model:

For other tips on how to use Office Automation with Xojo, refer to the Office Automation Overview page in the Xojo Dev Center.


Xojo Windows