OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Read OO-Text-document from VB

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
Dobi
Guest





PostPosted: Mon Nov 03, 2003 11:33 pm    Post subject: Read OO-Text-document from VB Reply with quote

Hi Smile
i did not find a thread like this with the search function, so i open a new thread
i want to read text from a dukument in OO-writer with VB.
this is, what i got:

Set objServiceManager = CreateObject("com.sun.star.ServiceManager")
Set objDesktop = objServiceManager.createInstance("com.sun.star.frame.Desktop")
Dim args()
Set objDocument = objDesktop.loadComponentFromURL(Dateiname, "_blank", 0, args)

so, the desktop shows up and loads my document ( Dateiname ), but now i want to read the text from the document.

but i want to read the text without the desktop visible, so like i can do it, when i read from microsoft word.

thanks for you help in advance

Dobi
Back to top
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Tue Nov 04, 2003 8:34 am    Post subject: Reply with quote

You are really asking two different questions.

1. How do I open a document invisible so that nothing shows up on the screen?

2. How do I obtain text out of an open Writer document?

Answer to question 1.
When you call loadComponentFromURL() pass an array of com.sun.star.beans.PropertyValue structures. These can specify details of how to open the document. Each possible property value you can pass to loadComponentFromURL is described here...

http://api.openoffice.org/docs/common/ref/com/sun/star/document/MediaDescriptor.html

So how did I find this?

The loadComponentFromURL() method is documented here...
http://api.openoffice.org/docs/common/ref/com/sun/star/frame/XComponentLoader.html

The store(), storeToURL() and storeAsURL() methods are documented here...
http://api.openoffice.org/docs/common/ref/com/sun/star/frame/XStorable.html

Both the mechanism used to open a document (loadComponentFromURL) and to save a document (store, storeToURL, storeAsURL) document that it is the MediaDescriptior that is the list of property values you can pass as arguments.

Notice that one of these arguments is the Hidden flag.

In order to create the com.sun.star.beans.PropertyValue struct in VB, see these posts to learn how...
http://www.oooforum.org/forum/viewtopic.php?t=3453
http://www.oooforum.org/forum/viewtopic.php?p=12231
http://www.oooforum.org/forum/viewtopic.php?p=12259#12259
http://www.oooforum.org/forum/viewtopic.php?t=3510

I would definitely recommend that you use the Bridge_CreateStruct mechanism to build the property values.

The basic technique will look approximately like this...

Code:
Dim oArgs(1)
oArgs(0) = oServiceManager.Bridge_CreateUnoStruct( "com.sun.star.beans.PropertyValue" )
oArgs(0).Name = "Hidden"
oArgs(0).Value = True


Even better would be something like this...

Code:
Dim oArgs(1)
oArgs(0) = createPropertyValue( "Hidden", True )

then elsewhere in your code declare the useful createPropertyValue function which you will need to use over and over and over again times without number. You should be able to figure out how to do this from the above links. I would recomment Bridge_GetStruct instead of using the CoreReflection mechanism.

Note that if you open a document hidden, you MUST close it yourself. Otherwise you leave open a document, tying up memory and other resources, and the user has no way to close it. On the document, try calling either close( True ) or dispose(). The former would be preferred, but the latter does work. During development, you might try NOT using Hidden so that you can see for yourself that the document pops up on screen, and is then closed.



Partial answer to question 2.

Once you have the document open, it is a TextDocument, described here in the online API reference...

http://api.openoffice.org/docs/common/ref/com/sun/star/text/TextDocument.html

As you can see, TextDocument implements the interface XTextDocument, described here...

http://api.openoffice.org/docs/common/ref/com/sun/star/text/XTextDocument.html

XTextDocument has a single method getText(), which you can see here...

http://api.openoffice.org/docs/common/ref/com/sun/star/text/XTextDocument.html#getText

It returns an XText interface, documented here...

