Copying a Paragraph from one Writer Document to Another

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
sean.odell@sun.com
Posts: 1
Joined: Tue Feb 26, 2008 4:59 pm

Copying a Paragraph from one Writer Document to Another

Post by sean.odell@sun.com »

Hi All,

I have searched these forums, google, have read Macros Explained by Andrew P., but am still not able to figure out how to copy a paragraph (preserving it's attributes) from one Document to another via a macro. A simplified description of what I am trying to do:

Open an Existing Document
Create a new Document
In the Existing Document find all of the Paragraphs with style "foo"
Copy all of the "foo" paragraphs to the new document

So far I have:

Code: Select all

Sub CopyParas

  Dim oDummy()

  'new document
  Dim oDoc     As Object
  Dim oUrl     As String
  Dim oVCurs   As Object 
  Dim oTCurs   As Object
 
  'source document
  Dim oDocSrc  As Object
  Dim oUrlSrc  As String
  Dim oParEnum As Object
  Dim oPar     As Object

  'open the source document
  oUrlSrc  = "file:///C:/foo.odt"
  oDocSrc  = StarDesktop.loadComponentFromURL(oUrlSrc, "_blank", 0, oDummy()) 
   
  'create the new document
  oUrl     = "private:factory/swriter"
  oDoc     = StarDesktop.loadComponentFromURL(oUrl, "_blank", 0, oDummy())
  oText    = oDoc.Text
  oVCurs   = oDoc.CurrentController.getViewCursor()     
  oTCurs   = oText.createTextCursorByRange(oVCurs.getStart())
 
  'loop through all of the paragraphs in the source document
  oParEnum = oDocSrc.getText().createEnumeration() 
  Do While oParEnum.hasMoreElements()

    oPar = oParEnum.nextElement()
   
    If oPar.supportsService("com.sun.star.text.Paragraph") Then
       
      If oPar.ParaStyleName = "foo" Then
     
          'we found a foo paragraph in the source, copy it to the new docuent

          MsgBox oPar.ParaStyleName, 0, "Found a foo."
         
          'the next line works, the source paragraph formatting is lost
          oDoc.Text.insertString(oTCurs, oPar.getString(), FALSE)
         
          'the next line does not work, insertTextContent does not seem to work
          'with com.sun.star.text.Paragraph
          oDoc.Text.insertTextContent(oTCurs, oPar, FALSE)

      End If

    End If

  Loop

End Sub
All works fine except for the line:

Code: Select all

oDoc.Text.insertTextContent(oTCurs, oPar, FALSE)
Any suggestions on how to copy a paragraph from one doc to another while preserving paragraph formatting?

Regards,
Sean
====
hol.sten
Volunteer
Posts: 495
Joined: Mon Oct 08, 2007 1:31 am
Location: Hamburg, Germany

Re: Copying a Paragraph from one Writer Document to Another

Post by hol.sten »

sean.odell@sun.com wrote:Any suggestions on how to copy a paragraph from one doc to another while preserving paragraph formatting?
Is this still a problem? Or has it been solved here http://www.oooforum.org/forum/viewtopic ... +paragraph?
OOo 3.2.0 on Ubuntu 10.04 • OOo 3.2.1 on Windows 7 64-bit and MS Windows XP
ms777
Volunteer
Posts: 177
Joined: Mon Oct 08, 2007 1:33 am

Re: Copying a Paragraph from one Writer Document to Another

Post by ms777 »

Hi,

as pointed out in http://www.oooforum.org/forum/viewtopic ... 702#265702 , you can use the XTransferable to copy parts of text documnets. This also works across different documents

Good luck,

ms777

Code: Select all

Sub CopyParas

  'open the source document
  Dim aProps(0) As New com.sun.star.beans.PropertyValue 
  aProps(0).Name  = "Hidden" 
  aProps(0).Value = true
  oUrlSrc  = "file:///C:/foo.odt"
  oDocSrc  = StarDesktop.loadComponentFromURL(oUrlSrc, "_blank", 0, aProps()) 
  oVCursSrc   = oDocSrc.CurrentController.getViewCursor()     
   
  'create the new document
  oUrl     = "private:factory/swriter"
  oDoc     = StarDesktop.loadComponentFromURL(oUrl, "_blank", 0, Array())
  oVCurs   = oDoc.CurrentController.getViewCursor()     

  'loop through all of the paragraphs in the source document
  oParEnum = oDocSrc.getText().createEnumeration() 
  Do While oParEnum.hasMoreElements()

    oPar = oParEnum.nextElement()
   
    If oPar.supportsService("com.sun.star.text.Paragraph") Then
       
      If oPar.ParaStyleName = "Heading 4" Then
     
          oVCursSrc.gotoRange(oPar, false)
         
          oDoc.CurrentController.insertTransferable(oDocSrc.CurrentController.getTransferable())

          oVCurs.gotoEnd(false)
          oDoc.Text.insertControlCharacter(oVCurs, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false)

      End If

    End If

  Loop
  
  oDocSrc.close(true)

End Sub
esztib
Posts: 5
Joined: Sun Jun 22, 2008 5:14 pm

Re: Copying a Paragraph from one Writer Document to Another

Post by esztib »

You guys are great!
I just spent a whole day to find a solution for this and was about to give up, when I came across this post.

Thank you so much! :lol:
OOo 2.4.X on MS Windows Vista + Ubuntu 8.x
User avatar
frozbie
Posts: 15
Joined: Thu Sep 18, 2008 10:25 am

Re: Copying a Paragraph from one Writer Document to Another

Post by frozbie »

Second the comment, I've spent too many hours trying to figure out how to copy a range from a calc spreadsheet to a different spreadsheet.

http://api.openoffice.org/docs/common/r ... plier.html

Works with spreadsheets as well as writer and implies may work with other apps within OOO as it is a data transfer interface.

In calc you just have to select the range to copy/transfer, then have selected where to paste/transfer to.
OOo 3.0.X on MS Windows Vista + Windows XP
Post Reply