From Apache OpenOffice Wiki
Revision as of 18:32, 24 February 2007 by Pmsyyz (Talk | contribs)

Jump to: navigation, search

VBA interoperability project is a joint Novell & Sun incubator project whose aim is to deliver a way to run Excel and Word macros natively in imported documents.

  • Noel Power ( co-lead )
  • Andreas Breagas ( co-lead )

Visual Basic for Applications (VBA) is an implementation of Microsoft's Visual Basic which is built into all Microsoft Office applications.

Latest news

  • Update Wiki Page with latest info
  • Novell and Sun to work together on the VBA incubator project
  • I am delighted that Andreas Bregas now joins me to co-lead the vba incubator project
  • cws npower6 will contain the initial code drop


  • helperapi - The compatibility API generously donated by Sun that was part of their migration toolkit
  • oovbaapi - the ooo-build vba interoperability solution originally implemented by Novell + object implementations from the helperapi. Note: Ooo-build contains the latest VBA model and patches which allows you to run macros from your Microsoft Office documents. This model is under heavy development by Novell and others and the changes are continuously ported to openoffice. It is expected that many macros won't run for now.


Task queue and status ( in order of priority )

Feature Description Issue # Planned cws Stared Milestone integrated
migrate ooo-build code suck in the mountain of idl and implementation code from ooo-buils 74096 npower6 yep n/a
import vba code conditionally import ( uncommented ) vba code and set appropriate module options 64570 npower6 yep n/a
vba constants adjust basic runtime to be 'conditionally' aware of the vba constants defined in oovbaapi module 68897 npower7 nope n/a
support dim of vba object adjust basic runtime to be 'conditionally' allow a vba compatibilty api object to be used like Dim r1 as Range 68897 npower7 nope n/a
compatible erase array in vba compatibility mode erase does not behave as expected 70380 npower7 yep n/a
null variant behaviour in vba compatibility mode operations on null variant objects behave differently 71827 npower7 yep n/a
'Like' new 'Like' operator for basic 73830 npower7 yep n/a
Module scope variable in vba module scope variables lifetime is tied to the document containing the macro n/a npower7? n/a n/a
array of array indexing in vba array of array indexing is very common, e.g. myArray(1)(2) n/a n/a n/a n/a
toolbox control event support these controls support a loose binding where the presence of a macro event handler of the form controlname_xxx e.g. controlname_click results in the appropriate macro getting called when the associated action takes place n/a n/a n/a n/a
toolbox control event support these controls support a loose binding where the presence of a macro event handler of the form controlname_xxx e.g. controlname_click results in the appropriate macro getting called when the associated action takes place n/a n/a n/a n/a

Broader scope tasks that need more investigation ( and splitting into smaller tasks )

How to enable the VBA model

The VBA model is enabled by default in the current ooo-build. It's enough to get, build, install, and be able to run it.

If you are using upstream OpenOffice.org and wish to play with this feature then check out and build the cws npower6 which contains the initial code drop for the oovbaapi

How to disable the VBA model

Depending on how your version of OpenOffice.org delivers the VBA feature to you, there are a number of ways to disable it.

If you have an Openoffice.org built by a linux distro ( or a windows one from Novell ) that has the vba interop feature then all you need do is navigate to the 'Tools.Options.Load/Save.VBAProperties' page and uncheck the 'Load Excelbasic code to edit' option.

If you are using an own build ( incorporating ) cws-npower6 the above method will always work. But if you built the vba interop feature as an office extension then you can of course disable or delete the vbaapi.oxt extension using the Extension Manager.

How can I help you?

We need help in a couple of areas, porting objects from the helperapi, understanding the VBA usage patterns, implementation and testing; see below.

Porting Objects from the HelperApi

What is the helperapi?

Have a look at the porting notes

Have a look at a porting example

Understanding the VBA usage patterns

VBA model is huge and we need help in understanding the VBA usage patterns for prioritizing as we progress. It's important to receive as many Microsoft Office documents with macros as possible for analysis. We are interested in documents from developers and regular people too.

