Let's assume we have an OO Calc spreadsheet with an embedded text document (I did this: Insert->Object->OLE object->Text). Now I need a macro to access this embedded text document and select the text and type something, programatically.
Anyone know a macro to do this?
Thanks!!!!
Last edited by Hagar Delest on Sun Feb 27, 2011 11:01 pm, edited 1 time in total.
Reason:tagged [Solved].
I can access the embedded document on Sheet1 like this. In this example, the embedded document is the only object on Sheet1, which makes it easier to find.
oDrawPages = ThisComponent.getDrawPages
oDrawPage = oDrawPages.getByIndex(0) 'Get the DrawPage of the leftmost sheet
oOLE_Obj = oDrawPage.getByIndex(0) 'only getByIndex is available. Either know the index or loop through all objects
oEmbeddedObject = oOLE_Obj.EmbeddedObject
oComponent = oEmbeddedObject.Component 'oComponent is the Text document
oText = oComponent.Text 'oText is just like the Text object of a Writer document
oText.setString("New text")
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.
oDrawPages = ThisComponent.getDrawPages
oDrawPage = oDrawPages.getByIndex(0) 'Get the DrawPage of the leftmost sheet
For i = 0 to oDrawPage.Count - 1
oObj = oDrawPage.getByIndex(i)
If oObj.supportsService("com.sun.star.drawing.OLE2Shape") then 'test if we have an OLE object
oEmbeddedObject = oObj.EmbeddedObject
oComponent = oEmbeddedObject.Component
If oComponent.supportsService("com.sun.star.text.TextDocument") then 'checks if we have a Text document
If oComponent.NameSpace = "file:///C:/Documents%20and%20Settings/username/My%20Documents/Testdoc.odt/" then 'test for desired document
oText = oComponent.Text 'oText is just like the Text object of a Writer document
oText.setString("New text3")
end if
end if
end if
next i
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.
If you only have one text document embedded, you can remove the innermost If-Then (If oComponent.NameSpace...) and the code will still find the right object. If you have multiple text documents, then I don't see a solution at the moment.
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.
martius wrote:How can I … find by the name of the text document? I didn't insert the text document from a file.
I'm not sure what "name" you intend to use in this case. If you have multiple embedded non-file text objects, you could right-click them and use Name … to assign names to them. Then in your macro you would use oOLE_Obj.getName() to access the names you assigned.
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.7.8, iMac Intel. The locale for any menus or Calc formulas in my posts is English (USA).
Sub Main
'I assigned a name to an embedded non-file text object
' by right-click it and used Name...
Dim oOleObjects, oOleObject, oMyOleObject, oMyTextDoc as object
oOleObjects = ThisComponent.DrawPages.getByIndex(0)'Let's assume the DrawPage of the leftmost sheet
'Let's look for the object name we want (in this case: teste0)
For i = 0 To oOleObjects.Count - 1
oOleObject = oOleObjects.getByIndex(i)
If oOleObject.Name = "teste0" Then
oMyOleObject = oOleObject
End If
Next
oMyTextDoc = oMyOleObject.EmbeddedObject.Component
oMyTextDoc.Text.setString("EUREKA!!!")
End sub