如何退出或关闭(不杀死)Word文档(过程)? [英] How to Quit or Close (not Kill) Word document (process)?

查看:157
本文介绍了如何退出或关闭(不杀死)Word文档(过程)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们公司中,我们使用Windows应用程序生成Word(2010)文档.有时文档没有正确关闭,所以另一个应用程序(是的,他们仍然称其为development :)杀死了运行超过1分钟的文字处理.

In our company we are using Windows application to generate Word (2010) documents. Sometimes the document is not properly closed, so another application (yes, they still call it development:) kill word process which is running more than 1 minute.

这些被杀死的进程存储在MS Word的文档恢复"中.无法在Word 2010中关闭这些文档恢复(选项"/保存自动恢复信息.."不能解决问题).当这些文档恢复"中装满文档(数十个或数百个)时,该应用程序停止生成文档,我们需要手动清理正面.

These killed processes are stored in MS Word's "Document Recovery". These Document recovery is not possible to turn off in Word 2010 (Options/"Save Autorecover info.." does'n solve the problem). When these "Document Recovery" is full of documents (tens or hundreds), the application stops to generate documents and we need to manually clear the front.

我能够编写此脚本来识别并杀死正在运行10秒钟以上的进程,但是有没有办法正确关闭它(不杀死它)?

I was able to write this script to identify and kill the processes which are running more than 10 seconds, but is there a way how to close it properly (without killing it)?

我发现本教程使用Powershell创建和退出Word文档,但是如何将特定的Word处理程序与这些MS Office方法连接以关闭文档呢?

I found this tutorial to create and quit word document with powershell, but how to connect specific word process with these MS Office method to close document?

Powershell和Word教程

$timer = 10
$date = Get-Date

if (Get-Process winword*) {
    Get-Process winword | foreach { 
        if ((($date - $_.StartTime).Seconds) -gt $timer) {
            $procID = $_.id
            Write-Host -ForegroundColor Magenta "Process $procID is running longer than $timer seconds."
            Write-Host -ForegroundColor Green "Killing process $procID.."
            Stop-Process $procID
        }
    }
}

推荐答案

您需要按照此处,然后关闭文档:

You need to attach to the running Word instance as described here, and then close the document:

$wd = [Runtime.Interopservices.Marshal]::GetActiveObject('Word.Application')
$wd.Documents | ? { $_.Name -eq 'some.docx' } | % {
  $_.Saved = $true
  $_.Close()
}

此操作必须在启动应用程序的用户的上下文中运行,并且只会附加到首先启动的实例(通常只有一个实例在运行).您需要先退出该实例,然后才能附加到第二个启动的实例.

This must be run in the context of the user who started the application, and it will only attach to the instance started first (usually there's only one instance running anyway). You'd need to quit that instance first before you can attach to the instance started second.

这篇关于如何退出或关闭(不杀死)Word文档(过程)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