Hello,
I’m having trouble with keeping the formatting of the text I want to copy from several Word documents into a new Word document. The script should open a document, select a text range, put the text into a variable, open the next document, select a text range, add that text to the end of the variable, etc. After it has opened and selected all the text, it places the content of the variable into a new document. Here is the script
property type_list : {"W8BN"}
property extension_list : {"doc"}
global TCAPPulledAnswers
global newDocText
on open these_items
set newDocText to ""
repeat with i from 1 to the count of these_items
set this_item to item i of these_items
set the item_info to info for this_item
if folder of the item_info is true then
process_folder(this_item)
else
try
set this_extension to the name extension of item_info
on error
set this_extension to ""
end try
try
set this_filetype to the file type of item_info
on error
set this_filetype to ""
end try
if (folder of the item_info is false) and (alias of the item_info is false) and ((this_filetype is in the type_list) or (this_extension is in the extension_list) or (this_typeID is in typeIDs_list)) and name of item_info contains "_ak" then
process_item(this_item)
end if
end if
end repeat
tell application "Microsoft Word"
make new document at desktop
save as active document file name "TCAP.doc"
set myRange to create range active document start 0 end 0
insert text newDocText at myRange
end tell
display dialog "Change name of TCAP.doc before processing more files or you will overwrite it."
end open
-- this sub-routine processes folders
on process_folder(this_folder)
set these_items to list folder this_folder without invisibles
repeat with i from 1 to the count of these_items
set this_item to alias ((this_folder as Unicode text) & (item i of these_items))
set the item_info to info for this_item
if folder of the item_info is true then
process_folder(this_item)
else
try
set this_extension to the name extension of item_info
on error
set this_extension to ""
end try
try
set this_filetype to the file type of item_info
on error
set this_filetype to ""
end try
if (alias of the item_info is false) and ((this_filetype is in the type_list) or (this_extension is in the extension_list)) and name of item_info contains "_ak" then
process_item(this_item)
end if
end if
end repeat
end process_folder
-- this sub-routine processes files
on process_item(this_item)
tell application "Microsoft Word"
open this_item
select text object of active document
--get starting character position for text range
set selFind to find object of selection
clear formatting selFind
set content of selFind to "TCAP Test Preparation"
¬
execute find selFind wrap find find continue with match forward
set strText to content of text object of selection
properties of selection
set startOfTCAPrange to selection start of selection
collapse range text object of selection direction collapse end
--get ending character position for text range
set selFind to find object of selection
clear formatting selFind
set style of selFind to "60-AK A head"
execute find selFind find text "" wrap find find continue with find format and match forward
set strText2 to content of text object of selection
properties of selection
set endOfTCAPrange to selection start of selection
--create range in document and select it
set myRange to create range active document start startOfTCAPrange end endOfTCAPrange
select myRange --with formatted text
if selection type of selection is selection normal then
set newText4newDocTextObject to text object of selection
set newText4newDoc to content of newText4newDocTextObject
set newText4newDoc to newText4newDoc & "
"
end if
my concatenateText(newText4newDoc)
close active document saving no
end tell
end process_item
on concatenateText(TCAPPulledAnswers)
set my newDocText to newDocText & TCAPPulledAnswers & return
end concatenateText
This works, but all the formatting is gone in the new document. The text ranges I’m selecting from the old documents contain multiple styles. How can I keep the formatting of the text? I have tried putting “with formatted text” in several places but it doesn’t seem to help.
Thanks for your advice.
Also when I post in the future, how much of the script do you need to understand my problem?