HELP! Inserting text into Word X

Hi Guys and Gals

I desperately need help with this script. I am striping out data from two excel sheets and then trying to paste them into Word X any help would be really appreciated.

try
tell application “Microsoft Excel”
Quit
end tell
end try
tell application “Microsoft Excel”
Activate
set File_Open to “Macintosh HD:Users:Jason M Doyle:Desktop:New Company Backup”
Open File_Open as string
end tell
tell application “Microsoft Excel”
set sharename to Value of Range “r1c2:r82c2”
Close Workbook 1
end tell
tell application “Finder”
activate
set thefile to choose file with prompt “Please select todays shares file”
tell application “Microsoft Excel”
set thefile to thefile as string
tell Application “Finder”
select file thefile
open selection
end tell
Delete Row 1
Delete Row 1
Delete Row 1
set thepricelist to Value of Range “r1c4:r82c4”
set thedifflist to Value of Range “r1c3:r82c3”
Close Workbook 1 saving No
Quit
end tell
repeat with i from 1 to 82
set item i of thepricelist to item i of thepricelist as number
set item i of thepricelist to (round ((item i of thepricelist) * 10)) / 10
set item i of thedifflist to item i of thedifflist as number
if item i of thedifflist > 0 then
set item i of thedifflist to “+” & item i of thedifflist
end if
end repeat
end tell
tell application “Microsoft Word”
activate
– do Visual Basic " Documents.Add"
do Visual Basic " Selection.ParagraphFormat.TabStops.Add Position:=InchesToPoints(2.5), _
Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces"
do Visual Basic " Selection.ParagraphFormat.TabStops.Add Position:=InchesToPoints(3.75), _
Alignment:=wdAlignTabright, Leader:=wdTabLeaderSpaces"
insert text (“FOR real time shares, just call 0906 360 9300. Calls cost 60p/min” & return & return) at after last character
repeat with i from 1 to 82
set compDet to {item i of sharename, " ", item i of thepricelist, " ", item i of thedifflist, "
"}
insert text (“compDet” & return & return) at after last character

tell application “Finder”
activate
end tell
end repeat
end tell
tell application “Microsoft Word”
activate
end tell
tell application “Finder”
activate
display dialog “Share Text Completed - save document as word 4 format” buttons “OK” default button 1
end tell

The part I am having problems involves inserting the text (I’ve highlighted this in bold) and for the life of me I can’t think of how to do it.

Hi,

you should refer to the front document


.
tell application "Microsoft Word"
	activate
	tell document 1
		-- do Visual Basic "    Documents.Add"
		do Visual Basic "    Selection.ParagraphFormat.TabStops.Add Position:=InchesToPoints(2.5),  _
        Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces"
		do Visual Basic "    Selection.ParagraphFormat.TabStops.Add Position:=InchesToPoints(3.75),  _
        Alignment:=wdAlignTabright, Leader:=wdTabLeaderSpaces"
		insert text ("FOR real time shares, just call 0906 360 9300. Calls cost 60p/min" & return & return) at after last character
		repeat with i from 1 to 82
			set compDet to {item i of sharename, "    ", item i of thepricelist, "    ", item i of thedifflist, ""}
			insert text ("compDet" & return & return) at after last character
		end repeat
	end tell
end tell
.

Note; I don’t know, what all these activate commands are for, they are not needed

Hi

Thanks for your advice I have moved the script onto a mac running 10.3.9 and altered the script accordingly removing the activates as advised the trouble I am having is in setting the contents of selection within a repeat. I am so close I can smell victory can anyone help?

try
tell application “Microsoft Excel”
Quit
end tell
end try
tell application “Microsoft Excel”
set CompanyBackup to “Macintosh HD:Users:editorial_user:Desktop:New Company Backup”
Open CompanyBackup
end tell
tell application “Microsoft Excel”
set sharename to Value of Range “r1c2:r82c2”
– Close Workbook 1
end tell
tell application “Finder”
set thefile to choose file with prompt “Please select todays shares file”
tell application “Microsoft Excel”
set thefile to thefile as string
tell Application “Finder”
select file thefile
open selection
end tell
Delete Row 1
Delete Row 1
Delete Row 1
set thepricelist to Value of Range “r1c4:r82c4”
set thedifflist to Value of Range “r1c3:r82c3”
Close Workbook 1 saving No
Quit
end tell
repeat with i from 1 to 82
set item i of thepricelist to item i of thepricelist as number
set item i of thepricelist to (round ((item i of thepricelist) * 10)) / 10
set item i of thedifflist to item i of thedifflist as number
if item i of thedifflist > 0 then
set item i of thedifflist to “+” & item i of thedifflist
end if
end repeat
end tell
tell application “Microsoft Word”
(– do Visual Basic " Documents.Add"
do Visual Basic " Selection.ParagraphFormat.TabStops.Add Position:=InchesToPoints(2.5), _
Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces"
do Visual Basic " Selection.ParagraphFormat.TabStops.Add Position:=InchesToPoints(3.75), _
Alignment:=wdAlignTabright, Leader:=wdTabLeaderSpaces"
)
set contents of selection to “FOR real time shares, just call 0906 360 9300. Calls cost 60p/min” & return & return

This section below is where I receive the error telling me that it is unable to set the contents selection to the variable I have as a string

repeat with i from 1 to 82
set item i of sharename to item i of sharename as string
set compDet to (item i of sharename) & tab & (item i of thepricelist) & tab & (item i of thedifflist)
set contents of selection to compDet & return
tell application “Finder”
end tell
end repeat
end tell
tell application “Microsoft Word”
end tell
tell application “Finder”
display dialog “Share Text Completed - save document as word 4 format” buttons “OK” default button 1
end tell

Remove also the wrapping tell / end tell lines, they are completely useless.
You didn’t consider to refer to document 1!

tell application "Microsoft Word"
   activate
   tell document 1
      -- do something with document 1
   end tell
end tell

Hi Stefan

I added the reference to the front document but to no avail. This scripting lark is beginning to wear me down.

I’m sorry, I don’t use Word v.X any more and there are some
significant differences between v.X and 2004/2008

Thats ok Stefan thanks for your help though I really appreciate it I hope someone out there is still using legacy or outdated software like my company uses.

Can anyone else offer any advice on the following

tell application “Microsoft Word”
(– do Visual Basic " Documents.Add"
do Visual Basic " Selection.ParagraphFormat.TabStops.Add Position:=InchesToPoints(2.5), _
Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces"
do Visual Basic " Selection.ParagraphFormat.TabStops.Add Position:=InchesToPoints(3.75), _
Alignment:=wdAlignTabright, Leader:=wdTabLeaderSpaces"
)
set contents of selection to “FOR real time shares, just call 0906 360 9300. Calls cost 60p/min” & return & return

This section below is where I receive the error telling me that it is unable to set the contents selection to the variable I have as a string

repeat with i from 1 to 82
set item i of sharename to item i of sharename as string
set compDet to (item i of sharename) & tab & (item i of thepricelist) & tab & (item i of thedifflist)
set contents of selection to compDet & return
tell application “Finder”
end tell
end repeat
end tell
tell application “Microsoft Word”
end tell
tell application “Finder”
display dialog “Share Text Completed - save document as word 4 format” buttons “OK” default button 1
end tell

Hi,

this works with Word / Excel 2004
maybe it helps for v.X. Probably you have to “rename” the range terminology


set File_Open to ((path to desktop as text) & "New Company Backup.xls")
tell application "Microsoft Excel"
	open File_Open as alias
	set sharename to value of range "B1:B82" -- "r1c2:r82c2"
	close workbook 1
end tell

set thefile to choose file with prompt "Please select todays shares file"

tell application "Microsoft Excel"
	open thefile
	delete row 1
	delete row 1
	delete row 1
	set thepricelist to value of range "D1:D82" -- "r1c4:r82c4"
	set thedifflist to value of range "C1:C82" --"r1c3:r82c3"
	close workbook 1 saving no
	quit
end tell

repeat with i from 1 to 82
	set item i of thepricelist to item i of thepricelist as number
	set item i of thepricelist to (round ((item i of thepricelist) * 10)) / 10
	set item i of thedifflist to item i of thedifflist as number
	if item i of thedifflist > 0 then
		set item i of thedifflist to "+" & item i of thedifflist
	end if
end repeat

tell application "Microsoft Word"
	activate
	make new document
	tell document 1
		-- do Visual Basic "    Documents.Add"
		do Visual Basic "    Selection.ParagraphFormat.TabStops.Add Position:=InchesToPoints(2.5),  _
        Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces"
		do Visual Basic "    Selection.ParagraphFormat.TabStops.Add Position:=InchesToPoints(3.75),  _
        Alignment:=wdAlignTabright, Leader:=wdTabLeaderSpaces"
		
		insert text ("FOR real time shares, just call 0906 360 9300. Calls cost 60p/min" & return & return) at end of text object
		repeat with i from 1 to 82
			set compDet to (item i of sharename as text) & tab & item i of thepricelist & tab & item i of thedifflist
			insert text (compDet & return & return) at end of text object
		end repeat
	end tell
end tell

tell application "Finder"
	activate
	display dialog "Share Text Completed - save document as word 4 format" buttons "OK" default button 1
end tell

Unfortunately the insert text isn’t a viable command. I have decided to use textedit instead of word but am having problems with it I have posted a separate query about it as I am having probs with the repeat sequence. Perhaps you could have a look and advise me where I am going wrong.

Although I get a VB error message (probably because of the line breaks),
this works on Panther with Word/Excel v.X


.
set theText to ("FOR real time shares, just call 0906 360 9300. Calls cost 60p/min" & return & return)
repeat with i from 1 to 82
	set compDet to (item i of sharename as text) & tab & (item i of the thepricelist) & tab & (item i of the thedifflist)
	set theText to theText & compDet & return & return
end repeat

tell application "Microsoft Word"
	activate
	tell document 1
		-- do Visual Basic "    Documents.Add"
		do Visual Basic "    Selection.ParagraphFormat.TabStops.Add Position:=InchesToPoints(2.5),  _
        Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces"
		do Visual Basic "    Selection.ParagraphFormat.TabStops.Add Position:=InchesToPoints(3.75),  _
        Alignment:=wdAlignTabright, Leader:=wdTabLeaderSpaces"
		set text 1 to theText
	end tell
end tell
.

I receive an error saying Word cannot set text 1 of document 1 to "FOR real time shares, just call 0906 360 9300. Calls cost 60p/min

… it then give the list of compdet data in the error msg

Now I’m stumped

then try this, I installed Office v.X on my old TiBook and the script worked


.
tell application "Microsoft Word"
   activate
   make new document
   tell document 1
.

Afraid no luck Stefan I think the only option is to copy and paste it from textedit into word and then set the tabs. Can I say again how much I appreciate your help digging out old hardware and installing ancient software is going beyond the call of duty. If you could help with the copying and pasting I will bother you no longer.

Have you also tried the suggestion of Jacques?

With the visualbasic I receive the error ambiguous name detected: TmpDDE