Page 1 of 1

Substitute selection by a file

Posted: 2010-06-25 02:40:19
by tomoyuki-kosaka
I made a macro, its simple one.

Code: Select all

$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.

Re: Substitute selection by a file

Posted: 2010-06-25 06:16:12
by Kino
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.

Re: Substitute selection by a file

Posted: 2010-06-27 18:22:08
by tomoyuki-kosaka
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.

Re: Substitute selection by a file

Posted: 2010-06-28 10:57:05
by martin
Yes, we all appreciate Kino's devotion to this forum and to helping fellow users out :)

Re: Substitute selection by a file

Posted: 2011-04-17 10:07:11
by loulesko
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: Select all

$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