Dumping the VBA from your MS Office documents


If you're worried about the sheet data you can use test-msvba from the libgsf. Download the latest test-msvba.c file and compile it with the following command:

gcc `pkg-config --libs --cflags libgsf-1` -o test-msvba test-msvba.c

Note: you need to have libgsf-devel package installed.

Now, you can dump the VBA from your documents with the following command:

find -name '*.xls' -exec sh -c 'test-msvba {} >> ~/excel-vba-stream-for-noel.bas' \;


Or you can use POLE with GUI. POLE is a portable C++ library to access structured storage. It is designed to be compatible with Microsoft structured storage, also sometimes known as OLE Compound Document.

Exporting modules from the Microsoft Excel

Open your document and edit associated macros. You can see list of modules on the left side of your screen. Use the right mouse button and select Export from the context menu. The whole module will be saved as one huge *.bas.

Where to send

If you are going to send pure VBA streams, send them directly to [Noel dot Power at Novell dot com].


The desired sequence

Add an extension to the IDL

The IDL lives in oovbaapi/org/openoffice/*, the IDL syntax can easily be gleaned from a few other IDL files. Two things are worth bearing in mind:

  • it's important that the argument names are correct, so that the MsgBox title:="hello" syntax works correctly.
  • for optional arguments, or variant types you need to use the any type

Having edited the IDL extension you need to:

  • run: build && deliver in oovbaapi (perhaps remove types.db first),

Add the extension to the implementation

The impl. code lives in sc/source/ui/vba/ - find the right header & source files. Add the method (with the correct prototype) to the header - often it's easiest to cut/paste that from the compiled C++ headers in sc/unxlngixxx/inc/org/openoffice/vba/

Then implement the method itself in the .cxx file. Run build debug=true

Running it

You did use [linkoo] right ? - in which case - just re-run soffice.bin (killall -9 soffice.bin first to ensure there is no lurking factory process).

Now of course - you want to add a new test to exercise it to ooo-build/test/macro/*. And of course loop back to the beginning. Once you've got something you want to share - a simple cvs diff -u | tee /tmp/foo.diff in ooo-build/scratch will yield a patch you can mail to Noel / the ooo-build mailing list to get your code folded in. NB. it's important you sign the JCA first.


It's extremely important to test the work which is already done. You can use test cases from the ooo-build/test/macro directory, you can extend them or you can write your own test cases.

It's a good idea to write test cases directly in the Microsoft Excel?

Accessing the VBA API from other languages

The VBA API is implemented by an UNO service, the service is accessible via a singleton. As such it is possible to use the API from any language that has an UNO binding. The main entry points to the API are certain global variables { Application, ActiveWorkbook and ActiveSheet } or methods { WorkSheets, WorkBooks } exported by the service. Using these globals as a starting point the rest of the api can be accessed.

NOTE: Beaware the idl is not ( nor won't be for the forseeable future be 'published' ) it WILL change, so if you are using a language that has a binary dependency on the interface ( like c++ & java ) then consider yourselves warned.


vba = uno.getComponentContext().getByName("/singletons/org.openoffice.vba.theGlobals")
sheet = vba.ActiveSheet
book = vba.ActiveWorkbook


import org.openoffice.vba.*;
// getComponentCtxFromSomewhere is a fictional
// routine representing acquisition of the component context
// from e.g. remote ServiceManage if the code is a remote client
// or from the XScriptContext if this were a java ScriptingFramework script 

XComponentContext ctx = getComponentCtxFromSomewhere(); 
Object oGlobs = ctx.getValueByName(
XGlobals xGlobs = AnyConverter.toObject(
                new Type(XGlobals.class), oGlobs);
XWorkBook xWBook =  xGlobs.getActiveWorkbook();
XWorkSheet xSheet = xGlobs.getActiveWorkSheet();

OO Basic

Basic allows access to the global variables automatically just like VBA does, there is no need to create any services or access any singletons to use the API

msgbox "Name of the current document is " & ActiveWorkbook.Path
Personal tools