Jump to content
Server Maintenance This Week. ×

Inserting image into MS Word


This topic is 5821 days old. Please don't post here. Open a new topic instead.

Recommended Posts

I have a script that inserts text from FileMaker into Word documents, making up a page per record. However, nothing I do seems to be able to make it also insert a picture on each page. I have tried using 'make new inline image' but I just get an error saying that it cannot make one!

Any ideas?

Many thanks - Anatole

My code is as follows:

set myname to "test"

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:PageTemplate.doc"



tell application "FileMaker Pro Advanced"

	tell database "ARTISTS"

		set EnqNo to cell "FullName" of current record

	end tell

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"

	save as active document file name myFinishedDoc

	close document 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

			insert text return & return & ArtName & return at end of text object of selection

			set myRange to text object of paragraph 1 of front document

			set bold of font object of myRange to true

			set name of font object of myRange to "Arial"

			set font size of font object of myRange to 20

			insert text biogTxt & return at end of text object of selection

			tell last section

			end tell

			insert break at text object of selection break type page break

		end tell

	end tell

end newpage

P.s. Sorry about the clumsy AppleScripting - I am having a bit of a fight getting into it as there seems to be little documentation outside basic use within the Finder.

As a side issues I am also unable to select the inserted text immediately after insertion to change the styling. I am sure there must be a simple way of tackling this.

Link to comment
Share on other sites

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.

thanks - Anatole

Link to comment
Share on other sites

OK - no response. Well, I won't be beat yet.

If I copy the image to the clipboard and then paste it, it works . . . but, only if I run the script as a stand-alone AppleScript. If I build the code into FileMaker it fails with an Apple Event failure - Bah!

Code enclosed - if anyone is interested!

Anatole (feeling ratty!)

set myname to "biogs"

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:NoLogo.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 myPic to cell "Photo" of current record

			set ArtName to cell "FullName" of current record

			set biogTxt to cell "Write up 1" of current record

			my newpage(myPic, ArtName, biogTxt)

			copy myPic as picture

			set i to i + 1

		end repeat

	end tell

end tell



tell application "Microsoft Word"

	type backspace selection

	save as active document file name myFinishedDoc

	close document 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

			insert text return at end of text object of selection

			insert break at text object of selection break type section break continuous

			set myRange to text object of last section of front document

			paste special myRange data type paste metafile picture

			

			insert break at text object of selection break type section break continuous

			insert text return & ArtName & 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 bold of font object of myRange to true

			set font size of font object of myRange to 20

			

			insert break at text object of selection break type section break continuous

			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 "Times"

			set bold of font object of myRange to false

			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

Edited by Guest
Link to comment
Share on other sites

Try this. Set the clipboard directly instead of using copy:

set myPic to cell "Photo" of current record

set the clipboard to myPic as picture

my newpage(myPic, ArtName, biogTxt)

[P.S. The above worked for me, but the picture in MS Word 2008 had a very bluish cast; it could be that version of Word; the clipboard looks fine, as I can see in the Finder with the Show Clipboard command, or paste anywhere else.]

Edited by Guest
Link to comment
Share on other sites

That's great Fenton. Big thanks for the hint.

That solves the immediate problem, as now it works, but I am sure there must be a way to do it without using the clipboard.

Anatole

Link to comment
Share on other sites

Yes, the clipboard is a problem, because it appears to store a copied picture as PICT,* which is a large format, like 10 times larger than a JPEG (which makes sense, as JPEG is both lossy and compressed. It depends I suppose on what the original graphic in FileMaker actually is, and what you're going to use it for once you get it into MS Word. If it's a large format in FileMaker, and you're going to print it from MS Word, then large is fine.

I also saw an option in Word to paste a link. But I imagine this is a dynamic link to a file, much like the "store as reference to file" option in FileMaker. Which may or may not work for you. I don't really know the AppleScript for Word (which is extensive, but kind of convoluted; unlike FileMaker's, which is concise, but kind of obscure :)-).

*(P.S. It's not just FileMaker. When I copy a JPEG in Preview, the clipboard also contains PICT data, as far as I can tell.)

Edited by Guest
Link to comment
Share on other sites

I think that the clipboard function has changed little since the early days of Mac OS (if at all), but you would have thought that it might be better to store images in their original format.

Unfortunately, the images that I have stored in this database are stored in inconsistent formats and sizes, as they have been pasted in by all and sundry over the years. Hence, there are no files to link to, so the only answer is to copy them out.

Incidentally, I have had to put the paste command inside a try / end try, as if there is no image in the container field, the applescript is halted by the resulting error.

Thanks again for your response - Anatole

Edited by Guest
Link to comment
Share on other sites

This topic is 5821 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.