I can see multiple possible re-writes for this. It all depends on what is supposed to be in Recipient List at any given time.
1. Open file, loop through 1 to li_count, keep adding to file, run after loop
for li_loops = 1 to li_count
if li_loops = 1 then
li_FileNum = FileOpen("Recipient List.txt", TextMode!, Write!, Shared!, Append!)
end if
//populate ls_email_list variable from database view
FileWriteEx(li_FileNum, ls_email_list)
if li_loops = li_count then
if FileClose(li_FileNum) = 1 then
Run("notepad "+"Recipient List.txt")
end if
end if
next
2. Recipient list becomes multiple files which need creation/opening with every loop
string ls_Recipients
for li_loops = 1 to li_count
ls_Recipients = "Recipient List_" + string(li_loops) + ".txt"
li_FileNum = FileOpen(ls_Recipients, TextMode!, Write!, Shared!, Append!)
//populate ls_email_list variable from database view
FileWriteEx(li_FileNum, ls_email_list)
if FileClose(li_FileNum) = 1 then
Run("notepad " + ls_Recipients)
end if
next
3. Recipient list stays single file, but gets opened, and waits for notepad to be closed with each loop
string ls_Recipients
ls_Recipients = "Recipient List.txt"
for li_loops = 1 to li_count
li_FileNum = FileOpen(ls_Recipients, TextMode!, Write!, Shared!, Replace!)
//populate ls_email_list variable from database view
FileWriteEx(li_FileNum, ls_email_list)
if FileClose(li_FileNum) = 1 then
uo_RunAndWait.of_Run("notepad.exe " + ls_Recipients, uo_RunAndWait.SW_SHOWNORMAL)
end if
next
Arnd pointed out what's causing the "file is still open" issue. If li_count > 1, then you're opening the file multiple times, but only closing (and releasing) it once. The remaining handles that were allocated stay live until your application is closed. A PB app running in the development environment is an extension of the PB dev environment. Nothing gets released until the Dev Env gets closed (with your app as written in your original post). The above three versions of your function solve that problem, and hopefully one of them should be what you originally needed for it to work right.