当应用程序要求保存或不保存时,将消息发送或发送到记事本或msword对话框

时间:2013-06-06 12:15:24

标签: vb.net postmessage

下面的代码显示了如何按文件名关闭应用程序。 并且它在不保存文档的情况下关闭它, 如果我在记事本中指向文件名,它不会直接关闭,而是要求保存或不保存。 我想直接保存文档来关闭应用程序。

任何纠正代码的想法?

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

记事本做得不好,它保存了,有时它还会询问对话框,  福昕阅读器是好的,没有保存为否,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

由于

1 个答案:

答案 0 :(得分:1)

您是否尝试在发送WM_Close之前发送Ctrl-S?