Enumerate bookmarks into a table using VB.Net

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Stojilcoviz
Posts: 4
Joined: Mon Aug 24, 2009 6:23 pm

Enumerate bookmarks into a table using VB.Net

Post by Stojilcoviz »

Hello,

I'm pretty new to OpenOffice UNO programming but I've to build an application (using VB.NET) that opens a Writer template file wich contains some bookmarks and replace the bookmark text with some data from a data source.
So far I've been able to do most of the work but now I'm facing a problem I don't know how to solve.
Some of the bookmarks in the document may be in the first row of a table and I've to repeat that row filling the bookmark with the appropriate data. The problem is I've not find a way to tell if a bookmark is placed inside a table or inside simple text. Alternatively I've looked for a way to enumerate the bookmark that may be present in a table-cell but without any result.

Does anybody knows a solution to this?

Thank you in advance.
OpenOffice 3.1 on Windows Vista
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Enumerate bookmarks into a table using VB.Net

Post by FJCC »

I used the MRI tool to look at bookmarks in the text body and in a table. Here are the first few properties of the Anchor of a bookmark in the text body

Code: Select all

(Name)                           (Value Type)               (Value)          (Info.)   (Attr.)             (Handle)  
AvailableServiceNames            []string                   -SEQUENCE-       Pseud     ReadOnly            154  
BorderDistance                   long                       0                          Maybevoid             0  
BottomBorder                     .table.BorderLine          -STRUCT-                   Maybevoid             1  
BottomBorderDistance             long                       0                          Maybevoid             2  
BreakType                        .style.BreakType           NONE                       Maybevoid             3  
Cell                             .table.XCell               -void-                     ReadOnly,Maybevoid    4  
and here is the same for the bookmark Anchor in a table

Code: Select all

(Name)                           (Value Type)               (Value)          (Info.)   (Attr.)             (Handle)  
AvailableServiceNames            []string                   -SEQUENCE-       Pseud     ReadOnly            154  
BorderDistance                   long                       0                          Maybevoid             0  
BottomBorder                     .table.BorderLine          -STRUCT-                   Maybevoid             1  
BottomBorderDistance             long                       0                          Maybevoid             2  
BreakType                        .style.BreakType           NONE                       Maybevoid             3  
Cell                             .table.XCell               -INTERFACE-                ReadOnly,Maybevoid    4  
Notice that the Cell property changes from Void to Interface. Looking at the Cell object properties I see

Code: Select all

(Name)                 (Value Type)               (Value)              (Info.)   (Attr.)             (Handle)  
BackColor              long                       -1                                                   0  
BackGraphicFilter      string                     ""                                                   1  
BackGraphicLocation    .style.GraphicLocation     NONE                                                 2  
BackGraphicURL         string                     ""                                                   3  
BackTransparent        boolean                    True                                                 4  
BorderDistance         long                       97                                                   5  
BottomBorder           .table.BorderLine          -STRUCT-                                             6  
BottomBorderDistance   long                       97                                                   7  
CellName               string                     A1                             ReadOnly              8  
ElementType            type                       -TYPE-               Pseud     ReadOnly             31  
End                    .text.XTextRange           -INTERFACE-          Pseud     ReadOnly             36  
EndRedline             [].beans.PropertyValue     -void-                         ReadOnly,Maybevoid    9  
Error                  long                       0                    Pseud     ReadOnly             27  
Formula                string                     ""                   Pseud                          24  
ImplementationId       []byte                     -SEQUENCE-           Pseud     ReadOnly             33  
ImplementationName     string                     SwXCell              Pseud     ReadOnly             28  
IsProtected            boolean                    False                                               10  
LeftBorder             .table.BorderLine          -STRUCT-                                            11  
LeftBorderDistance     long                       97                                                  12  
NumberFormat           long                       256                            Maybevoid            13  
PropertySetInfo        .beans.XPropertySetInfo    -INTERFACE-          Pseud     ReadOnly             30  
RightBorder            .table.BorderLine          -STRUCT-                                            14  
RightBorderDistance    long                       97                                                  15  
RowSpan                long                       1                                                   16  
Start                  .text.XTextRange           -INTERFACE-          Pseud     ReadOnly             35  
StartRedline           [].beans.PropertyValue     -void-                         ReadOnly,Maybevoid   17  
String                 string                     Text in marked cell  Pseud                          37  
SupportedServiceNames  []string                   -SEQUENCE-           Pseud     ReadOnly             29  
Text                   .text.XText                -INTERFACE-          Pseud     ReadOnly             34  
TextSection            .text.XTextSection         -void-                         ReadOnly,Maybevoid   18  
TopBorder              .table.BorderLine          -STRUCT-                                            19  
TopBorderDistance      long                       97                                                  20  
Type                   .table.CellContentType     TEXT                 Pseud     ReadOnly             26  
Types                  []type                     -SEQUENCE-           Pseud     ReadOnly             32  
UserDefinedAttributes  .container.XNameContainer  -INTERFACE-                    Maybevoid            21  
Value                  double                     0.0                  Pseud                          25  
VertOrient             short                      0                                                   22  
WritingMode            short                      4                                                   23  
which includes the Cell Name (A1 in this case) and the text I put in the cell "Text in marked cell". I hope that gets you started solving your problem.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Stojilcoviz
Posts: 4
Joined: Mon Aug 24, 2009 6:23 pm

