Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking to see if MS Word is already open 1

Status
Not open for further replies.

hererxnl

Technical User
Jul 8, 2003
239
US

Hey folks. I have an in-house classic asp app that is using the following function to open a word document on a webdav folder:
Code:
<script language="JavaScript">
function openDocument(FilePath)
<!--
	{
		var objApp=new ActiveXObject("Word.Application");
		objApp.Visible=true; // "Visible" is in the Word Object ModelEnd Sub
		objApp.Documents.Open(FilePath);
	}
//-->
</script>
Every time a user clicks the open button for a document this function creates a new instance of word via:
Code:
var objApp=new ActiveXObject("Word.Application");
Does anyone know of a way to determine if word is already open to target the current instance instead of creating a new object each time?

Any help is greatly appreciated, thanks in advance!
 
Sorry I didn't see your response Dan. In this case, because it opens multiple instances of Word and it's often necessary to have many documents open simultaneously.
 
I don't know how much you get a general knowledge of windows administrative scripting. If you don't have any, I would think even if I answer to your question would not help because the ground is not prepared for an understanding. So it won't end with any kind of satisfaction on any party, you and me.

With windows scripting, you can use GetObject() to control the re-use of word.application existing instance. But, in the browser environment, even with your consent of using ActiveXObject on the client, the GetObject() is by design blocked. Hence, you won't be able to do that policing.

Hence, you should instead do proper closing of the word.application in the script spawning any instance of it. However, if the application is such that it spawns an instance and let it loose for the user to interfacing with it, to doing any thing necessary on it, then if the user won't close it out after all the operations done, you just have to content with the less than perfect situation.
 
Dan I thought one of us was dyslexic, looks like it was me ;)
I read what you wrote as "Why not just create the ActiveX object every time?"

I still don't get what you mean though. Are you saying use:
Code:
var objApp=ActiveXObject("Word.Application");
instead of:
Code:
var objApp=new ActiveXObject("Word.Application");
Please elaborate.

tsuji: Thanks for the response. My knowledge of windows administrative scripting is definitely limited.

"However, if the application is such that it spawns an instance and let it loose for the user..."

This is exactly my problem and I may have to live with it, you're right.
 
I was thinking more along the lines of creating a global var with the one instance of the ActiveX control:

Code:
var objApp=new ActiveXObject("Word.Application");

and then referring to that global variable each time you open a file:

Code:
objApp.Documents.Open(FilePath);

I haven't tried it, but it's the first step I would take if faced with the same problem.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 

I see what you're saying. I'll give that a shot and I appreciate the help. I know fiddling with ActiveX BS is the last thing a proper js person wants to do. It's the last thing I want to do also. ;)

 
BillRayPreacher,

If one sets variable
Code:
var objApp=new ActiveXObject("Word.Application");
within a given function, is this variable not of local scope to the function?

Further, if one sets said variable, is an instance of word virtually opened but not visible?

So far, and must I say that I am at entry level JS, when I intend to use a variable as global, I define the variable outside all functions. I reset its value within the functions and thus effectively track its status.

Kind of cool I found this thread since I just started looking into how to open both word and excel documents from within my PHP scripts. Perhaps, this is a road I can follow!

Regards,



--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
southbeach said:
If one sets variable
Code:
var objApp=new ActiveXObject("Word.Application");
within a given function, is this variable not of local scope to the function?

It would, yes. That's why I stated "creating a global var" so that it was obvious that that was my intention. The implementation is up to the coder, as there are many different ways to create a global variable (see later in this post).

southbeach said:
Further, if one sets said variable, is an instance of word virtually opened but not visible?

Now that I dont know, which is why I said "I haven't tried it, but it's the first step I would take if faced with the same problem." ;-)

southbeach said:
when I intend to use a variable as global, I define the variable outside all functions.

I tend to do that as well - not because it's necessary, but because I feel that I'm making it obvious that these variables are global (without going into Hungarian notation, etc).

As an aside, to define a global variable from within a function, you can use:

Code:
function someFunc() {
   window['myGlobalVar'] = 'someValue';
}

or

Code:
function someFunc() {
   myGlobalVar = 'someValue';
}

Notice the latter has no 'var' keyword in front of it, which would (as you spotted earlier) make a local variable instead.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top