Template with Macro that imports existing document paragraph by paragraph

I need to create a template in Writer that, when opened as a new document (New Document Event), prompts the user for an existing document. Then it copies the existing document, paragraph by paragraph, to the new document, making necessary formatting changes as it goes along. Let me say that I am an experienced programmer and I know my a way around the document. I used to do this sort of programming in Word. I just can’t seem to figure out how to work with two documents at once in Writer.

Any help would be appreciated.

Welcome Nelson!

In general, your idea is clear, but I will ask a clarifying question.

You tagged the question with the PDF tag, but you didn’t mention exporting the document in the body of the question. At what point should this happen?

The current document (which has just been created from a template) can be easily accessed using ThisComponent (see 3.6.9.2. ThisComponent)

Ask for a filename and upload it - see 10.4. Choosing a File Using the File Dialog and 5.8.2. Load A Document From A URL

Enumerate paragraphs of the loaded document - see Listing 7.52: Enumerate paragraph level text content.

All these points can be found in the book by Andrew Pitonyak.

I guess I added the pdf tag by mistake. Sorry.

That’ll do it. It’s not exactly what I intended, but I was complicating the process. Thanks.

Sounds to me, like youre after:
open the target_doc, save it to via →File→save to to a newfile_name, and run simply

sub import_styles
    doc = thisComponent
    doc.StyleFamilies.loadStylesFromURL("file:/path/to/your/Template.ott", array())
end sub
1 Like