아래 코드는 파일 이름으로 응용 프로그램을 닫는 방법을 보여줍니다. 그리고 문서를 저장하지 않고 파일을 닫습니다. 메모장에서 파일 이름을 가리키면 저장하지 말고 직접 닫지 않습니다. 문서를 직접 저장하여 응용 프로그램을 닫고 싶습니다.응용 프로그램에서 저장할 것인지 묻는 메시지가 나타나면 메모장 또는 msword 대화 상자에 메시지 보내기 또는 게시
코드 수정을위한 아이디어가 있으십니까?
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를 닫습니다. 문서를 닫은 후에 저장하지 않습니다. 하지만 닫을 때까지 문서를 저장하고 싶습니다. 메모장에 저장 여부를 묻는 대화 상자가 나타납니다. 응용 프로그램을 닫고 대화 상자를 표시하지 않고 편집 한 문서를 직접 저장했습니다. 나는 또한이 시도
searchnclosedoc(dfilenamewithoutextension, "MSWORD")
searchnclosedoc(dfilenamewithoutextension, "notepad")
이 내가하는 절차를 사용하는 방법이다
PostMessage(wproc.MainWindowHandle, WM_Close, WM_KEYDOWN, Keys.Enter)
,
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
메모장이 잘되지
, 그것은 저장, 언젠가 그것은 또한 대화 상자가 물었다 , Foxit Reader은 좋아요, 저장 안함, 아니요, msWORD는 저장되지 않고 닫히고, 저장하고, 닫으려고 직접 닫습니다.
아래 코드에서 해결되었습니다. (I CHANGE THE CODE).
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
감사
그러나 Postmessage는 응용 프로그램을 포 그라운드로 설정하지 않고 메시지를 보내기 때문에 SendKeys보다는 Postmessage를 사용하는 것이 좋습니다. Postmessage와 함께 사용하는 방법을 궁금해. 감사합니다 – XXXXXXXXXXXXXX