Unable to load file to current document using loadComponentFromURL for writer

Hello Team,

I am trying to load file from disk using loadComponentFromURL .
My requirement is to load the file in the current document rather a new window.
I tried passing the second argument of loadComponentFromURL “_self” but it doesn’t work.
My Code:
xComp = xCLoader.loadComponentFromURL(path, “_self”, 0, new com.sun.star.beans.PropertyValue [0]);

PS: I tried using “_default” option but the behavior is not constant. Some times the file loads to current document and sometimes it opens up as new document.

Is there any way that i can always open the file into the current document only. ?

For next time, you might want to also always mention your OS and LO version+type preferable in the tags. Even if you think it does not matter, it’s good form. Just a friendly hint.

Hello,

without showing use your entiere code for loading,
i think i just reference some code from the macro bibel. OOME_4_0.odt

If i had to guess, i would say, you forgot the "private:factory/swriter" part in the path/url

Listing 261. Open a document into an existing frame.

Sub UseAnExistingFrame
  Dim noArgs()         'An empty array for the arguments
  Dim vDoc             'The loaded component
  Dim sURL As String   'URL of the document to load
  Dim nSearch As Long  'Search flags
  Dim sFName As String 'Frame Name
  Dim vFrame           'Document Frame
  Dim s As String      'Display string
    
  REM Search globally for this
  nSearch = com.sun.star.frame.FrameSearchFlag.GLOBAL + _
            com.sun.star.frame.FrameSearchFlag.CREATE
            
  REM I can even open a real file for this, but I don't know what files
  REM you have on your computer so I create a new Writer document instead
  sURL = "file:///andrew0/home/andy/doc1.odt"
  sURL = "private:factory/swriter"
  
  REM Create a frame with the name MyFrame rather than _default
  sFName = "MyFrame"
  vFrame = ThisComponent.CurrentController.Frame
  vDoc = vFrame.LoadComponentFromUrl(sURL, sFName, nSearch, noArgs())
  If IsNull(vDoc) Then 
    Print "Failed to create a document"
    Exit Sub
  End If
  
  REM The name of the frame is MyFrame. Note that the name has nothing
  REM to do with the title!
  sFName = vDoc.CurrentController.Frame.Name
  s = "Created document to frame " & sFName & CHR$(10)
  MsgBox s
  
  REM This time, do not allow creation; only allow an existing frame
  nSearch = com.sun.star.frame.FrameSearchFlag.Global
  sURL = "file:///andrew0/home/andy/doc2.odt"
  sURL = "private:factory/scalc"
  'sFName = "doc1 – OpenOffice.org Writer"
  vDoc = vFrame.LoadComponentFromUrl(sURL, sFName, nSearch, noArgs())
  If IsNull(vDoc) Then 
    Print "Failed to create a document"
    Exit Sub
  End If
  s = s & "Created document to frame " & sFName
  MsgBox s
End Sub

Hope that helps.


To show the community your question has been answered, click the ✓ next to the correct answer, and “upvote” by clicking on the ^ arrow of any helpful answers. These are the mechanisms for communicating the quality of the Q&A on this site. Thanks!


Have a nice day and let’s (continue to) “Be excellent to each other!”


Ask / Getting Started:

https://wiki.documentfoundation.org/Ask/Getting_Started