-
createobject( internetexplorer.application ) new sessionProgramming/VBA(Visual Basic for Application) 2013. 11. 8. 15:49
VB에서 인터넷창을 여러개 켜서 각기 다른 세션으로 핸들해야하는데..
외국애들 자료 찾아봐도 아무도 이렇다할 해결방법이 없어서 몇일 삽질끝에 만들었음.
뭐 예외처리가 안된부분도 많지만.. 크게 문제없이 사용할수 있을것같아서 Post ㅋ
IE 새 세션을 여는방법은 (사용자기준) 3가지 정도..
1. IE > 파일 > 새 세션
2. 실행옵션(바로가기 옵션)을 아래방식으로 설정.
"C:\Program Files\Internet Explorer\iexplore.exe" -nomerge
3. 레지스트값 변경으로 설정하기.
always_new_session_setting.reg
찝찝하면 메모장열고 아래내용 넣은뒤 "파일명.reg"로 저장.
---------------------------------------------------------
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main]
"FrameMerging"=dword:00000000
"SessionMerging"=dword:00000000
---------------------------------------------------------
어쨋든.
기존에 createobject("internetexplorer.application") 방식이 아닌,
shell을 이용해서 새 세션을 Open한뒤 핸들링 할수있는 IWebBrowser 가져오기.
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _ (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long Private Declare Function WaitForSingleObject Lib "kernel32.dll" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Private Const INFINITE = &HFFFF ' 현재 열린 모든 Window창 닫기 Sub CloseWindowAll() Dim TempTitle As String Dim retValue As IWebBrowser Set objShell = CreateObject("Shell.Application") Set activewindows = objShell.Windows For i = 0 To activewindows.Count - 1 activewindows.Item(activewindows.Count - 1).Quit Next i End Sub ' Title을 가지고 IWebBrowser 가져오기 Function FindShellAppInTitle(Title As String) As IWebBrowser Dim TempTitle As String Dim retValue As IWebBrowser Set objShell = CreateObject("Shell.Application") Set activewindows = objShell.Windows For i = 0 To activewindows.Count - 1 TempTitle = activewindows.Item(activewindows.Count - 1).LocationName If (0 <> InStr(1, TempTitle, Title, vbTextCompare)) Then Set retValue = activewindows.Item(activewindows.Count - 1) 'Debug.Print "찾았음" End If Next i Set FindShellAppInTitle = retValue End Function Function CreateNewSessionIE(url As String, VisibleValue As Boolean) As IWebBrowser 'Shell을 이용해 새세션("-nomerge"옵션) 으로 explore 실행 Shell """C:\Program Files\Internet Explorer\iexplore.exe"" -nomerge ""about:blank""", vbHide 'process_id = Shell("""C:\Program Files\Internet Explorer\iexplore.exe"" -nomerge ""about:blank""", vbMinimizedNoFocus) '창이 완전히 열릴때까지 wait.. 시스템마다 튜닝필요.. Shell함수가 틀림;; Sleep 500 'Not use.. 'process_handle = OpenProcess(Synchronize, 0, process_id) 'If process_handle <> 0 Then ' WaitForSingleObject process_handle, INFINITE ' CloseHandle process_handle 'End If Set TempWindow = FindShellAppInTitle("about:blank") ' Get New iexplore TempWindow.Navigate2 url ' url setting TempWindow.Visible = VisibleValue ' Visible setting 'Wait loading.... Do While TempWindow.Busy Or TempWindow.readyState <> READYSTATE_COMPLETE 'Debug.Print "." DoEvents Loop 'Debug.Print "---------------완료" Set CreateNewSessionIE = TempWindow End Function Sub NewWindowTest() Dim tempStr As String Set IE1 = CreateNewSessionIE("http://www.naver.com", True) Set IE2 = CreateNewSessionIE("http://www.google.com", True) tempStr = IE1.LocationName tempStr = tempStr & Chr(13) tempStr = tempStr & IE2.LocationName MsgBox tempStr IE1.Quit IE2.Quit End Sub