http://api.openoffice.org/docs/common/ref/com/sun/star/text/XText.html

which offers the methods...

insertTextContent
removeTextContent

but it also inherits from the interface XSimpleText which provides methods

createTextCursor
createTextCursorByRange
insertString
insertControlCharacter

and XSimpleText inherits from XTextRange which provides methods

getText
getStart
getEnd
getString
setString

(Summary of our story thus far...
From the document, you can call getText(), which returns an object that has all of these methods...
insertTextContent
removeTextContent
createTextCursor
createTextCursorByRange
insertString
insertControlCharacter
getText
getStart
getEnd
getString
setString
)

Also notice on many of the above linked pages, references to the Developer's Guide which may be very helpful.
http://api.openoffice.org/docs/DevelopersGuide/Text/Text.htm#1+1+Overview

If you call createTextCursor documented here...

http://api.openoffice.org/docs/common/ref/com/sun/star/text/XSimpleText.html#createTextCursor

it returns an XTextCursor, documented here...

http://api.openoffice.org/docs/common/ref/com/sun/star/text/XTextCursor.html

This has several methods, which I won't utter here, plus the methods from its inherited interface (XTextRange) which I described above already.

Now one other crucial bit of info is from the Developer's Guide (link above) under 7.3.1 Editing Text. In a Writer document, when an XTextCursor is returned, it also includes...
XWordCursor http://api.openoffice.org/docs/common/ref/com/sun/star/text/XWordCursor.html
XSentenceCursor http://api.openoffice.org/docs/common/ref/com/sun/star/text/XSentenceCursor.html
and XParagraphCursor http://api.openoffice.org/docs/common/ref/com/sun/star/text/XParagraphCursor.html



So one technique to walk the document by paragraphs would be something like this...

Code:

Dim oArgs( ... )
oArgs( ... ) = createPropertyValue( "Hidden", True )
oDocument = ......loadComponentFromURL( ....., oArgs )
oText = oDocument.getText()
oTextCursor = oText.createTextCursor()
oTextCursor.gotoStart( False )
Do While True  ' or whateve syntax is appropriate in Visual Basic
   If Not oTextCursor.isStartOfParagraph() Then
      Exit Loop
   EndIf
   
   oTextCursor.gotoEndOfParagraph( True )
   
   cString = oTextCursor.getString()
   ' do something with cString, which is a paragraph...
   
   oTextCursor.gotoNextParagraph( False )
End Do ' or whatever syntax VB uses for loops


Note that I have not tested this code. I'm just putting it together off the top of my head based on the API reference material that I'm linking to. You must learn how to navigate this documentation, which is an art or skill unto itself. It would do no good if the documentation were easily accessible -- then just anybody could program for OOo.

I'm not real expert on the Writer API, but the general idea of the above example should work. I don't study Writer or Calc so closely because I don't find word processing or spreadsheets to be as much fun as drawing. So I mostly focus on the Draw API. Smile

Finally, on the gotoStart(), gotoNextParagraph(), etc. methods, you notice that they all need a boolean True/False parameter. What does the True or False mean? Think of it this way. When you call gotoXXX(), you are clicking the mouse insertion point at a different place in the document. When you call gotoNextParagraph(), you are effectively, clicking an invisible mouse I-beam at the start of the next paragraph. The True or False is whether a virtual SHIFT key is held down. That is, the click expanded some kind of invisible "selection" of text. This invisible selection is what the getString() method will return as a string. Now you see why I gotoNextParagraph( False), followed by gotoEndOfParagraph( True ). This selects the paragraph. (But not on the screen, and not the View selection that the user manipulates, just an internal cursor text selection.)

Hope this information helps.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
Dobi
Guest





PostPosted: Wed Nov 05, 2003 4:51 am    Post subject: Reply with quote

wow, thank you very much, you helped me a lot Smile
i think now, i can continue my project, which must be able to read some different file formats ( including OO ) Very Happy
Back to top
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group