问题
I am using word interop api in outlook addins for reading word file.
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
Thank You
Ravi
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.
回答1:
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)
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.
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/)
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.
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.
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();
}
来源:https://stackoverflow.com/questions/35555178/word-is-getting-closed-for-some-system