Open Word from javascript and bring to front

让人想犯罪 __ 提交于 2019-12-24 00:59:40

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/11554891/open-word-from-javascript-and-bring-to-front

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