nisus.com
https://www.nisus.com/forum/

Substitute selection by a file
https://www.nisus.com/forum/viewtopic.php?f=17&t=3800
Page 1 of 1

Author:  tomoyuki-kosaka [ 2010-06-25 02:40:19 ]
Post subject:  Substitute selection by a file

I made a macro, its simple one.

Code:
$doc1 = Document.active
$file = Choose File '~/Documents', 'Choose File to insert'
$doc2 = Document.open $file
$text = $doc2.allTexts
Document.setActive $doc1
$doc1.insertText $text, 'm'


The purpose of this macro is "Select region to substitute by a file".
However, it has a problem.
When the text inserted, there are unexpected texts are added to front and end of the inserted text.
Front one is ("
End one is ", "", "")

I hope to know the reason of the unexpected result and I'd like to make a macro which I expected.
Please help me.

Author:  Kino [ 2010-06-25 06:16:12 ]
Post subject:  Re: Substitute selection by a file

You get those odd characters because allTexts returns an array, something like ("main body text", "header1", "header2", "header3", "footer1", "footer2", "footer3", "endnotes", "footnotes") and some text objects in your file, e.g. endnotes, are empty.

Try to use $text = $doc2.text instead of $text = $doc2.allTexts.

Author:  tomoyuki-kosaka [ 2010-06-27 18:22:08 ]
Post subject:  Re: Substitute selection by a file

Kino, I appreciate your kindly help.
Your explanation completely understands me about the unexpected result from the macro I made.
Now, the revised macro is work fine as expected !

Thank you for your devotion to this forum.

Author:  martin [ 2010-06-28 10:57:05 ]
Post subject:  Re: Substitute selection by a file

Yes, we all appreciate Kino's devotion to this forum and to helping fellow users out :)

Author:  loulesko [ 2011-04-17 10:07:11 ]
Post subject:  Re: Substitute selection by a file

What a great macro, I made a few additions. I put in a test if the user cancels the macro exits, otherwise NWP crashes, also I have the insert file open hidden and then close after grabbing the text, and last, I use options "sa" for insert so it matches the style of the document that you are inserting into. Your preference may be different.
Code:
$doc1 = Document.active
$file = Choose File '~/Documents', 'Choose File to insert'
if !$file
   exit
end
$doc2 = Document.open $file, false
$text = $doc2.text
$doc2.close
Document.setActive $doc1
$doc1.insertText $text, 'sa'   



Thanks to tomoyuki-kosaka and kino.

Lou

Page 1 of 1 All times are UTC - 8 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/