Re: Enumerate bookmarks into a table using VB.Net

Post by Stojilcoviz »

Thank you FJCC,

I've advanced in my project but still haven't solved the issue.
Using MRI I've gained access to the content of table cells and it seems to me that solution may be near but I can't still grasp it.
I can now inspect cell's content but I've no way to tell if content is a bookmark or simple text...
Here's some of my code:

Code: Select all

        Private Shared Function TableHasBookmarks(ByVal table As unoidl.com.sun.star.text.XTextTable) As Boolean
            Dim xTableRows As unoidl.com.sun.star.table.XTableRows = table.getRows

            If xTableRows.getCount > 0 Then
                Dim xCols As unoidl.com.sun.star.table.XTableColumns = table.getColumns()
                For k As Integer = 0 To xCols.getCount - 1
                    Dim xCell As unoidl.com.sun.star.table.XCell = table.getCellByName(String.Format("{0}{1}", Chr(Asc("A") + k), 1))
                    Dim xAccess As unoidl.com.sun.star.container.XEnumerationAccess = DirectCast(xCell, unoidl.com.sun.star.container.XEnumerationAccess)
                    Dim enumaration As unoidl.com.sun.star.container.XEnumeration = xAccess.createEnumeration()
                    Dim Conta As Integer = 0
                    Dim xTextRange As unoidl.com.sun.star.text.XTextRange
                    Dim field As uno.Any
                    While enumaration.hasMoreElements()
                        field = enumaration.nextElement()
                        xTextRange = DirectCast(field.Value, unoidl.com.sun.star.text.XTextRange) 'This gets cell content
                        exit while
                    End While
                        xAccess = DirectCast(xTextRange, unoidl.com.sun.star.container.XEnumerationAccess)
                        enumaration = xAccess.createEnumeration()
                        While enumaration.hasMoreElements() 'This way I get the element in the cell
                            field = enumaration.nextElement()
                            Dim Content As unoidl.com.sun.star.text.XTextContent = DirectCast(field.Value, unoidl.com.sun.star.text.XTextContent)
                            Dim xBookmarkRange As unoidl.com.sun.star.text.XTextRange = Content.getAnchor()
                            ' At this point I've no way to tell if field.value is a bookmark or simple text....
                        End While
                Next
            Else
                TableHasBookmarks = False
            End If
        End Function
It seems to me the that I could solve the problem using the TextPortion and TextPortionEnumeration services but I can't find any sample on that and I don't know how to use them.

Ty.
OpenOffice 3.1 on Windows Vista
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Enumerate bookmarks into a table using VB.Net

Post by FJCC »

As I understand it, you are going to the table cell and then trying to discern whether there is a bookmark there. I was coming at your problem from the other direction, going to the bookmark and then determining whether it is in a table. I understood from you first post that would be an acceptable approach. The Anchor of the bookmark has both the Cell property and a TextTable property that contains information about the entire table. I think that using those two properties you can find out all you need to know to manipulate the contents of the table.
I have tried to find a way to look at the table or cell first and see if it has a bookmark, but I don't see it. I'm no expert, so there may well be a way.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Post Reply