我正在寻找一种更好的方法来编码,以便在我的应用程序中正确关闭WINWORD.EXE或word进程,方法是获取其PID(进程ID),就像在Excel中获取PID一样。
这是我使用Hwnd属性和GetProcessID类获取Excel的PID的示例。
class Filter(QWidget):
"""Common base for all filters"""
defaultK = 3
filterCount = 0
def __init__(self):
super(Filter, self).__init__()
lay = QVBoxLayout(self)
# Variable for the constant of the OpenCV filter
self.k = 3
# Label for the slider
self.k_lbl = QLabel(str(self.k))
# Increase the number of filters created
Filter.filterCount += 1
# Slider for the first OpenCV filter, with min, max, default and step values
self.thresh_sld = QSlider(Qt.Horizontal, self)
self.thresh_sld.setFocusPolicy(Qt.NoFocus)
self.thresh_sld.setMinimum(3)
self.thresh_sld.setMaximum(51)
self.thresh_sld.setValue(self.k)
self.thresh_sld.setSingleStep(2)
self.thresh_sld.valueChanged.connect(self.changeValue)
lay.addWidget(self.k_lbl)
lay.addWidget(self.thresh_sld)
def changeValue(self, value):
# Function for setting the value of k1
print(value)
if value % 2 == 1:
self.k = value
else:
self.k = value + 1
self.thresh_sld.setValue(self.k)
self.k_lbl.setText(str(self.k))
class MainWindow(QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.filter1 = Filter()
self.filter2 = Filter()
# Creates the main layout (vertical)
v_main_lay = QVBoxLayout()
# Adds the sliders and their labels to the bottom of the main layout
v_main_lay.addWidget(self.filter1)
v_main_lay.addWidget(self.filter2)
# Sets the main layout
self.setLayout(v_main_lay)
# Sets the geometry, position, window title and window default mode
self.setGeometry(300, 300, 350, 300)
self.setWindowTitle('Review')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
这是关闭Word Process / WINWORD.EXE的代码,但不幸的是,它不能在服务器端工作,所以我正在寻找一种方法和代码来获取Word Process ID以正确关闭它。
Imports System.Diagnostics
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Integer, ByRef lpdwProcessId As IntPtr) As IntPtr
Public Sub CreateNewFromTemplate_Excel()
' Create Excel Application, Workbook, and WorkSheets
Dim xlExcel As Excel.Application = New Excel.Application
'Getting the process ID of excel application
Dim processId As IntPtr = Nothing
GetWindowThreadProcessId(xlExcel.Hwnd, processId)
Dim excelProcess As Process = Process.GetProcessById(processId.ToInt32())
Dim blError As Boolean = False
Try
[Perform Excel generation here.]
' Make sure all objects are disposed
xlBook.Close()
xlExcel.Quit()
Catch ex As Exception
blError = True
If Not excelProcess.HasExited Then excelProcess.Kill() ' End the specific Excel Process ID if not yet closed.
Throw
Finally
If Not blError Then
If Not excelProcess.HasExited Then excelProcess.Kill() ' End the specific Excel Process ID if not yet closed.
End If
End Try
End Sub
任何人都可以建议编写如何获取Word实例的PID或Hwnd属性的最佳方法或最佳实践。谢谢:))
答案 0 :(得分:-1)
您尝试做的事情可能不会像预期的那样可靠。如果您需要从服务器处理文档和电子表格,我强烈建议您切换到OpenXML或其他库。侧。
我倾向于使用效果非常好的EPPlus,它与VBA / Interop几乎相同。 http://epplus.codeplex.com/
在继续之前,我会阅读Microsoft的以下警告和文章:
所有当前版本的Microsoft Office都经过设计,测试和配置,可在客户端工作站上作为最终用户产品运行。他们假设交互式桌面和用户配置文件。它们不提供满足设计为无人值守的服务器端组件需求所必需的可重入性或安全性。
Microsoft目前不建议也不支持从任何无人参与的非交互式客户端应用程序或组件(包括ASP,ASP.NET,DCOM和NT服务)自动化Microsoft Office应用程序,因为Office在此环境中运行Office时,可能会出现不稳定的行为和/或死锁。
https://support.microsoft.com/en-us/help/257757/considerations-for-server-side-automation-of-office