Open Word from javascript and bring to front

vmasanas picture vmasanas · Jul 19, 2012 · Viewed 15.9k times · Source

I'm using the following code to open a Word document from javascript:

    function openWord(file) {
    try {
        var objword = new ActiveXObject("Word.Application");
    } catch (e) {
        alert(e + 'Cannot open Word');
    }

    if (objword != null) {
        objword.Visible = true;
        objword.Documents.Open(file);
    }
}

This works fine the only problem is that the Word application does not come to the front when opened, instead it opens just behind the browser. Is there a way to force Word to open on top of any other window? or to bring it to front when its open?

Answer

vmasanas picture vmasanas · Jul 19, 2012

Not exactly perfect but this worked for me:

$(document).ready(function() {
  $("#open").click(function() {
    openWord('https://www.google.com.mx/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwjp7ajpqoTLAhUUwGMKHc3UB5AQFggbMAA&url=http%3A%2F%2Fblog.espol.edu.ec%2Fgfflores%2Ffiles%2F2012%2F02%2FC%25C3%25B3digo-de-Hola-Mundo-para-Simulador-BlackBerry.docx&usg=AFQjCNHoFTUJxMonRG1lpr44K9eZjuxEvA&sig2=9bgOMw8yYzWhFXz0q_JbKg');
  });
});

function openWord(file) {
  try {
    var objword = new ActiveXObject("Word.Application");
  } catch (e) {
    alert(e + 'Error Word');
  }

  if (objword != null) {
    objword.Visible = true;
    objword.Documents.Open(file);
    objword.WindowState = 2;
    objword.WindowState = 1;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="open">Try</button>

it still opens Word in the background, but then forces a minimize - maximize and brings it to front.