Ask Your Question
1

How to change "ThisComponent"? [closed]

asked 2013-05-08 16:36:18 +0200

Gaffer gravatar image

I want to create a Write document from a Calc macro.

The actual creation works fine but "ThisComponent" still points to the Calc document, so I can't insert any text into my new Write document. I have tried to bring the Write document to the front, and tried to make it active in two different ways but ThisComponent doesn't change.

Can anyone point to my error?

Sub Main
 Dim oFF           REM FilterFactory service

  oFF = createUnoService( "com.sun.star.document.FilterFactory" )

  REM Create a Write doc
  oDoc = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0, Array() )

  REM bring the writer doc to the front.  This seems to happen anyway.
  oDoc.CurrentController.Frame.ContainerWindow.toFront()

  REM Try to make the writer doc active one way
  StarDesktop.setActiveFrame(oDoc.CurrentController.Frame)

  REM Try to make the writer doc active another way
  oDoc.CurrentController.Frame.Activate()

  REM I would hope that by now, ThisComponent is a TextDocument
  If ThisComponent.supportsService("com.sun.star.text.TextDocument") Then
    MsgBox "Success - this is a TextDocument!"
  else
    REM But it isnt :-(
    MsgBox "Failed :-("
  End If

  REM try writing some text into the document.  This silently fails because we are not in the text document.
  fnDispatch("InsertText", array("Text", "The quick brown fox jumped over the lazy dogs." ))

End Sub
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Alex Kemp
close date 2015-10-31 16:16:53.112278

1 Answer

Sort by » oldest newest most voted
2

answered 2013-05-09 12:48:01 +0200

newOOo3 gravatar image

Hi,Gaffer

I think when you write the macro in Calc Document, " ThisComponent" means Calc document. So, "ThisComponent.supportsService("com.sun.star.text.TextDocument")" will return false. I guess your correct code mean following ;

Sub Main
Dim oDoc as Object, oText as Object, oInsertText as String
    oDoc = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0, Array())
    oText = oDoc.getText()
    oInsrtText = "The quick brown fox jumped over the lazy dogs."
    oText.insertString(oText.getStart(), oInsrtText , false)
End Sub
edit flag offensive delete link more

Comments

Many thanks. Your example has set me in the right direction. That is very helpful.

Gaffer gravatar imageGaffer ( 2013-05-09 20:19:25 +0200 )edit

Question Tools

1 follower

Stats

Asked: 2013-05-08 16:36:18 +0200

Seen: 1,973 times

Last updated: May 09 '13