2

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
Community
  • 1
  • 1
jtpereyda
  • 6,987
  • 10
  • 51
  • 80

1 Answers1

2

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.

Chris Baker
  • 49,926
  • 12
  • 96
  • 115
  • I'm getting an "Object doesn't support this property or method" error with `Word.Visible = true;`. Same with `Word.WindowState = WIN_MIN`. Maybe these functions aren't valid with Word.Basic? – jtpereyda Jun 09 '11 at 18:22
  • Can you try `Word.Application` as the ActiveXObject instead? Let me know. – Chris Baker Jun 09 '11 at 18:24
  • `var Word = new ActiveXObject("Word.Application");` does work, but it invalidates the other calls. I think the replacement for `Word.FileNew()` is `Word.Documents.Add()`, but I'm having trouble with the selection. If you or somebody already knew how to work this API, that would be helpful. Otherwise I'll pick it up after the weekend. – jtpereyda Jun 09 '11 at 23:43
  • Check out the doc: http://msdn.microsoft.com/en-us/library/kw65a0we%28v=vs.80%29.aspx – Chris Baker Jun 10 '11 at 14:26
  • Well, changing everything to use `Word.Application` basically fixed the problem and gave me the desired behavior, haha. However, `Word.Visible` came in handy for hiding the document after the spell check (which makes the document visible upon completion). – jtpereyda Jun 14 '11 at 17:32