<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Серый форум &mdash; AHK: Как соединить простое GUI и окно программы Excel]]></title>
	<link rel="self" href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=15579&amp;type=atom" />
	<updated>2020-08-12T20:22:59Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.script-coding.com/viewtopic.php?id=15579</id>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Как соединить простое GUI и окно программы Excel]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=141640#p141640" />
			<content type="html"><![CDATA[<p>Спасибо - теперь все нормально закрывается.</p>]]></content>
			<author>
				<name><![CDATA[Werwolf]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=39565</uri>
			</author>
			<updated>2020-08-12T20:22:59Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=141640#p141640</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Как соединить простое GUI и окно программы Excel]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=141638#p141638" />
			<content type="html"><![CDATA[<p>Ну не знаю тогда, у меня такого не происходит. Можно сразу убивать Эксель, без предложения сохранить:<br /></p><div class="codebox"><pre><code>QuitExcel() {
   if WinExist(&quot;ahk_id&quot; Info.hExcel) {
      Process, Close, % Info.PID
      Process, WaitClose, % Info.PID
   }
}</code></pre></div>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2020-08-12T19:41:09Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=141638#p141638</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Как соединить простое GUI и окно программы Excel]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=141636#p141636" />
			<content type="html"><![CDATA[<p>Закомментировал.<br />Все то же самое - мигающая кнопка закрытия Экселя.</p>]]></content>
			<author>
				<name><![CDATA[Werwolf]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=39565</uri>
			</author>
			<updated>2020-08-12T18:50:55Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=141636#p141636</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Как соединить простое GUI и окно программы Excel]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=141635#p141635" />
			<content type="html"><![CDATA[<p>Попробуйте пока закомментировать<br /></p><div class="codebox"><pre><code>Gui, % Info.hGui . &quot;: +AlwaysOnTop&quot;</code></pre></div><p>Возможно, появляется окно с предложением сохранить документ, но GUI его перекрывает.</p>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2020-08-12T18:41:48Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=141635#p141635</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Как соединить простое GUI и окно программы Excel]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=141634#p141634" />
			<content type="html"><![CDATA[<p>Заменил.<br />Но пока что-то не работает.<br />Нажимаю на крест закрытия GUI - и начинает мигать кнопка закрытия экселя.<br />Кнопка мигает, но эксель не закрывается. <br />То есть не предлагает сохранится перед закрытием, например (если документ не сохранен).</p><p>Щелкаю правой клавишей мыши на ярлык программы в панели задач - жму &quot;закрыть&quot;.<br />Тоже ничего не происходит - только мигание экселевской кнопки закрытия.</p>]]></content>
			<author>
				<name><![CDATA[Werwolf]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=39565</uri>
			</author>
			<updated>2020-08-12T18:39:09Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=141634#p141634</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Как соединить простое GUI и окно программы Excel]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=141630#p141630" />
			<content type="html"><![CDATA[<p>Замените QuitExcel() на такое:<br /></p><div class="codebox"><pre><code>QuitExcel() {
   if WinExist(&quot;ahk_id&quot; Info.hExcel) {
      try Info.Excel.Quit
      catch {
         Process, Close, % Info.PID
         Process, WaitClose, % Info.PID
      }
      WinWaitClose
      Gui, % Info.hGui . &quot;: Cancel&quot;
   }
   Process, Close, % Info.PID
   Process, WaitClose, % Info.PID
}</code></pre></div>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2020-08-12T17:51:49Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=141630#p141630</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Как соединить простое GUI и окно программы Excel]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=141619#p141619" />
			<content type="html"><![CDATA[<p>teadrinker, посоветуйте как сделать, чтобы при закрытии GUI (кнопка с крестиком) - оба окна закрывались ?</p><p>Сейчас - на окне GUI - кнопка закрытия окна - почему-то не работает.<br />А если жать на кнопку закрытия встроенного экселевского окна -&nbsp; то оно закрывается, а GUI - продолжает оставаться открытым.</p>]]></content>
			<author>
				<name><![CDATA[Werwolf]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=39565</uri>
			</author>
			<updated>2020-08-12T02:17:17Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=141619#p141619</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Как соединить простое GUI и окно программы Excel]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=141603#p141603" />
			<content type="html"><![CDATA[<p>teadrinker, спасибо.</p>]]></content>
			<author>
				<name><![CDATA[Werwolf]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=39565</uri>
			</author>
			<updated>2020-08-10T21:32:23Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=141603#p141603</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Как соединить простое GUI и окно программы Excel]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=141587#p141587" />
			<content type="html"><![CDATA[<p>Добавьте в AttachExcel():<br /></p><div class="codebox"><pre><code>AttachExcel() {
   static GWLP_HWNDPARENT := -8, eolc := EVENT_OBJECT_LOCATIONCHANGE := 0x800B
   DllCall(&quot;SetWindowLong&quot; . (A_PtrSize = 4 ? &quot;&quot; : &quot;Ptr&quot;), &quot;Ptr&quot;, Info.hExcel, &quot;UInt&quot;, GWLP_HWNDPARENT, &quot;Ptr&quot;, Info.hGui)
   WinMove, % &quot;ahk_id&quot; Info.hExcel,, Info.left, Info.top, Info.right - Info.left, Info.bottom - Info.top
   OnMessage(0x3, &quot;WM_MOVE&quot;)
   Info.Hook := new WinEventHook(eolc, eolc, &quot;OnExcelMove&quot;,, Info.PID)
   Gui, % Info.hGui . &quot;: +AlwaysOnTop&quot;
   WinActivate, % &quot;ahk_id&quot; Info.hExcel
}</code></pre></div>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2020-08-10T09:12:04Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=141587#p141587</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Как соединить простое GUI и окно программы Excel]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=141585#p141585" />
			<content type="html"><![CDATA[<div class="quotebox"><cite>teadrinker пишет:</cite><blockquote><p>Окно экселя всегда над нашими окном, так что остаётся разобраться с последним.</p></blockquote></div><p>Так в том-то и дело, что не получается.</p><p>под строчкой<br /></p><div class="codebox"><pre><code>CreateGui(1000, 800, &quot;MainGui&quot;)</code></pre></div><p>пишу<br /></p><div class="codebox"><pre><code>WinSet, AlwaysOnTop, on, MainGui</code></pre></div><p>или под строчкой<br /></p><div class="codebox"><pre><code>Gui, New, +hwndhGui +Resize +Label%label%, Excel</code></pre></div><p>пишу<br /></p><div class="codebox"><pre><code>WinSet, AlwaysOnTop, on, Excel</code></pre></div><p>Ни в том ни в другом случае - &quot;поверх всех окон&quot; не срабатывает.</p>]]></content>
			<author>
				<name><![CDATA[Werwolf]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=39565</uri>
			</author>
			<updated>2020-08-09T22:22:26Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=141585#p141585</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Как соединить простое GUI и окно программы Excel]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=141560#p141560" />
			<content type="html"><![CDATA[<p>Окно экселя всегда над нашими окном, так что остаётся разобраться с последним.</p>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2020-08-09T06:19:12Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=141560#p141560</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Как соединить простое GUI и окно программы Excel]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=141556#p141556" />
			<content type="html"><![CDATA[<p>Спасибо.<br />А можно сделать, чтобы это объединение из двух окон - отображалось поверх других окон ?<br />(Сейчас ведь неясно как это сделать - потому что это два окна, а не одно)</p>]]></content>
			<author>
				<name><![CDATA[Werwolf]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=39565</uri>
			</author>
			<updated>2020-08-08T23:12:34Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=141556#p141556</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Как соединить простое GUI и окно программы Excel]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=141554#p141554" />
			<content type="html"><![CDATA[<div class="codebox"><pre><code>#NoEnv
SetWinDelay, 0
SetBatchLines, -1
global Info := {}

filePath := &quot;D:\MyExcelFile.xlsm&quot;

CreateGui(1000, 800, &quot;MainGui&quot;)
GetExcel(filePath)
AttachExcel()
Return

CreateGui(width, height, label) {
   Gui, New, +hwndhGui +Resize +Label%label% -DPIScale, Excel
   Gui, Show, w%width% h%height%
   VarSetCapacity(WINDOWINFO, 60, 0)
   NumPut(60, WINDOWINFO)
   DllCall(&quot;GetWindowInfo&quot;, &quot;Ptr&quot;, hGui, &quot;Ptr&quot;, &amp;WINDOWINFO)
   for k, v in [&quot;left&quot;, &quot;top&quot;, &quot;right&quot;, &quot;bottom&quot;] {
      Info[v] := NumGet(WINDOWINFO, 16 + A_Index*4, &quot;UInt&quot;)
      Info[v . &quot;Offset&quot;] := Info[v] - NumGet(WINDOWINFO, A_Index*4, &quot;UInt&quot;)
   }
   Info.hGui := hGui
}

GetExcel(filePath := &quot;&quot;) {
   if filePath {
      Excel := ComObjCreate(&quot;Excel.Application&quot;)
      Book := Excel.Workbooks.Open(filePath)
      Excel.Visible := true
      WinWait, ahk_class XLMAIN
   }
   else if WinExist(&quot;ahk_class XLMAIN&quot;)
      Excel := Excel_Get()
   else {
      Excel := ComObjCreate(&quot;Excel.Application&quot;)
      Book := Excel.Workbooks.Add
      Excel.Visible := true
      WinWait, ahk_class XLMAIN
   }
   hExcel := Excel.hwnd
   WinGet, PID, PID, ahk_id %hExcel%
   for k, v in [&quot;Excel&quot;, &quot;hExcel&quot;, &quot;PID&quot;]
      Info[v] := %v%
   OnExit(Func(&quot;QuitExcel&quot;).Bind(Info))
}

AttachExcel() {
   static GWLP_HWNDPARENT := -8, eolc := EVENT_OBJECT_LOCATIONCHANGE := 0x800B
   DllCall(&quot;SetWindowLong&quot; . (A_PtrSize = 4 ? &quot;&quot; : &quot;Ptr&quot;), &quot;Ptr&quot;, Info.hExcel, &quot;UInt&quot;, GWLP_HWNDPARENT, &quot;Ptr&quot;, Info.hGui)
   WinMove, % &quot;ahk_id&quot; Info.hExcel,, Info.left, Info.top, Info.right - Info.left, Info.bottom - Info.top
   OnMessage(0x3, &quot;WM_MOVE&quot;)
   Info.Hook := new WinEventHook(eolc, eolc, &quot;OnExcelMove&quot;,, Info.PID)
   WinActivate, % &quot;ahk_id&quot; Info.hExcel
}

MainGuiClose() {
   ExitApp
}

MainGuiSize(hGui, eventInfo, width, height) {
   WinMove, % &quot;ahk_id&quot; Info.hExcel,,,, width, height
}

WM_MOVE(wp, lp, msg, hwnd) {
   static timer := Func(&quot;ClearTimout&quot;)
   if !(Info.hGui = hwnd)
      Return
   
   Info.timeout := true
   x := lp &amp; 0xFFFF, (x &gt; 0x7FFF &amp;&amp; x -= 0xFFFF)
   y := lp &gt;&gt; 16   , (y &gt; 0x7FFF &amp;&amp; y -= 0xFFFF)
   WinMove, % &quot;ahk_id&quot; Info.hExcel,, x, y
   SetTimer, % timer, -20
}

ClearTimout() {
   Info.timeout := false
}

OnExcelMove(hWinEventHook, event, hwnd, idObject) {
   if (hwnd != Info.hExcel) || Info.timeout
      Return
   
   WinGetPos, X, Y, W, H, % &quot;ahk_id&quot; . Info.hExcel
   WinMove, % &quot;ahk_id&quot; . Info.hGui,, X - Info.leftOffset, Y - Info.topOffset
                                   , W + Info.leftOffset - Info.rightOffset
                                   , H + Info.topOffset - Info.bottomOffset
}

QuitExcel() {
   if WinExist(&quot;ahk_id&quot; Info.hExcel) {
      Info.Excel.Quit
      WinWaitClose
      Gui, % Info.hGui . &quot;: Cancel&quot;
      Sleep, 1000
   }
   Process, Close, % Info.PID
   Process, WaitClose, % Info.PID
}

Excel_Get(WinTitle := &quot;ahk_class XLMAIN&quot;) {
   ControlGet, hwnd, hwnd,, Excel71, %WinTitle%
   if !hwnd
      return
   Window := AccObjectFromWindow(hwnd, -16)
   Loop {
      try Application := Window.Application
      catch
         ControlSend, Excel71, {esc}, %WinTitle%
   } until !!Application
   return Application
}

AccObjectFromWindow(hWnd, idObject = 0)
{
   static IID_IDispatch   := &quot;{00020400-0000-0000-C000-000000000046}&quot;
        , IID_IAccessible := &quot;{618736E0-3C3D-11CF-810C-00AA00389B71}&quot;
        , OBJID_NATIVEOM  := 0xFFFFFFF0, VT_DISPATCH := 9, F_OWNVALUE := 1
        , h := DllCall(&quot;LoadLibrary&quot;, Str, &quot;oleacc&quot;, Ptr)
        
   VarSetCapacity(IID, 16), idObject &amp;= 0xFFFFFFFF
   DllCall(&quot;ole32\CLSIDFromString&quot;, Str, idObject = OBJID_NATIVEOM ? IID_IDispatch : IID_IAccessible, Ptr, &amp;IID)
   if DllCall(&quot;oleacc\AccessibleObjectFromWindow&quot;, Ptr, hWnd, UInt, idObject, Ptr, &amp;IID, PtrP, pAcc) = 0
      Return ComObjEnwrap(VT_DISPATCH, pAcc, F_OWNVALUE)
}

class WinEventHook
{
   __New(eventMin, eventMax, hookProc, eventInfo := 0, idProcess := 0, idThread := 0, dwFlags := 0) {
      this.pCallback := RegisterCallback(hookProc, &quot;F&quot;,, eventInfo)
      this.hHook := DllCall(&quot;SetWinEventHook&quot;, &quot;UInt&quot;, eventMin, &quot;UInt&quot;, eventMax, &quot;Ptr&quot;, 0, &quot;Ptr&quot;, this.pCallback
                                             , &quot;UInt&quot;, idProcess, &quot;UInt&quot;, idThread, &quot;UInt&quot;, dwFlags, &quot;Ptr&quot;)
   }
   __Delete() {
      DllCall(&quot;UnhookWinEvent&quot;, &quot;Ptr&quot;, this.hHook)
      DllCall(&quot;GlobalFree&quot;, &quot;Ptr&quot;, this.pCallback, &quot;Ptr&quot;)
   }
}</code></pre></div>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2020-08-08T21:57:12Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=141554#p141554</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Как соединить простое GUI и окно программы Excel]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=141548#p141548" />
			<content type="html"><![CDATA[<p>teadrinker, спасибо.<br />Как раз то, что нужно.</p><p>Единственное что непонятно, как в код - вписать название файла xls, который должен там сразу открыться ?</p>]]></content>
			<author>
				<name><![CDATA[Werwolf]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=39565</uri>
			</author>
			<updated>2020-08-08T20:12:26Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=141548#p141548</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Как соединить простое GUI и окно программы Excel]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=141547#p141547" />
			<content type="html"><![CDATA[<p>Попробуйте такой код:<br /></p><div class="codebox"><pre><code>#NoEnv
SetWinDelay, 0
SetBatchLines, -1

global Info := {}

CreateGui(1000, 800, &quot;MainGui&quot;)
GetExcel()
AttachExcel()
Return

CreateGui(width, height, label) {
   Gui, New, +hwndhGui +Resize +Label%label%, Excel
   Gui, Show, w%width% h%height%
   VarSetCapacity(WINDOWINFO, 60, 0)
   NumPut(60, WINDOWINFO)
   DllCall(&quot;GetWindowInfo&quot;, &quot;Ptr&quot;, hGui, &quot;Ptr&quot;, &amp;WINDOWINFO)
   for k, v in [&quot;left&quot;, &quot;top&quot;, &quot;right&quot;, &quot;bottom&quot;] {
      Info[v] := NumGet(WINDOWINFO, 16 + A_Index*4, &quot;UInt&quot;)
      Info[v . &quot;Offset&quot;] := Info[v] - NumGet(WINDOWINFO, A_Index*4, &quot;UInt&quot;)
   }
   Info.hGui := hGui
}

GetExcel() {
   if WinExist(&quot;ahk_class XLMAIN&quot;)
      Excel := Excel_Get()
   else {
      Excel := ComObjCreate(&quot;Excel.Application&quot;)
      Book := Excel.Workbooks.Add
      Excel.Visible := true
      WinWait, ahk_class XLMAIN
   }
   hExcel := Excel.hwnd
   WinGet, PID, PID, ahk_id %hExcel%
   for k, v in [&quot;Excel&quot;, &quot;hExcel&quot;, &quot;PID&quot;]
      Info[v] := %v%
   OnExit(Func(&quot;QuitExcel&quot;).Bind(Info))
}

AttachExcel() {
   static GWLP_HWNDPARENT := -8, eolc := EVENT_OBJECT_LOCATIONCHANGE := 0x800B
   DllCall(&quot;SetWindowLong&quot; . (A_PtrSize = 4 ? &quot;&quot; : &quot;Ptr&quot;), &quot;Ptr&quot;, Info.hExcel, &quot;UInt&quot;, GWLP_HWNDPARENT, &quot;Ptr&quot;, Info.hGui)
   WinMove, % &quot;ahk_id&quot; Info.hExcel,, Info.left, Info.top, Info.right - Info.left, Info.bottom - Info.top
   OnMessage(0x3, &quot;WM_MOVE&quot;)
   Info.Hook := new WinEventHook(eolc, eolc, &quot;OnExcelMove&quot;,, Info.PID)
   WinActivate, % &quot;ahk_id&quot; Info.hExcel
}

MainGuiClose() {
   ExitApp
}

MainGuiSize(hGui, eventInfo, width, height) {
   WinMove, % &quot;ahk_id&quot; Info.hExcel,,,, width, height
}

WM_MOVE(wp, lp, msg, hwnd) {
   static timer := Func(&quot;ClearTimout&quot;)
   if !(Info.hGui = hwnd)
      Return
   
   Info.timeout := true
   x := lp &amp; 0xFFFF, (x &gt; 0x7FFF &amp;&amp; x -= 0xFFFF)
   y := lp &gt;&gt; 16   , (y &gt; 0x7FFF &amp;&amp; y -= 0xFFFF)
   WinMove, % &quot;ahk_id&quot; Info.hExcel,, x, y
   SetTimer, % timer, -20
}

ClearTimout() {
   Info.timeout := false
}

OnExcelMove(hWinEventHook, event, hwnd, idObject) {
   if (hwnd != Info.hExcel) || Info.timeout
      Return
   
   WinGetPos, X, Y, W, H, % &quot;ahk_id&quot; . Info.hExcel
   WinMove, % &quot;ahk_id&quot; . Info.hGui,, X - Info.leftOffset, Y - Info.topOffset
                                   , W + Info.leftOffset - Info.rightOffset
                                   , H + Info.topOffset - Info.bottomOffset
}

QuitExcel() {
   if WinExist(&quot;ahk_id&quot; Info.hExcel) {
      Info.Excel.Quit
      WinWaitClose
      Gui, % Info.hGui . &quot;: Cancel&quot;
      Sleep, 1000
   }
   Process, Close, % Info.PID
   Process, WaitClose, % Info.PID
}

Excel_Get(WinTitle := &quot;ahk_class XLMAIN&quot;) {
   ControlGet, hwnd, hwnd,, Excel71, %WinTitle%
   if !hwnd
      return
   Window := AccObjectFromWindow(hwnd, -16)
   Loop {
      try Application := Window.Application
      catch
         ControlSend, Excel71, {esc}, %WinTitle%
   } until !!Application
   return Application
}

AccObjectFromWindow(hWnd, idObject = 0)
{
   static IID_IDispatch   := &quot;{00020400-0000-0000-C000-000000000046}&quot;
        , IID_IAccessible := &quot;{618736E0-3C3D-11CF-810C-00AA00389B71}&quot;
        , OBJID_NATIVEOM  := 0xFFFFFFF0, VT_DISPATCH := 9, F_OWNVALUE := 1
        , h := DllCall(&quot;LoadLibrary&quot;, Str, &quot;oleacc&quot;, Ptr)
        
   VarSetCapacity(IID, 16), idObject &amp;= 0xFFFFFFFF
   DllCall(&quot;ole32\CLSIDFromString&quot;, Str, idObject = OBJID_NATIVEOM ? IID_IDispatch : IID_IAccessible, Ptr, &amp;IID)
   if DllCall(&quot;oleacc\AccessibleObjectFromWindow&quot;, Ptr, hWnd, UInt, idObject, Ptr, &amp;IID, PtrP, pAcc) = 0
      Return ComObjEnwrap(VT_DISPATCH, pAcc, F_OWNVALUE)
}

class WinEventHook
{
   __New(eventMin, eventMax, hookProc, eventInfo := 0, idProcess := 0, idThread := 0, dwFlags := 0) {
      this.pCallback := RegisterCallback(hookProc, &quot;F&quot;,, eventInfo)
      this.hHook := DllCall(&quot;SetWinEventHook&quot;, &quot;UInt&quot;, eventMin, &quot;UInt&quot;, eventMax, &quot;Ptr&quot;, 0, &quot;Ptr&quot;, this.pCallback
                                             , &quot;UInt&quot;, idProcess, &quot;UInt&quot;, idThread, &quot;UInt&quot;, dwFlags, &quot;Ptr&quot;)
   }
   __Delete() {
      DllCall(&quot;UnhookWinEvent&quot;, &quot;Ptr&quot;, this.hHook)
      DllCall(&quot;GlobalFree&quot;, &quot;Ptr&quot;, this.pCallback, &quot;Ptr&quot;)
   }
}</code></pre></div><p>Написано на скорую руку, не уверен, что каких-нибудь косяков не вылезет.</p>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2020-08-08T18:59:42Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=141547#p141547</id>
		</entry>
</feed>
