Have you tried send a Ctrl-S before sending WM_Close?
Send or Post a message to notepad or msword dialog when application ask to save or not
-
31-05-2022 - |
سؤال
The code below shows how to closed an application by filename. And it closes it without saving a document, If I point a filename in notepad, it doesn't closed directly, instead asking to save or not. I want to close the application with saving the document directly.
Any ideas to correct the code?
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
This closes msword without asking to saved or not. This doesn't save the document after closed. But I want the document to be saved before closing. And in notepad a dialog box appears asking to saved or not. I want to close the application and directly saved the documents edited without showing the diaglog box.
PostMessage(wproc.MainWindowHandle, WM_Close, WM_KEYDOWN, Keys.Enter)
This is how I used the procedure,
searchnclosedoc(dfilenamewithoutextension, "MSWORD")
searchnclosedoc(dfilenamewithoutextension, "notepad")
I also tried this,
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
Notepad is not doing well, it saves, sometime it also asked with dialog box, The Foxit Reader is Ok, not saving for NO, The msWORD is not saving and closes directly which I want to closed, saved, closed.
Solved with this code:( I CHANGE THE CODE ) Below.
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
Using this code for a moment: And its working fine.
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
Thanks
المحلول