某些系统的单词即将关闭 [英] word is getting closed for some system

查看:91
本文介绍了某些系统的单词即将关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Outlook外挂程序中使用Word Interop api来读取Word文件.

I am using word interop api in outlook addins for reading word file.

这对于带有Windows 8和Windows 10的Office 2010很好用,但是对于某些带有Office(x86)的Windows 7(x64)系统,我面临的问题是一段时间后单词变得不可见.我想这是因为我看到任务管理器时,它只打开一个实例,并且在word上执行以下操作

This is working fine for office 2010 with windows 8 and windows 10. but for some of windows 7 (x64) system with office (x86) i am facing problem that word is getting invisible after sometime. I guess this is happening because when i am seeing task manager it opens only one instance and i am doing following operation on word

          Private Shared ObjwordApp As Word.Application
          ObjwordApp = New Word.Application()
          ObjwordApp.Visible = False 

谢谢

Ravi

-似乎 word在发生上述问题的计算机上创建了一个实例.这意味着如果我们使用互操作代码和用户手动干预从Outlook Add in中打开单词. 两者均创建winword.exe的单个实例.

Edit :- It seems that word creates single instance on machines, where the said problem occurs. Meaning if we open word from Outlook Add in by using our interop code and by using user manual intervention. Both creates single instance of winword.exe.

我希望它可以澄清问题.

I hope it clarifies the issue.

推荐答案

从历史上看,Word和Excel等Office应用程序在Windows ROT中仅注册一个实例.默认行为是,将启动Office应用程序的任何后续操作都将重新路由"到ROT中注册的实例. (参考知识库文章 https://support.microsoft.com/en-us/kb/316125 https://support.microsoft.com/en-us/kb/238975 )

Historically, Office applications such as Word and Excel register only one instance in the Windows ROT. The default behavior has been that any subsequent action that would start the Office application will be "re-routed" to the instance registered in the ROT. (Reference KB articles https://support.microsoft.com/en-us/kb/316125 and https://support.microsoft.com/en-us/kb/238975)

当代码使用GetActiveObject时,此行为也适用-它将拾取ROT中注册的实例,而不管可能正在运行Word的实例是多少.为了解决此问题,如果您要确保代码 not 不共享它没有启动的实例,请使用New关键字来启动Word.Application对象的新实例.

This behavior also applies when code uses GetActiveObject - it picks up the instance registered in the ROT, regardless how many instances of Word may be running. In order to work around that, if you want to be sure your code does not share an instance it does not start, the New keyword is used to start a new instance of the Word.Application object.

棘手的部分是当ROT中没有注册Word的现有实例时.在这种情况下,当您启动一个新实例时,它将是在ROT中注册的实例,并且用户操作将趋向于拾取它,这可能会导致冲突. (参考知识库文章 http://support.microsoft.com/kb/188546/)

The tricky part is when no existing instance of Word is registered in the ROT. In this case, when you start a new instance it will be the one registered in the ROT and user actions will tend to pick it up, which can cause conflicts. (Reference KB article http://support.microsoft.com/kb/188546/)

唯一的解决方法是首先测试Word.Application的运行实例(进程)是否可用.如果存在,则使用New Word.Application将启动用户不可用的Word实例.

The only way to workaround this is to first test whether a running instance (Process) of the Word.Application is available. If there is, using New Word.Application will start a Word instance that is not available to the user.

如果没有可用的Word实例,则您的代码需要首先启动一个不会使用的Word进程,以使其继续为用户运行.然后,您的代码将启动一个 second 实例供其自己使用.它不会在ROT中注册,因此,当用户启动Word或双击文档时,将拾取第一个实例,而不是您的应用程序正在使用的实例.

If there is no Word instance available, your code needs to first start a Word process that it will not use, leaving it running for the user. Your code then starts a second instance for its own use. It will not be registered in the ROT, so when the user starts Word or double-clicks a document the first instance will be picked up, not the one your application is using.

请注意,在Windows + Office的最新版本中,发生了一些(未记录的)更改,这些更改更改了Word是否/如何从ROT拾取实例或启动新实例.这可能就是为什么您看到各种行为的原因.但是您需要在代码中构建变通办法,以确保没有冲突.

Note that in recent versions of Windows + Office some (undocumented) changes have taken place that change whether/how Word picks up an instance from the ROT vs. starting a new instance. This is probably why you see varying behavior. But you need to build the workaround into your code in order to be sure there are no conflicts.

这是一个代码片段,说明了解决方法的原理

Here's a code snippet illustrating the principle of the workaround

Process[] wdPcs = Process.GetProcessesByName("WinWord");
int nrWordInstances = wdPcs.Length - 1;
if (nrWordInstances >= 0)
{
  //To pick up other running instance, if you want
  //wdApp = (Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
  wdApp = new Word.Application();
}
else
{
  //The process that will be registered in the ROT
  //Allows use of command line switches to control how Word starts
  //https://support.office.com/en-us/article/Command-line-switches-for-Microsoft-Office-Word-2007-8be53d5f-9f13-4987-a91e-b272aac5d39d 
  //Note: new Word.Application() is also fine
  ProcessStartInfo psi = new ProcessStartInfo("winword.exe", "/w");
  Process wdProc = Process.Start(psi);

  //Instance to be used in the running code
  wdApp = new Word.Application();
}

这篇关于某些系统的单词即将关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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