15

次のコードを検討してください。

using Microsoft.Office.Interop.Word;

ApplicationClass _application = new ApplicationClass();

_application によって起動された Winword.exe プロセスから PID を取得できますか?

ファイルが破損していると、次のコードを使用しても ApplicationClass を終了できないため、PID が必要です。

_application.Quit(ref saveFile, ref missing, ref missing);
System.Runtime.InteropServices.Marshal.ReleaseComObject(_application);
GC.Collect();
GC.WaitForPendingFinalizers();

winword.exe プロセスを検索して強制終了することはできません。複数のプロセスがあり、どれを強制終了するかわからないからです。各 ApplicationClass の PID を取得できれば、終了するのに苦労している正しい winword.exe プロセスを強制終了できます。

4

9 に答える 9

10

方法は次のとおりです。

//Set the AppId
string AppId = ""+DateTime.Now.Ticks(); //A random title

//Create an identity for the app

this.oWordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
this.oWordApp.Application.Caption = AppId;
this.oWordApp.Application.Visible = true;

while (GetProcessIdByWindowTitle(AppId) == Int32.MaxValue) //Loop till u get
{
    Thread.Sleep(5);
}

///Get the pid by for word application
this.WordPid = GetProcessIdByWindowTitle(AppId);

///You canh hide the application afterward            
this.oWordApp.Application.Visible = false;

/// <summary>
/// Returns the name of that process given by that title
/// </summary>
/// <param name="AppId">Int32MaxValue returned if it cant be found.</param>
/// <returns></returns>
public static int GetProcessIdByWindowTitle(string AppId)
{
   Process[] P_CESSES = Process.GetProcesses();
   for (int p_count = 0; p_count < P_CESSES.Length; p_count++)
   {
        if (P_CESSES[p_count].MainWindowTitle.Equals(AppId))
        {
                    return P_CESSES[p_count].Id;
        }
   }

    return Int32.MaxValue;
}
于 2011-07-01T09:42:29.533 に答える
3

Wordファイルにエラーがある可能性があります。その結果、メソッドWord.ApplicationClass.Documents.Open()でファイルを開くと、ダイアログが表示され、プロセスがハングします。

Word.ApplicationClass.Documents.OpenNoRepairDialog()代わりに使用してください。問題が解決したことがわかりました。

于 2010-08-03T14:32:34.967 に答える
2

これを取得する通常の方法は、Wordのタイトルを一意のものに変更し、それが見つかるまでトップレベルのウィンドウリストをホップすることです(EnumWindows)。

于 2009-05-02T15:10:18.933 に答える
2

http://www.codekeep.net/snippets/7835116d-b254-466e-ae66-666e4fa3ea5e.aspx

///Return Type: DWORD->unsigned int
///hWnd: HWND->HWND__*
///lpdwProcessId: LPDWORD->DWORD*
[System.Runtime.InteropServices.DllImportAttribute( "user32.dll", EntryPoint = "GetWindowThreadProcessId" )]
public static extern int GetWindowThreadProcessId ( [System.Runtime.InteropServices.InAttribute()] System.IntPtr hWnd, out int lpdwProcessId );


private int _ExcelPID = 0;
Process _ExcelProcess;

private Application _ExcelApp = new ApplicationClass();
GetWindowThreadProcessId( new IntPtr(_ExcelApp.Hwnd), out _ExcelPID );
_ExcelProcess = System.Diagnostics.Process.GetProcessById( _ExcelPID );

...

_ExcelProcess.Kill();
于 2009-09-08T07:25:44.347 に答える
1

残念ながら、ApplicationClass のインスタンスを Word の実行中のプロセスに関連付ける方法はありません。

Word のインスタンスを強制終了する必要があるのはなぜですか? すべてのドキュメントを閉じてから、そのインスタンスの使用を停止するように依頼することはできませんか? クラスへのすべての参照を削除すると、最終的にGCが開始され、 COMサーバーがダウンします。

于 2009-05-02T14:42:11.903 に答える
1

アプリケーションを開始する前に、実行中のすべての Word プロセスを一覧表示し、アプリケーションを起動して、実行中の Word プロセスを再度一覧表示します。2 番目のリストにあり、最初のリストにはないプロセスが正しいものです。

var oPL1 = from proc in Process.GetProcessesByName("WINWORD") select proc.Id;
var app = new Word.Application();

var oPL2 = from proc in Process.GetProcessesByName("WINWORD") select proc.Id;
var pid = (from p in oPL2 where !oPL1.Contains(p) select p).ToList()[0];

この方法には明らかなタイミングの問題がありますが、ほとんどの場合確実に機能する唯一の方法です。

于 2014-02-07T09:41:37.203 に答える