Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Putdocx in a loop

    Hi,

    I am trying to create several word documents containing some text, data, and pictures, I am doing this using a loop but am running into issues where putdocx is not running in the loop, but runs fine on its own.

    Here is an example of the code I am running.

    version 16

    global list au rf rr

    foreach l of global list {
    putdocx clear
    putdocx begin

    putdocx paragraph, style(Title)
    putdocx text ("My document about `l'")

    putdocx textblock begin, paramode
    This is the block of text I am trying to insert.
    putdocx textblock end


    putdocx save "myfile_`l'.docx"
    }

    When I run this I get an error saying "unrecognized command 'This'" and upon tracing the program it seems the problem is that the line "putdocx textblock begin, paramode" is not running, but the previous putdocx commands ran successfully.

    However, if I run the code inside the loop like so:

    local l au

    putdocx clear
    putdocx begin

    putdocx paragraph, style(Title)
    putdocx text ("My document about `l'")

    putdocx textblock begin, paramode
    This is the block of text I am trying to insert.
    putdocx textblock end


    putdocx save "myfile_`l'.docx"

    Then the code runs through just fine.

    Does anyone have any ideas why that particular putdocx command would not execute in the loop but would run just fine outside of it?

    Thanks,
    Robert

  • #2
    putdocx textblock processes the lines between "begin" and "end" differently from the regular do-file lines, hence it has trouble to work inside a loop now. We are aware of the issue and going to try to improve its behavior in the future.

    For now, maybe try the following workaround, create a do-file put.do

    Code:
    // put.do
    version 16
    
    local l = "`1'"
    
    putdocx clear
    putdocx begin
    
    putdocx paragraph, style(Title)
    putdocx text ("My document about `l'")
    
    putdocx textblock begin, paramode
    This is the block of text I am trying to insert.
    putdocx textblock end
    
    
    putdocx save "myfile_`l'.docx", replace
    Then in the main do-file, call put.do with argument

    Code:
    global list au rf rr
    
    foreach l of global list {
        do put `l'
    }

    Comment

    Working...
    X