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
Code: Select all
oDoc.Text.insertTextContent(oTCurs, oPar, FALSE)
Regards,
Sean
====