Applescript to run a VB Macro in Folders and sub folders

Hi Guys,

Using Applescript, I am trying to run a VB macro in all MS Word Files (doc, docx) of Selected folder and all subfolders and to save as all the file as Plaint Text format.

But What I get the result is Original file is Saved as Text format. And the Macro is runned on all original Documents and stay opening.

Below Script for your reference.

set folderToProcess to (choose folder with prompt "Choose Folder::")

tell application "Microsoft Word"
	close every document saving no
end tell

tell application "Finder"
	activate
	set fileExt to {".doc", ".docx"}
	
	set theTopFolder to (folderToProcess as alias)
	repeat with EachFile in (get every file of folder (folderToProcess as alias))
		
		set FileName to EachFile as text
		try
			copy name of EachFile as string to FileName
			repeat with ext in fileExt
				if FileName ends with ext then
					set result to (open EachFile)
					tell application "Microsoft Word"
						
						--I think help needed in below 2 lines
						run VB macro macro name "Normal.NewMacros.MyMacro"
						do shell script "textutil -convert txt " & quoted form of POSIX path of (EachFile as text)
					end tell
					--msg(result)
				end if
			end repeat
		end try
	end repeat
	
	
	
	--display dialog (theTopFolder as text)
	repeat with EachSubDir in (get every folder of folder theTopFolder)
		try
			--display dialog (EachSubDir as text)
			repeat with EachFile in (get every file of folder (EachSubDir as alias))
				try
					copy name of EachFile as string to FileName
					--display dialog FileName
					--move Eachfile to theTopFolder
					repeat with ext in fileExt
						if FileName ends with ext then
							--display dialog FileName
							set result to (open EachFile)
							tell application "Microsoft Word"
								
								--I think help needed in below 2 lines
								run VB macro macro name "Normal.NewMacros.MyMacro"
								do shell script "textutil -convert txt " & quoted form of POSIX path of (EachFile as text)
							end tell
							--delete Eachfile
							--msg(result)
						end if
					end repeat
				end try
			end repeat
			--delete folder (EachSubDir as alias)
		end try
	end repeat
end tell

Any Help would be appreciated.

Thanks in advance
John

Hi John,

Just did a quick test.

I created a quick Macro in Word:

I then wrote this little script and saved it as an application:


on open (somefiles)
	
	tell application "Microsoft Word"
		
		repeat with thisFile in somefiles
			
			run VB macro macro name "Normal.NewMacros.TestMacro"
			do shell script "textutil -convert txt " & quoted form of POSIX path of (thisFile as text)
			
		end repeat
		
	end tell
	
end open

I then dragged and dropped a few word docs onto the application.
The Word Macro fired each time and then a txt file was created.

Need to add in the recursion though.

HTH

Hi TechNIk,

Thanks for your reply.

When I drag and drop the files into the application, It works Charmly.

But I want to go through each and every sub folders manually to drag the files. It consumes time.

Is there any applescript to run, on all open word files and to save it as text format in its actual path.

Thanks,
John

Hi John,

Try this:


tell application "Finder"
	set somefiles to entire contents of (choose folder with prompt "Please select directory.") as alias list
end tell


tell application "Microsoft Word"
	
	repeat with thisFile in somefiles
		
		run VB macro macro name "Normal.NewMacros.TestMacro"
		do shell script "textutil -convert txt " & quoted form of POSIX path of (thisFile as text)
		
	end repeat
	
end tell

Hi TecNik,

Its really awesome. Thanks a Lot.

Its saves me a day.

Thanks
John

Hi John,

Glad to hear it’s working ok. :slight_smile: