Find if MS Word Document has opened

Hello:

I’m having an issue figuring out if my document has opened before continuing with the script later on. I have this:

set theTemplatePath to my_path
set theTemplateFile to (POSIX file inTemplatePath)

tell application “Microsoft Word”
set theTemplateDocument to (open file name theTemplateFile without add to recent files)

set pathToMe to POSIX path of (inTemplatePath as text)
set filename_script to “basename '” & pathToMe & “'”
set fileName to do shell script filename_script

set inTime to current date
repeat
try
set namesList to name of documents
if fileName is in namesList then
display dialog "EXITING LOOP BECAUSE FOUND FILE" & namesList
exit repeat
end if
end try

if (current date) - inTime is greater than 10 then
display dialog "EXITING LOOP BECAUSE OF TIMEOUT" & namesList
exit repeat
end if
delay 1

end repeat

However, the namesList does not seem to hold anything, so this loop does not work. Any ideas why, or how to make a better script to find if the document has been opened yet?

Browser: Safari 601.4.4
Operating System: Mac OS X (10.10)

I assume that my_path is a posix path defined before entering this piece of code.
You define the variable theTemplatePath accordingly.
Where is the variable inTemplatePath defined ?
Is it also defined before entering or is there a typo in the first instruction which would be :

set inTemplatePath to my_path.

As I don’t allow microsoft products to enter my home, I can’t test but you may try with :

--set theTemplatePath to my_path
set inTemplatePath to my_path
set theTemplateFile to (POSIX file inTemplatePath)

# Three instructions which have no reason to be in the tell Word block.
set pathToMe to quoted form of POSIX path of (inTemplatePath as text)
set filename_script to "basename " & pathToMe
set fileName to do shell script filename_script

tell application "Microsoft Word"
	set theTemplateDocument to (open file name theTemplateFile without add to recent files)
	
	set foundIt to false
	repeat 10 times
		delay 1
		try
			set namesList to name of documents
			if fileName is in namesList then
				set foundIt to true
				exit repeat
			end if
		end try
	end repeat
	if foundIt then
		display dialog "EXITING LOOP BECAUSE FOUND FILE " & fileName # was namesList
	else
		display dialog "EXITING LOOP BECAUSE OF TIMEOUT " & namesList
	end if
end tell

Yvan KOENIG running El Capitan 10.11.3 in French (VALLAURIS, France) mercredi 24 février 2016 17:35:46

I still have problems with this:

set namesList to name of documents

The namesList never seems to contain anything.I managed to fix the issue by doing this:

on executeMergeWithTemplatePathAndDataSourcePathAndResultPath(inTemplatePath, inDataSourcePath, inResultPath)
set theTemplateFile to (POSIX file inTemplatePath)
set theDataSourceFile to (POSIX file inDataSourcePath)
set theResultFile to (POSIX file inResultPath)
tell application “Microsoft Word”
set theTemplateDocument to (open file name theTemplateFile without add to recent files)
set pathToMe to POSIX path of (inTemplatePath as text)
set filename_script to “basename '” & pathToMe & “'”
set fileName to do shell script filename_script
set didFindDocument to false
set inTime to current date
repeat
try
set cd to count documents
repeat with a from 1 to cd
set docName to name of document a
if docName is equal to fileName
set didFindDocument to true
end if
end repeat
if didFindDocument then
exit repeat
end if
end try
if (current date) - inTime is greater than 8 then
exit repeat
end if
delay 1
end repeat
if didFindDocument is false then
error number -1756
return
end if
set theDataMerge to the data merge of theTemplateDocument
tell theDataMerge to open data source name theDataSourceFile
execute data merge theDataMerge
tell theTemplateDocument
close saving no
end tell
tell the active document
save as file name (theResultFile as string)
close
end tell
end tell
end executeMergeWithTemplatePathAndDataSourcePathAndResultPath

I still have a problem, though. On occasion, I get this error:

2016-02-24 17:18:32.841 Error running script {
NSAppleScriptErrorAppName = “Microsoft Word”;
NSAppleScriptErrorBriefMessage = “The object you are trying to access does not exist”;
NSAppleScriptErrorMessage = “Microsoft Word got an error: The object you are trying to access does not exist”;
NSAppleScriptErrorNumber = “-1728”;
NSAppleScriptErrorRange = “NSRange: {1831, 10}”;
}

If I look at the NSAppleScriptErrorRange, It ends up being at line:

set theDataMerge to the data merge of theTemplateDocument

Any ideas on why or how to fix it? I’m running El Cap and MS Word 2016. I’m wondering if it is a sandbox issue.

Thanks!