免費論壇 繁體 | 簡體
Sclub交友聊天~加入聊天室當版主
分享
Board logo

標題: 判定WORD有無在執行 [打印本頁]

作者: mhfo    時間: 2013-11-4 09:11     標題: 判定WORD有無在執行

Dim tempPath As String = System.Environment.GetEnvironmentVariable("TEMP")
Dim fileName As String = Path.Combine(tempPath, "推理01.doc")
Dim winwordPath As String = ""

' 判断系统中是否已经有 Word 实例在运行。
Dim wordProcesses As Process() = Process.GetProcessesByName("winword")
For Each process__1 As Process In wordProcesses
        Debug.WriteLine(process__1.MainWindowTitle)
        winwordPath = process__1.MainModule.FileName
        ' 如果有的话获得 Winword.exe 的完全限定名称。
        Exit For
Next

Dim wordProcess As New Process()

If winwordPath.Length > 0 Then
        ' 如果有 Word 实例在运行,使用 /w 参数来强制启动新实例,并将文件名作为参数传递。
        wordProcess.StartInfo.FileName = winwordPath
        wordProcess.StartInfo.UseShellExecute = False
        wordProcess.StartInfo.Arguments = fileName & " /w"
Else
        ' 如果没有 Word 实例在运行,还是
        wordProcess.StartInfo.FileName = fileName
        wordProcess.StartInfo.UseShellExecute = True
End If

wordProcess.Start()
wordProcess.WaitForExit()
' 当前进程一直在等待,直到该 Word 实例退出。
wordProcess.Close()




歡迎光臨 魚骨頭的雲端圖書館 (http://mhfo.hotbbs.info/) Powered by Discuz! 7.0.0