...

View Full Version : visible issue using new ActiveXObject('Word.Application')



Graeme Hackston
04-06-2003, 09:12 PM
I converted this to work in an hta.

http://www.pcmag.com/article2/0,4149,460693,00.asp

You use it by selecting the text you want to spell check then click spell.

It works but I can't stop Word from flashing on screen when the spell check is complete. If I fill the function full of this.focus() and objWord.visible = false it minimizes the flash but it's still there. Any Ideas?



<html>
<head>
<title>Spell Check</title>

<script type="text/javascript">

fso = new ActiveXObject("Scripting.FileSystemObject")

onload = function() {
document.onselectstart = Who_Was_Selected
}

Selected_Element_Id = ''

function Who_Was_Selected() {
if (event.srcElement.id && document.selection.type == 'Text') {
Selected_Element_Id = event.srcElement.id
}
}


// http://www.pcmag.com/article2/0,4149,460693,00.asp

function Spell_Check() {

var str, objWord, CTR

if (document.selection.type == 'Text') {

try {

document.execCommand("Copy")

str = window.clipboardData.getData("Text").replace(/^\s+/,"").replace(/\s+$/,"")

CTR = document.getElementById(Selected_Element_Id).createTextRange()
CTR.findText(str);
CTR.select();

document.execCommand("Copy")

objWord = new ActiveXObject('Word.Application');
objWord.Documents.Add();
objWord.visible = false
objWord.Selection.Paste();
objWord.ActiveDocument.CheckGrammar();
objWord.Selection.WholeStory();
objWord.Selection.Copy();
objWord.visible = false
objWord.ActiveDocument.Close(0);
objWord.Quit();

document.execCommand("Paste")
}

catch(e) {
alert(e + '\n\nThis error occurred while\nattempting to load MS Word')
}

}
objWord = null
}


</script>
</head>
<body>

<input type="button" value="Spell" onclick="Spell_Check()">

<textarea id="TextArea0" rows="5" cols="20"></textarea>

</body>
</html>

Graeme Hackston
05-27-2003, 12:57 AM
Well I thought I'd take another run at this. It's a mess but it works. WindowState was the key.

There's atleast 1 workaround in it that probably isn't neccesary.

I couldn't get the last example in the following link to return correctly. Instead I used focus to find if a spell/grammar correction was needed.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/office97/html/output/F1/D4/S5A94D.asp


<html>
<head>
<title>Spell Check</title>

<script type="text/javascript">

// fso just so spell check works 1st time (focus issue)
fso = new ActiveXObject("Scripting.FileSystemObject")

onload = function() {
document.body.attachEvent('onselectstart', Who_Was_Selected)
document.body.attachEvent('onfocusin', on_focus_in)
document.body.attachEvent('onfocusout', on_focus_out)
Input_Selected = false
this_blur = false
}


function Who_Was_Selected() {
if (document.selection.type == 'Text') {
Selected_Element = event.srcElement
}
if (event.srcElement.isContentEditable) {
Input_Selected = true
} else {
Input_Selected = false
}
}


function on_focus_in() {
this_blur = false
}


function on_focus_out() {
this_blur = true
}


// http://www.pcmag.com/article2/0,4149,460693,00.asp

function Spell_Check() {

var str, objWord, CTR

if (document.selection.type == 'Text') {

try {

document.execCommand("Copy")

if (Input_Selected) {
str = window.clipboardData.getData("Text").replace(/^\s+/,"").replace(/\s+$/,"")
CTR = document.selection.createRange()
CTR.findText(str)
CTR.select()
document.execCommand("Copy")
}

objWord = new ActiveXObject("Word.Application")
objWord.WindowState = 2

objWord.Documents.Add()
objWord.Selection.Paste()
objWord.ActiveDocument.CheckGrammar()
objWord.Selection.WholeStory()
objWord.Selection.Copy()

window.clipboardData.setData('text', window.clipboardData.getData("Text").replace(/^\s+/,"").replace(/\s+$/,""))

if (!this_blur) {
alert('Correct spelling and grammar')
}
if (Input_Selected) {
CTR.text = window.clipboardData.getData("Text")
}
objWord.ActiveDocument.Close(0)
//objWord.ActiveWindow.Close(0) // ActiveWindow seems to work the same as ActiveDocument
objWord.Quit()

} catch(e) {
alert(e + '\n\nThis error occurred while\nattempting to load MS Word')
}
objWord = null
}

}
</script>
</head>
<body>
<input type="button" value="Spell" onclick="Spell_Check()">
<p></p>
<textarea rows="5" cols="20">txt area</textarea>
<p></p>
<div ContentEditable>Content Editable</div>
<p></p>
Page txt
</body>
</html>



<edit>
Regular page text is just corrected and copied to the clipboard
</edit>

swmr
05-27-2003, 05:28 AM
Hi

