Vb.net 当应用程序要求保存或不保存时,向记事本或msword对话框发送或发布消息

Vb.net 当应用程序要求保存或不保存时,向记事本或msword对话框发送或发布消息,vb.net,postmessage,Vb.net,Postmessage,下面的代码显示了如何按文件名关闭应用程序。 它会在不保存文档的情况下关闭它, 如果我在记事本中指向一个文件名,它不会直接关闭,而是询问是否保存。 我想通过直接保存文档来关闭应用程序 有什么办法来纠正代码吗 Const WM_Close As UInteger = &H10 Const WM_KEYDOWN = &H100 Const WM_COMMAND = &H112 Private Sub searchnclosedoc(ByVal noextfil

下面的代码显示了如何按文件名关闭应用程序。 它会在不保存文档的情况下关闭它, 如果我在记事本中指向一个文件名,它不会直接关闭,而是询问是否保存。 我想通过直接保存文档来关闭应用程序

有什么办法来纠正代码吗

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,而不要求是否保存。 关闭后不会保存文档。 但我希望在关闭之前保存文档。 在记事本中会出现一个对话框,询问是否保存。 我想关闭应用程序并直接保存已编辑的文档,而不显示diaglog框

    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
暂时使用一下这个代码:它工作得很好

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

谢谢

在发送WM\u Close之前您是否尝试过发送Ctrl-S?

哦,是的,没错,发送Ctrl-S,好的,我会尝试。thanksPostMessage(wproc.MainWindowHandle,WM_KEYDOWN,Keys.Control,0)PostMessage(wproc.MainWindowHandle,WM_KEYDOWN,Keys.S,0)PostMessage(wproc.MainWindowHandle,WM_Close,0,0)请阅读本文:我也尝试过这个方法,但它只在应用程序未隐藏的情况下有效,或者应用程序位于任务栏中。SetForeGroundWindowHandle(wproc.MainWindowHandle)SendKeys.SendWait(“^(s)”)SendKeys.SendWait(“%{F4}”)PostMessage(wproc.MainWindowHandle,WM_Close,0,0)但是,最好使用PostMessage而不是SendKeys,因为PostMessage发送消息时没有将应用程序设置为前台。我想知道如何在Postmessage中使用它。谢谢
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