VB.Net example for LibreOffice SDK

2019-07-25 03:07发布

问题:

The WriterDemo example uses VB.NET to interact with the UNO API. (The files are also included with the SDK download in file:///C:/Program Files/LibreOffice 5/sdk/examples/CLI/VB.NET/WriterDemo/.)

To build and run, I set up the command line environment according to https://api.libreoffice.org/docs/install.html, and then executed the Makefile using make WriterDemo.run. But it crashes on this line:

xContext = Bootstrap.bootstrap()

The error message:

Unhandled Exception: System.Runtime.InteropServices.SEHException: External component has thrown an exception.
   at cppu.bootstrap(Reference<com::sun::star::uno::XComponentContext>* )
   at uno.util.Bootstrap.bootstrap()
   at WriterDemo.Main()
make: *** [WriterDemo.run] Error -1066598274

This is a followup of the question Equivalent of Microsoft.Office.Interop.Excel for Open Office and/or Libre Office for VB.NET/Visual Studio 2017.

回答1:

Use Visual Studio instead of the Makefile.

  • Start a new Visual Basic Console Application.
  • Paste the code from WriterDemo.vb into Module1.vb. Rename Module1.vb to WriterDemo.vb by right-clicking on the file in Solution Explorer.
  • In Project Properties, change Startup object to WriterDemo.
  • Right-click on References and choose Add References. Browse to the cli folder in the SDK, for example C:\Program Files (x86)\LibreOffice 5\sdk\cli. (The files in the GAC did not work for me.)
  • Select all 5 files from that folder and press Add. Then be sure that there is a check mark in each of their boxes.
  • Modify App.config to fix the loading problem as described here.
  • To avoid a warning, set the processor architecture to x86 as described here.
  • It may also be necessary to set UNO_PATH, although it worked for me without doing this.

Now press Start in the toolbar (or Debug -> Start Debugging). If all goes well, it will start LibreOffice and insert some content into Writer.

An older example describing VB 2005 is at https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=21494.

Thanks to @Zev Spitz for hints and corrections.