That sounds like an interesting utility...

can you give me a hint as to why I get the "occurred while attempting to load MS Word" error?

(the spell-check isn't working--yet, I have that program... :confused: )

Graeme Hackston
05-27-2003, 05:46 AM
Hi swmr, I'm not sure why. This is the same thing with try/catch removed. If you could, give it a try and (if you have error alerts turned on in IE) it will give you the error and line number.


<html>
<head>
<title>Spell Check</title>

<script type="text/javascript">

// fso just so spell check works 1st time (focus issue)
fso = new ActiveXObject("Scripting.FileSystemObject")

onload = function() {
document.body.attachEvent('onselectstart', Who_Was_Selected)
document.body.attachEvent('onfocusin', on_focus_in)
document.body.attachEvent('onfocusout', on_focus_out)
Input_Selected = false
this_blur = false
}


function Who_Was_Selected() {
if (document.selection.type == 'Text') {
Selected_Element = event.srcElement
}
if (event.srcElement.isContentEditable) {
Input_Selected = true
} else {
Input_Selected = false
}
}


function on_focus_in() {
this_blur = false
}


function on_focus_out() {
this_blur = true
}


// http://www.pcmag.com/article2/0,4149,460693,00.asp

function Spell_Check() {

var str, objWord, CTR

if (document.selection.type == 'Text') {

document.execCommand("Copy")

if (Input_Selected) {
str = window.clipboardData.getData("Text").replace(/^\s+/,"").replace(/\s+$/,"")
CTR = document.selection.createRange()
CTR.findText(str)
CTR.select()
document.execCommand("Copy")
}

objWord = new ActiveXObject("Word.Application")
objWord.WindowState = 2

objWord.Documents.Add()
objWord.Selection.Paste()
objWord.ActiveDocument.CheckGrammar()
objWord.Selection.WholeStory()
objWord.Selection.Copy()

window.clipboardData.setData('text', window.clipboardData.getData("Text").replace(/^\s+/,"").replace(/\s+$/,""))

if (!this_blur) {
alert('Correct spelling and grammar')
}
if (Input_Selected) {
CTR.text = window.clipboardData.getData("Text")
}
objWord.ActiveDocument.Close(0)
//objWord.ActiveWindow.Close(0) // ActiveWindow seems to work the same as ActiveDocument
objWord.Quit()

objWord = null
}

}
</script>
</head>
<body>
<input type="button" value="Spell" onclick="Spell_Check()">
<p></p>
<textarea rows="5" cols="20">txt area</textarea>
<p></p>
<div ContentEditable>Content Editable</div>
<p></p>
Page txt
</body>
</html>

swmr
05-27-2003, 06:59 AM
Line 59:

objWord = new ActiveXObject("Word.Application")

---------------------------------------------------------
error: Automation server can't create object
---------------------------------------------------------

I've done some unproductive searching on this error:

it is portrayed as a "security issue", but nothing seems to prevent it... I tried both using an .hta, and lowering all activeX permissions to "enable" (for the local zone), with no luck.

Perhaps "Word.Application" isn't being recognized?

Graeme Hackston
05-27-2003, 03:42 PM
Try lowering your security locally.

Also, If XP is the same as 98, hit Ctrl + Alt + Delete and see if WinWord is running. If so, close that. You might have to do it a couple times until you get the box with the "End Task" dialogue.

swmr
05-28-2003, 08:40 AM
Well this doesn't appear to be a local security issue, because I can get THIS (http://www.winscripter.com/wsh/office/ie_simpleie.asp) example to work without error...

new ActiveXObject("InternetExplorer.Application");

I wonder where one might find the proper name for a particular automation server on one's own system (such as the word processor)... is there a title listed in the registry, or something?

What I mean to ask is: where is it written that the Microsoft Word Processor is identified as "Word"? How would I check this?

There just doesn't seem to be any information about OLE scripting errors, anywhere--which really makes understanding difficult.

Roelf
05-28-2003, 09:28 AM
the flashing of the word window was caused by the order of the statements in your code:
first you had:


objWord = new ActiveXObject('Word.Application');
objWord.Documents.Add();
objWord.visible = false

first load a new document, then make the window invisible, this causes the flashing

then you had:


objWord = new ActiveXObject("Word.Application")
objWord.WindowState = 2
objWord.Documents.Add()

first make the window invisible, then add a new document.

for the other question: when you install office, word identifies itself as word in the system (registry) so does excel, powerpoint etc.
Office has an architecture that every application can be invoked as an ActiveX object in every client with ActiveX capabilities. You can use the office components in a VB-app. So not every application can be treated this way, only the apps which are designed to act as an ActiveX component

swmr
05-28-2003, 10:10 AM
thanks for revealing that :rolleyes:

Graeme Hackston
05-28-2003, 02:54 PM
Yes thank you Roelf



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum