从 javascript 打开 Word 并显示在前面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11554891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 13:39:28  来源:igfitidea点击:

Open Word from javascript and bring to front

javascriptms-wordactivexobject

提问by vmasanas

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

我正在使用以下代码从 javascript 打开 Word 文档:

    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?

这工作正常,唯一的问题是 Word 应用程序在打开时不会出现在前面,而是在浏览器后面打开。有没有办法强制 Word 在任何其他窗口之上打开?还是在打开时把它放在前面?

回答by vmasanas

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.

它仍然在后台打开 Word,但随后强制最小化 - 最大化并将其置于前面。