Jump to content
Salesforce and other SMB Solutions are coming soon. ×

Applescripting word documents from FileMaker Pro


TerribleToll

Recommended Posts

I have ported a client solution over from FP6 to FP9 and it is all working fairly well except for a group of scripts that generate Word documents and populate them with data from FileMaker.

 

Basically the Applescript runs OK but gets stuck in the important bit in the middle where it is setting the data from the filemaker fields and returns an error of "Microsoft Word got an error: Can't set selection of document to "sampledata".

 

Now my AppleScript is pretty basic and my understanding of how it works with MS word is even less. So far I have tweaked it to work using FP9, but only enough to generate a blank document with the above error.

 

Any ideas - I've been bashing my head against a wall on this all day.

 

many thanks - Anatole

 

 

 

set myname to "testfile"
set myFinishedDoc to (path to desktop as string) & myname & ".doc"
set x to 0 as integer
repeat
if not (exists file myFinishedDoc of application "Finder") then
	exit repeat
else
	set x to x + 1
	set myFinishedDoc to (path to desktop as string) & myname & x & ".doc"
end if
end repeat

set MyDoc to (path to preferences folder as string) & "FileMaker Preferences:template.doc"
tell application "FileMaker Pro Advanced"
tell database "ARTISTS"
	set EnqNo to cell "FullName" of current record
end tell
end tell

tell application "Finder"
if (exists file myFinishedDoc) then
	activate me
	display dialog "You have already created a document called:" & return & return & myFinishedDoc buttons {"Overwrite", " Cancel "} default button " Cancel "
	set userResponse to the button returned of the result
	if userResponse is " Cancel " then return false
end if
end tell

tell application "Microsoft Word"
open MyDoc
end tell

tell application "FileMaker Pro Advanced"
set x to count record of document "ARTISTS"
tell document "ARTISTS"
	set i to 1
	repeat while i < (x + 1)
		go to record i
		set ArtName to cell "FullName" of current record
		set biogTxt to cell "Write up 1" of current record
		set myPic to cell "Photo" of current record
		my newpage(myPic, ArtName, biogTxt)
		set i to i + 1
	end repeat
end tell
end tell

tell application "Microsoft Word"
delete last character
close front document saving in myFinishedDoc
end tell

display dialog "A biogs doc has been saved to the desktop" with icon 1 buttons {"OK"} default button 1

on newpage(myPic, ArtName, biogTxt)
tell application "Microsoft Word"
	tell front document
		try
			set selection to myPic
		end try
		do Visual Basic "	Selection.InsertBreak Type:=wdSectionBreakContinuous"
		set selection to return & ArtName
		tell last section
			set font size to 20
		end tell
		do Visual Basic "	Selection.InsertBreak Type:=wdSectionBreakContinuous"
		set selection to return & biogTxt
		tell last section
			set font size to 10
		end tell
		set selection to ASCII character 12
	end tell
end tell
end newpage

Link to comment
Share on other sites

  • 2 weeks later...

OK - I've figured out how to get the text in. The looping routine at the end can be replace with the code below. However, try as I might I can't get it to put in the picture (myPic). Insert only seems to want to work with text objects - any ideas out there? I could use a VBasic line, but I don;t know how to interpret the contents of myPic for the VBasic code

 

thanks for any help, hints or just plain abuse

 

Anatole

 

 

on newpage(myPic, ArtName, biogTxt)
tell application "Microsoft Word"
	tell front document
		insert text return & return & return at end of text object of selection
		insert text ArtName & return at end of text object of selection
		insert text biogTxt & return at end of text object of selection
		set myRange to text object of last section of front document
		     set name of font object of myRange to "Arial"
		set font size of font object of myRange to 10
		insert break at text object of selection break type page break
	end tell
end tell
end newpage

Link to comment
Share on other sites

OK, this is the problem as simply as I can put it:

 

The script above transfers data out of my FileMaker database to create nice little Word Documents. The AppleScript I am using sets the text in FileMaker fields to AppleScript variables and then uses 'set selection' to put the data onto the Word document page. However, this process doesn't work for picture files (even if I specify 'as picture').

 

I have also tried embedding segments of VBscript which works for simple commands like:

do Visual Basic "Selection.InsertBreak Type:=wdSectionBreakContinuous"

 

But if I try this method to place the picture:

do Visual Basic "ActiveDocument.Shapes.AddPicture (\" & myPic & \")"

I just get an Word 5152 error saying that myPic is not a valid filename.

 

Note: The (\" & x & \") notation is an AppleScript thing to transfer the AppleScript variable to VBscript - I have been led to believe. However, I get the same error without it so it may be wrong.

I am sure this method should work, but I know that my syntax has slipped up somewhere. Any ideas or hints would be much appreciated? There are so few examples that I can find of the web of AppleScripting with Word.

 

Anatole

Link to comment
Share on other sites

Right - now, with a little help I have it working by copying the picture to the clipboard and then pasting it into Word. It is a bit clumsy and slow, but at least it works.

 

I am still sure there is a way to do it without using the clipboard.

 

Anatole

Link to comment
Share on other sites



×
×
  • Create New...

Important Information

Terms of Use