generating window recreation macros from within a user-defined function

I have a user-defined function that imports all of the data files in a folder, and then creates a graph for each one, which works just fine. However, once I've created all these graphs, I want to Kill them and create window recreation macros for each one WITHOUT having to close each one, one at a time (there can be upwards of 50 windows, and it just takes too long). Killing the windows isn't the hard part, but generating the window recreation macros is giving me a hard time.

Here's what I've got so far to try to illustrate my idea, but this code just isn't cutting it:
Function Hide()
    String list = WinList("*",";","WIN:1")
    Variable numItems = ItemsInList(list), i
    for (i = 0; i<numItems; i+=1)
        String plot
        plot = StringFromList(i,list)
        String plotmacro
        plotmacro = WinRecreation(plot,0)
        print plotmacro // for de-bugging purposes. It successfully prints the recreation macro for each graph
        execute "MyMacro(plotmacro)"
    endfor
End

Macro MyMacro(input)
    String input
    print input
End


When I run Hide(), MyMacro throws an error, saying that it expected a string expression. What I want is to somehow get the content in the plotmacro string into MyMacro, so that this macro creates the window recreation macro for every graph that is passed to it.

Any help would be greatly appreciated. Thanks very much.
Kevin
From what I understand then, I cannot solve my problem using DoWindow, since DoWindow won't run if a function is also running? Is there another way around this issue?

What I want to be able to do is to generate all of these window recreation macros, but that could conceivably be done after the function has stopped running, correct? How might that be done?
Building on Thomas' suggestion, try the following code. On my system it creates the graph macro and kills the graph window. This was tested on an experiment where two graphs were open.

 Function Hide2()
    String list = WinList("*",";","WIN:1")
    Variable numItems = ItemsInList(list), i
    for (i = 0; i<numItems; i+=1)
        String plot
        String cmd
        String cmdKill
        plot = StringFromList(i,list)
        String plotmacro
        plotmacro = WinRecreation(plot,0)
        print plotmacro // for de-bugging purposes.
    sprintf cmd, "DoWindow/R %s", plot
    sprintf cmdKill, "DoWindow/K %s", plot
    Execute/P cmd
    Execute/P cmdKill
    endfor
End
This is REALLY close, thanks for the help. However, I still want to be able to have a graph macro associated with each of these. In other words, I want to get the exact macro that I get when I close a graph window. This gets me to the point where I have the window macro, but how can I insert each of these into their own graph recreation macro?