Script to Open MS Word and execute a VB macro

I am having trouble opening MS Word (2004) and executing an existing macro using Applescript. If I place the VB code in ""s the script will not compile as …

Tell Application “Microsoft Word”
do visual basic “my VB code goes in here …”
end tell

or

Tell Application “Microsoft Word”
do visual basic “Execute(MyMacro)”
end tell

I get an error message like Can’t continue <>.

How can I execute this macro from Applescript?
Thanks

I’m having similar problems.

I also tried to take the VB and translate it into AppleScript but this isn’t working out for me because the relavent code examples from Microsoft’s 526 page “Word 2004 AppleScript Reference” don’t work. The editor seems to have no problem with the syntax of anything vaguely resembling good code but you get errors at runtime. The URL for the guide is here:

http://www.microsoft.com/mac/resources/resources.aspx?pid=asforoffice

the Word 2004 AS dictionary lists the following command:

run VB macro: Runs a Visual Basic macro.
run VB macro
macro name Unicode text – The name of the VB macro to run.

I created a quick macro named TestMacro and this works fine for me:

tell application "Microsoft Word"
	run VB macro macro name "TestMacro"
end tell

Doing a search within the Word 2004 AppleScript Reference returns similar material on page 324. Where are you guys getting your examples from?

-N

My situation is this: I was given some VBA code which was written for use on Windows and I have to get the same function going for the Mac users. This could either be done by getting the actual VB code to run on the Mac or by taking what the script is doing and rewriting it in AppleScript. When I tried the first option, I got the error message “Sub or function not defined.” I then found out (I hope I have this straight) that the guy who wrote it had to do two different versions, one for Office 2000 and another for Office XP, because the same code would not work in both places. Given that, I decided to try the second option.

It’s actually a relatively simple part of a script which has to insert some text in the footer at each page of the document (based on other things going on in the script). At this point, I just want to get to the point of being able to insert text into a footer in a test document. The thing is that I tried the following bits of code straight from the reference p. 125-126:

–first example
set s1 to section 1 of active document
set content of text object of (get footer s1 index header footer primary) to “Footer text”

–second example
set different first page header footer of page setup of active document ¬
to true
insert text “Written by Joe Smith” at text object of (get footer section 1 of active document index header footer first page)

They both compile but give vague error messages at runtime like “<> id 131074 of <> 1 of <<class 1003>> of application “Microsoft Word” doesn’t understand the <> message.”

Here’s the full text of my test script:

on open files_dragged_onto_this_script
repeat with each_file in files_dragged_onto_this_script

	tell application "Microsoft Word"
		activate
		open each_file
		set read only of each_file to false

		set s1 to section 1 of active document
		set content of text object of (get footer s1 index header footer primary) to "Footer text"
		--insert text "end of document" at end of text object of active document
		--The code in the above comment does work.
	end tell

end repeat

end open

Fingal,

On 10.3.6 and Word 11.1 - This works for me:

tell application "Microsoft Word"
	set s1 to section 1 of active document
	set content of text object of (get footer s1 index header footer primary) to "Footer Text"
end tell

and so does this:

tell application "Microsoft Word"
	set different first page header footer of page setup of active document ¬
		to true
	insert text "Written by Joe Smith" at text object of (get footer section 1 of active document index header footer first page)
	end tell

and your sample droplet of:

on open files_dragged_onto_this_script
	repeat with each_file in files_dragged_onto_this_script
		
		tell application "Microsoft Word"
			activate
			open each_file
			set read only of each_file to false
			
			set s1 to section 1 of active document
			set content of text object of (get footer s1 index header footer primary) to "Footer text"
			insert text "end of document" at end of text object of active document
			--The code in the above comment does work.
		end tell
		
	end repeat
end open

works also. (Creates “Footer Text” in the footer and “end of document” at the end of text in the document body)

Hope this helps you narrow it down-

-N

OK, this is odd but it makes it look like the problem is something besides the code. I am using Word 11.1. It’s on 10.3.9 rather than 10.3.6 but I don’t think that’s too likely to be the problem even though I just tried it on another machine with Word 11.1 and 10.3.9 and got exactly the same error message as I mentioned before. Every copy of Office 2004 in the department was installed from the same academic bulk-license disk so I suppose the problem could be there. Maybe some component doesn’t get installed by default from that disk. It would be annoying if that’s the problem because it means that I would have to go around and fix this on about 20 machines.

Thank you, nedlow99.

After some more investigation, I found out what the problem was or, at least, I got it working and have a good theory why there had been a problem. It turns out that I had an additional (screwed up) install of Office v.X at the root level of the hard drive (not the Applications folder where it is normally found). The Remove Office tool can’t see this copy and didn’t remove it when I upgraded to Office 2004. It looks like the AppleScript was being compiled with the old copy of Word X as the target but then tried to control Word 2004 when it ran. The problem persisted when I copied the compiled script to another machine, even one that never had older copies of Office on it. The solution was to recompile the script on a clean machine. Now it works.

I don’t know if this is what I need to do (open Word and execute VB) but I do need to open about 1,500 Word docs and insert a page (another 1-page Word document) into each document as first page.

So page one of all the 1500 Word docs needs to be this other 1 page Word doc.

Can this happen?

Thnx

Oh, and BTW, I just found out that the 1500 docs are protected (in Word).

I’ve gotten this far, which is so close. Just having difficulties placing the doc into the BEGINNING of the doc. It’s placing it about a 1/4 of the way down.

Help Pleeeease.

set wordDoc to (choose file with prompt "Choose file")

tell application "Microsoft Word"
	
	activate
	
	open wordDoc
	
	unprotect active document password "jh"
	
	insert file at text object of selection file name "Macintosh HD:Users:jmhaynes:Desktop:WordTest:How to Fill Out a Copy Template.doc" with link
	protect active document protection type allow only form fields password "jesse" with no reset
	close active document saving yes
	
end tell