Bring spell check window to foreground with JavaScript/JScript in Windows 7

我们两清 提交于 2019-12-12 17:00:56

问题


I have some JScript code (converted from some old VBScript) that starts like this:

var Word = new ActiveXObject("Word.Basic");

Word.FileNew(); // opens new Word document
Word.Insert(IncorrectText);
Word.ToolsSpelling(); // opens spell check behind IE

The idea is to utilize the MS Word spell check for browser use, and it works well in XP, but the spell check box opens in the background in Windows 7 / IE 8 (this question tells me that the problem started in Vista and is probably an OS issue, not a browser or Office issue).

So my question is, how can I bring this window to the foreground? One important note is that the last line, Word.ToolsSpelling();, locks up my script, so anything I do will need to be before that.

I've tried

var wshShell = new ActiveXObject("WScript.Shell");
wshShell.AppActivate("Document1 - Microsoft Word"); // or some other text

before the ToolsSpelling call, but this does not do anything (maybe because the Word document is not actually revealed at this point?). Of course, this will only work if no "Document1" is already opened, so this is a questionable thought to begin with.

Per this answer, I tried using window.blur(); in order to blur IE, but this will only work if the IE window is the only one opened. Maybe there's some way I can loop through all opened windows and apply this?

SetForegroundWindow looked promising, but I don't know how to use it in JSript.

Any ideas?

Note: Browser permissions will be completely open for this site.

Update: Turns out if you use Word.Application, the spell check comes up in front as it should. Only the Word.Basic method has this problem (I don't expect to know why this side of eternity):

var wordApp = new ActiveXObject("Word.Application");
wordApp.Documents.Add();
wordDoc = wordApp.ActiveDocument;
... // safety checks before insertion
wordSelection.TypeText(IncorrectText);
wordDoc.CheckSpelling();
wordApp.Visible = false; // CheckSpelling makes the document visible

回答1:


You might be able to jigger the window state. When the window is maximized after having been minimized, Windows will stack that in front (zIndex to top).

Something like:

 var WIN_MAX = 2;
 var WIN_MIN = 1;

 var Word = new ActiveXObject("Word.Application");
 Word.Visible = true;
 // minimize the app
 Word.WindowState = WIN_MIN ;
 // in 500ms, maximize
 setTimeout(function () {
     Word.WindowState = WIN_MAX;
 }, 500);

The setTimeout call seeks to work around a timing issue; Windows will sometimes "get confused" when a programmatic minimize/maximize happens in immediate succession. You might have to extend that delay a little, test it repeatedly and see what kind of results you get.



来源:https://stackoverflow.com/questions/6230877/bring-spell-check-window-to-foreground-with-javascript-jscript-in-windows-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!