[Solved] OpenOffice Automation with VB6

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
Tenebroleso
Posts: 15
Joined: Thu Jul 05, 2012 2:58 pm

[Solved] OpenOffice Automation with VB6

Post by Tenebroleso »

Hello,

I've got a problem whit the OpenOffice (Calc) Automation with VB6.

If I run my code in Debug Mode (Step by Step) or with "setVisible True" all works correctly.

If I run it in execution or with "setVisible False" I found my Document without "Courier New 9" font.

Code: Select all

    Set oServiceManager = CreateObject("com.sun.star.ServiceManager")
    Set oDesktop = oServiceManager.createInstance("com.sun.star.frame.Desktop")
    Set oDocument = oDesktop.loadComponentFromURL("private:factory/scalc", "_blank", 0, args())
    oDocument.getCurrentController.getFrame.getContainerWindow.setVisible False
...
    Cells_Select "$A:$AZ", oServiceManager, oDesktop
    Font_CourierNew oServiceManager, oDesktop
    Font_Size 9, oServiceManager, oDesktop
...
    oDocument.getCurrentController.getFrame.getContainerWindow.setVisible True

Code: Select all

Public Sub Cells_Select(sValue As String, oServiceManagerLav As Object, oDesktopLav As Object)
    Dim oFrame          As Object
    Dim oDispatcher     As Object
    Dim oArguments(0)   As Object

    Set oFrame = oDesktopLav.getCurrentFrame()
    
    Set oDispatcher = oServiceManagerLav.createInstance("com.sun.star.frame.DispatchHelper")
    
    Set oArguments(0) = oServiceManagerLav.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
    oArguments(0).Name = "ToPoint"
    oArguments(0).Value = sValue
    
    oDispatcher.executeDispatch oFrame, ".uno:GoToCell", "", 0, oArguments()
    
    DoEvents
End Sub

Code: Select all

Public Sub Font_CourierNew(oServiceManagerLav As Object, oDesktopLav As Object)
    Dim oFrame          As Object
    Dim oDispatcher     As Object
    Dim oArguments(4)   As Object

    Set oFrame = oDesktopLav.getCurrentFrame()
    
    Set oDispatcher = oServiceManagerLav.createInstance("com.sun.star.frame.DispatchHelper")
    
    Set oArguments(0) = oServiceManagerLav.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
    oArguments(0).Name = "CharFontName.StyleName"
    oArguments(0).Value = ""
    
    Set oArguments(1) = oServiceManagerLav.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
    oArguments(1).Name = "CharFontName.Pitch"
    oArguments(1).Value = 1
    
    Set oArguments(2) = oServiceManagerLav.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
    oArguments(2).Name = "CharFontName.CharSet"
    oArguments(2).Value = -1

    Set oArguments(3) = oServiceManagerLav.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
    oArguments(3).Name = "CharFontName.Family"
    oArguments(3).Value = 2

    Set oArguments(4) = oServiceManagerLav.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
    oArguments(4).Name = "CharFontName.FamilyName"
    oArguments(4).Value = "Courier New"

    oDispatcher.executeDispatch oFrame, ".uno:CharFontName", "", 0, oArguments()
    
    DoEvents
End Sub

Code: Select all

Public Sub Font_Size(lHeight As Long, oServiceManagerLav As Object, oDesktopLav As Object)
    Dim oFrame          As Object
    Dim oDispatcher     As Object
    Dim oArguments(2)   As Object

    Set oFrame = oDesktopLav.getCurrentFrame()
    
    Set oDispatcher = oServiceManagerLav.createInstance("com.sun.star.frame.DispatchHelper")
    
    Set oArguments(0) = oServiceManagerLav.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
    oArguments(0).Name = "FontHeight.Height"
    oArguments(0).Value = lHeight
    
    Set oArguments(1) = oServiceManagerLav.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
    oArguments(1).Name = "FontHeight.Prop"
    oArguments(1).Value = 100
    
    Set oArguments(2) = oServiceManagerLav.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
    oArguments(2).Name = "FontHeight.Diff"
    oArguments(2).Value = 0

    oDispatcher.executeDispatch oFrame, ".uno:FontHeight", "", 0, oArguments()
    
    DoEvents
End Sub
Thank you all! :)
Last edited by Hagar Delest on Mon Jul 16, 2012 10:01 pm, edited 1 time in total.
Reason: tagged [Solved].
OpenOffice 3.4 on Windows XP
User avatar
Villeroy
Volunteer
Posts: 31365
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: OpenOffice Automation with VB6

Post by Villeroy »

http://www.kalitech.fr/clients/doc/VB_APIOOo_en.html

Write your own program using the API instead of pasting recorded dispatches to the GUI which can not work when there is no GUI.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Tenebroleso
Posts: 15
Joined: Thu Jul 05, 2012 2:58 pm

Re: OpenOffice Automation with VB6

Post by Tenebroleso »

Villeroy wrote:Write your own program using the API instead of pasting recorded dispatches to the GUI which can not work when there is no GUI.
Sorry... How can I change Font with APIs? Can you be more specific please? Do you have an example?

Thank you!
OpenOffice 3.4 on Windows XP
User avatar
RoryOF
Moderator
Posts: 35267
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: OpenOffice Automation with VB6

Post by RoryOF »

Have you done any Googling for an answer? I've found several cases where they change font attributes via API; to change the font style ought be trivial once one has a working piece of code to change font attributes.
Apache OpenOffice 4.1.17 on Xubuntu 26.04.1 LTS
User avatar
Villeroy
Volunteer
Posts: 31365
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: OpenOffice Automation with VB6

Post by Villeroy »

Normally, you never have to do that. You simply define the right templates and styles to work with. That's how all office suites use to work since decades.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Tenebroleso
Posts: 15
Joined: Thu Jul 05, 2012 2:58 pm

Re: OpenOffice Automation with VB6

Post by Tenebroleso »

Ok, after a few days of trying I have something like this:

Code: Select all

Public Sub Font_Bold(Range As String, bBold As Boolean, oSheet As Object)
    Dim oRange      As Variant

    Set oRange = oSheet.getCellRangeByName(Range)
    If bBold Then
        oRange.CharWeight = 150 'Bold
    Else
        oRange.CharWeight = 100
    End If
    Set oRange = Nothing
End Sub


Public Sub Font_Italic(Range As String, bItalic As Boolean, oSheet As Object)
    Dim oRange      As Variant

    Set oRange = oSheet.getCellRangeByName(Range)
    If bItalic Then
        oRange.CharPosture = 2 'Italic
    Else
        oRange.CharPosture = 0
    End If
    Set oRange = Nothing
End Sub


Public Sub Font_Underline(Range As String, bUnderline As Boolean, oSheet As Object)
    Dim oRange      As Variant

    Set oRange = oSheet.getCellRangeByName(Range)
    If bUnderline Then
        oRange.CharUnderline = 1 'Underline
    Else
        oRange.CharUnderline = 0
    End If
    Set oRange = Nothing
End Sub


Public Sub Font_Select(Range As String, sFontName As String, oSheet As Object)
    Dim oRange      As Variant

    Set oRange = oSheet.getCellRangeByName(Range)
    oRange.CharFontName = sFontName
    Set oRange = Nothing
End Sub


Public Sub Font_Size(Range As String, sFontSize As Integer, oSheet As Object)
    Dim oRange      As Variant

    Set oRange = oSheet.getCellRangeByName(Range)
    oRange.CharHeight = sFontSize
    Set oRange = Nothing
End Sub
Look for these functions on the web is still very difficult and. Where can I find a complete list? Or, how can I retrieve them easily?

If you prefer... I still need to change the cell borders, background color and alignment.

Thank you all!!
OpenOffice 3.4 on Windows XP
User avatar
Villeroy
Volunteer
Posts: 31365
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: OpenOffice Automation with VB6

Post by Villeroy »

It's always the same answer:
Just ask the object. It can tell everything about itself. Install the MRI extension for a more interactive way of exploring objects.
In any case you need to be familiar with the basic hierarchy of model, frame and controller (view). Everything is well enough documented on the web and it works with many programming languages. Just don't expect anything as easy as VBA.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
rudolfo
Volunteer
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: OpenOffice Automation with VB6

Post by rudolfo »

Are you really sure you need all this? As Villeroy has mentioned before the way to do this with a minimum amount of hassle is to define your visual attributes in a named style and assign this style:

Code: Select all

 oCells = oSheet.getCellRangeByName( theRange )
 oCells.CellStyle = "YourStyleForThis"
You can save both macros (your logic) and styles (your visual presentation) in a template file.
Yes, at the end you will have to create several styles for each combination (of bold or normal, font size, background color, ..) that is used. But creating a style is cheap in terms of memory usage of the application and also in terms of time of the person who is writing the macro (because you can simply use the GUI to define the style). And after all you will have a clear separation between logic and layout (and that's the way how OpenOffice is supposed to be used actually any office suite, but unfortunately MS Office makes it so easy to use direct formatting that most people complain if the don't get where they want to get with direct formatting instead of saying good bye to direct formatting).
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
Tenebroleso
Posts: 15
Joined: Thu Jul 05, 2012 2:58 pm

Re: OpenOffice Automation with VB6

Post by Tenebroleso »

Finally the program works, thanks to you all! :)

I have a new problem now...

Code: Select all

Private sub MySub
   [...]
   MaxCol = Cells_RecoverLastColumn(oSheet) + 1 --> OK!
   Cells_DeleteColumn MaxCol, oSheet --> Don't work...
   [...]
End sub

Public Function Cells_RecoverLastColumn(oSheet As Object) As Long
    Dim oCell       As Object
    Dim oCursor     As Object
    Dim vAddress    As Variant
    
    Set oCell = oSheet.GetCellbyPosition(0, 0)
    Set oCursor = oSheet.CreateCursorByRange(oCell)
    oCursor.GotoEndOfUsedArea (True)
    Set vAddress = oCursor.RangeAddress
    Cells_RecoverLastColumn = vAddress.EndColumn
End Function

Public Sub Cells_DeleteColumn(iColumn As Long, oSheet As Object, Optional nColumns As Long = 1)
    oSheet.oColumns.removeByIndex iColumn - 1, nColumns
End Sub
OpenOffice 3.4 on Windows XP
B Marcelly
Volunteer
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: OpenOffice Automation with VB6

Post by B Marcelly »

Hi,
I have a new problem now...
New problem :arrow: new thread :!:

Code: Select all

' incorrect
oSheet.oColumns.removeByIndex

' correct
oSheet.Columns.removeByIndex
Bernard

OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
Tenebroleso
Posts: 15
Joined: Thu Jul 05, 2012 2:58 pm

Re: OpenOffice Automation with VB6

Post by Tenebroleso »

Ok, sorry. :)
OpenOffice 3.4 on Windows XP
Post Reply