下面的代码显示了如何按文件名关闭应用程序。它在不保存文档的情况下关闭它,如果我在记事本中指向一个文件名,它不会直接关闭,而是询问是否保存。我想通过直接保存文档来关闭应用程序。
有什么想法可以更正代码吗?
Const WM_Close As UInteger = &H10
Const WM_KEYDOWN = &H100
Const WM_COMMAND = &H112
Private Sub searchnclosedoc(ByVal noextfilename As String, ByVal ms_appname As String)
For Each wproc As Process In Process.GetProcessesByName(ms_appname)
If wproc.MainWindowTitle.Contains(noextfilename) Then
If PostMessage(wproc.MainWindowHandle, WM_Close, WM_KEYDOWN, Keys.Enter) Then
'MessageBox.Show("not closed, dialog asked")
'PostMessage(wproc.MainWindowHandle, WM_KEYDOWN, WM_COMMAND, Keys.Enter)
MyThreadedControl(lbltest, "Text", noextfilename & " was SAVED and CLOSED to Prevent Conflict")
Else
MyThreadedControl(lbltest, "Text", noextfilename & " was CLOSED to Prevent Conflict")
End If
End If
Next
End Sub
这会关闭 msword 而不询问是否保存。这不会在关闭后保存文档。但我希望在关闭之前保存文档。在记事本中会出现一个对话框,询问是否保存。我想关闭应用程序并直接保存编辑的文档而不显示对话框。
PostMessage(wproc.MainWindowHandle, WM_Close, WM_KEYDOWN, Keys.Enter)
这就是我使用该程序的方式,
searchnclosedoc(dfilenamewithoutextension, "MSWORD")
searchnclosedoc(dfilenamewithoutextension, "notepad")
这个我也试过
Private Sub searchnclosedoc(ByVal noextfilename As String, ByVal ms_appname As String)
For Each wproc As Process In Process.GetProcessesByName(ms_appname)
If wproc.MainWindowTitle.Contains(noextfilename) Then
If PostMessage(wproc.MainWindowHandle, WM_Close, 0, 0) Then
'MessageBox.Show("not closed, dialog asked")
Dim h, h2, h3, h4 As IntPtr
If ms_appname = "notepad" Then
h = FindWindowA(h, "Notepad")
ElseIf ms_appname = "Foxit Reader" Then
h = FindWindowA(h, "Foxit Reader")
Else
h = FindWindowA(h, ms_appname)
End If
If h = 0 Then Exit Sub
If ms_appname = "notepad" Then
h2 = FindWindowEx(h, IntPtr.Zero, "Button", "&Yes")
ElseIf ms_appname = "Foxit Reader" Then
h2 = FindWindowEx(h, IntPtr.Zero, "Button", "&No")
Else
h3 = FindWindowEx(h, IntPtr.Zero, "Button", "&Save")
End If
If h2 <> 0 Then h4 = h2 Else h4 = h3
If h4 <> 0 Then
PostMessage(h4, &HF5, 0, 0)
End If
MyThreadedControl(lbltest, "Text", noextfilename & " was SAVED and CLOSED to Prevent Conflict")
Else
MyThreadedControl(lbltest, "Text", noextfilename & " was CLOSED to Prevent Conflict")
End If
End If
Next
End Sub
记事本不好,它保存,有时它还用对话框询问,福昕阅读器是好的,不保存为NO,msWORD不保存并直接关闭我想关闭,保存,关闭。
使用此代码解决:(我更改代码)在下面。
Private Sub searchnclosedoc(ByVal noextfilename As String, ByVal ms_appname As String)
For Each wproc As Process In Process.GetProcessesByName(ms_appname)
If wproc.MainWindowTitle.Contains(noextfilename) Then
SetForegroundWindow(wproc.MainWindowHandle)
SendKeys.SendWait("^(s)")
Thread.Sleep(100)
SendKeys.SendWait("%{F4}")
Thread.Sleep(100)
SendKeys.SendWait("{ENTER}")
MyThreadedControl(lbltest, "Text", noextfilename & " was SAVED and CLOSED to Prevent Conflict")
End If
Next
End Sub
暂时使用此代码:并且工作正常。
Private wordApp as New Word.Application
Private Sub searchnclosedoc(ByVal noextfilename As String, ByVal ms_appname As String)
For Each wproc As Process In Process.GetProcessesByName(ms_appname)
If ms_appname.Contains("WINWORD") Then
If wproc.MainWindowTitle.Contains(noextfilename) Then
wordapp = DirectCast(GetObject(, "Word.Application"), Word.Application)
AddHandler wordapp.DocumentBeforeClose, AddressOf WordClose
PostMessage(wproc.MainWindowHandle, WM_Close, 0, 0)
MyThreadedControl(lbltest, "Text", noextfilename & " was SAVED and CLOSED to Prevent Conflict")
End If
End If
Next
End Sub
谢谢