{"id":2379,"date":"2022-05-14T18:43:13","date_gmt":"2022-05-14T09:43:13","guid":{"rendered":"http:\/\/oreoreki.gotdns.ch\/?p=2379"},"modified":"2022-06-01T14:06:58","modified_gmt":"2022-06-01T05:06:58","slug":"post-2379","status":"publish","type":"post","link":"http:\/\/oreoreki.gotdns.ch\/?p=2379","title":{"rendered":"VBA\u3067Windows\u30a2\u30d7\u30ea\u300c\u30e1\u30e2\u5e33\u300d\u3092\u64cd\u4f5c\u3057\u3066\u307f\u305f\u30fbUI Automation"},"content":{"rendered":"<p><span class=\"embed-youtube\" style=\"text-align:center; display: block;\"><iframe loading=\"lazy\" class=\"youtube-player\" width=\"750\" height=\"422\" src=\"https:\/\/www.youtube.com\/embed\/DL9MuwJfXVw?version=3&#038;rel=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;fs=1&#038;hl=ja&#038;autohide=2&#038;wmode=transparent\" allowfullscreen=\"true\" style=\"border:0;\" sandbox=\"allow-scripts allow-same-origin allow-popups allow-presentation allow-popups-to-escape-sandbox\"><\/iframe><\/span><\/p>\n<h2><\/h2>\n<div class=\"hcb_wrap\">\n<p>VBA\u3067Windows\u30a2\u30d7\u30ea\u300c\u30e1\u30e2\u5e33\u300d\u3092\u64cd\u4f5c\u3057\u3066\u307f\u305f\u30fbUI Automation<br \/>\n<span class=\"bold-red\">\u3053\u306e\u52d5\u753b\u306e\u30de\u30af\u30ed\u306fWindows11\/Office2013\u3067\u4f5c\u6210\u3057\u3066\u307e\u3059\u3002<br \/>\n\u30e1\u30e2\u5e33\u306eid\u3068\u304b\u306fWindows11\u3068\u305d\u308c\u4ee5\u524d\u306eWindows\u3068\u3067\u306f\u7570\u306a\u3063\u3066\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u306e\u3067\u3001<br \/>\nWindows11\u4ee5\u5916\u3067\u52d5\u304b\u3059\u5834\u5408\u306fInspect.exe\u3067id\u3092\u78ba\u8a8d\u3057\u30b3\u30fc\u30c9\u4fee\u6b63\u3092\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/span><\/p>\n<pre class=\"prism line-numbers lang-ts\" data-lang=\"TypeScript\"><code>Option Explicit\r\n' 32\u30d3\u30c3\u30c8\u7248Excel\u306e\u5834\u5408\u306fPtrSafe\u306f\u4e0d\u8981\u3067\u3059\u306e\u3067\u3001\u524a\u9664\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002\r\nPrivate Declare PtrSafe Function FindWindowA Lib \"user32\" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long\r\nPrivate Declare PtrSafe Function FindWindowEx Lib \"User32.dll\" Alias \"FindWindowExA\" (ByVal hWndParent As Long, ByVal hWndChildAfter As Long, ByVal lpszClass As String, ByVal lpszWindow As String) As Long\r\nPrivate Declare PtrSafe Sub Sleep Lib \"kernel32\" (ByVal dwMilliseconds As Long)\r\nPrivate Declare PtrSafe Function MessageBoxTimeoutA Lib \"user32\" (ByVal hWnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal uType As Long, ByVal wLanguageID As Long, ByVal dwMilliseconds As Long) As Long\r\n\r\nSub Memo()\r\n    Dim str_Title, i, fso, CurrentDirectory, xFikename\r\n    Dim hWnd As Long\r\n    Dim uiAuto As CUIAutomation\r\n    Dim iCnd As IUIAutomationCondition\r\n    Dim iValuePattern As IUIAutomationValuePattern\r\n    '---------------------------------------------------------------------------\r\n    '\u30a8\u30af\u30bb\u30eb\u30d5\u30a1\u30a4\u30eb\u306e\u3042\u308b\u30d1\u30b9\u540d\u3092\u53d6\u5f97\r\n    Set fso = CreateObject(\"Scripting.FileSystemObject\")\r\n    CurrentDirectory = ActiveWorkbook.Path\r\n    \r\n    '---------------------------------------------------------------------------\r\n    ' \u30e1\u30e2\u5e33\u306e\u8d77\u52d5\r\n        Call Shell(\"notepad.exe\", vbNormalFocus)\r\n        '1\u79d2\u505c\u6b62\r\n        Call Sleep(1000)\r\n        \r\n        '\u30bf\u30a4\u30c8\u30eb\u3067\u30a2\u30d7\u30ea\u3092\u898b\u3064\u3051\u308b\r\n        'hWnd = FindWindowA(vbNullString, \"\u30bf\u30a4\u30c8\u30eb\u306a\u3057 - \u30e1\u30e2\u5e33\")\r\n        '\u30af\u30e9\u30b9\u540d\u3067\u63a2\u3059\r\n        hWnd = FindWindowEx(0, 0, \"Notepad\", vbNullString)\r\n        If hWnd = 0 Then\r\n            Call MessageBoxTimeoutA(0&amp;, \"\u30e1\u30e2\u5e33\u304c\u307f\u3064\u304b\u308a\u307e\u305b\u3093\", \"hWnd\u30a8\u30e9\u30fc\", vbMsgBoxSetForeground, 0&amp;, 10000)\r\n            Exit Sub\r\n        End If\r\n    \r\n        '\u30e1\u30e2\u5e33\u306e\u300dWindow\u306e\u30a8\u30ec\u30e1\u30f3\u30c8\u3092\u63a2\u3059\r\n        Dim elmMemocho As IUIAutomationElement\r\n    \r\n        Set uiAuto = New CUIAutomation\r\n        Set elmMemocho = uiAuto.ElementFromHandle(ByVal hWnd)\r\n        If elmMemocho Is Nothing Then\r\n            i = MessageBoxTimeoutA(0&amp;, \"TimelineView\" &amp; Chr(13) &amp; \"\u304c\u307f\u3064\u304b\u308a\u307e\u305b\u3093\", \"FindFirst\u30a8\u30e9\u30fc\", vbMsgBoxSetForeground, 0&amp;, 10000)\r\n            Exit Sub\r\n        End If\r\n    \r\n    '---------------------------------------------------------------------------\r\n    '\u30e1\u30e2\u5e33\u306b\u6587\u5b57\u5217\u3092\u66f8\u304f\r\n        Dim elmMemocho_edit As IUIAutomationElement\r\n        Set iCnd = uiAuto.CreatePropertyCondition(UIA_ClassNamePropertyId, \"RichEditD2DPT\")\r\n        Set elmMemocho_edit = elmMemocho.FindFirst(TreeScope_Subtree, iCnd)\r\n        Set iValuePattern = elmMemocho_edit.GetCurrentPattern(UIA_ValuePatternId)\r\n        ' B1,B2\u30bb\u30eb\u306e\u5185\u5bb9\u3092\u30e1\u30e2\u5e33\u306b\u66f8\u304f\r\n        iValuePattern.SetValue Range(\"B1\").Value &amp; Chr(13) &amp; Range(\"B2\").Value\r\n        ' \u30e1\u30e2\u5e33\u306e\u7de8\u96c6\u5185\u5bb9\u3092\u898b\u308b\u306b\u306fCurrentValue\r\n        'Debug.Print iValuePattern.CurrentValue\r\n    \r\n    '---------------------------------------------------------------------------\r\n    '\u540d\u524d\u3092\u4ed8\u3051\u3066\u4fdd\u5b58\u3059\u308b\r\n    '\r\n        '\u300c\u30d5\u30a1\u30a4\u30eb\u300d\u306e\u30e1\u30cb\u30e5\u30fc\u306e\u30a8\u30ec\u30e1\u30f3\u30c8\u3092\u63a2\u3059\r\n        Dim elmMemocho_m0 As IUIAutomationElement\r\n        Dim elmMemocho_m1 As IUIAutomationElement\r\n        Dim elmMemocho_m2 As IUIAutomationElement\r\n        Dim elmMemocho_m3 As IUIAutomationElement\r\n        \r\n        Set iCnd = uiAuto.CreatePropertyCondition(UIA_ClassNamePropertyId, \"Windows.UI.Input.InputSite.WindowClass\")\r\n        Set elmMemocho_m0 = elmMemocho.FindFirst(TreeScope_Subtree, iCnd)\r\n        \r\n        Set iCnd = uiAuto.CreatePropertyCondition(UIA_AutomationIdPropertyId, \"MenuBar\")\r\n        Set elmMemocho_m1 = elmMemocho_m0.FindFirst(TreeScope_Subtree, iCnd)\r\n    \r\n        Set iCnd = uiAuto.CreatePropertyCondition(UIA_NamePropertyId, \"\u30d5\u30a1\u30a4\u30eb\")\r\n        Set elmMemocho_m2 = elmMemocho_m1.FindFirst(TreeScope_Subtree, iCnd)\r\n        \r\n        '\u300c\u30d5\u30a1\u30a4\u30eb\u300d\u306e\u30e1\u30cb\u30e5\u30fc\u3092\u62bc\u3059\r\n        Dim InvokePattern As IUIAutomationInvokePattern\r\n        Set InvokePattern = elmMemocho_m2.GetCurrentPattern(UIA_InvokePatternId)\r\n        elmMemocho_m2.SetFocus\r\n        InvokePattern.Invoke\r\n        \r\n        '\u300c\u30d5\u30a1\u30a4\u30eb\u300d\u306e\u30e1\u30cb\u30e5\u30fc\u914d\u4e0b\u306e\u300c\u540d\u524d\u3092\u4ed8\u3051\u3066\u4fdd\u5b58\u300d\u306e\u30a8\u30ec\u30e1\u30f3\u30c8\u3092\u63a2\u3059\r\n    \r\n        Set iCnd = uiAuto.CreatePropertyCondition(UIA_NamePropertyId, \"\u540d\u524d\u3092\u4ed8\u3051\u3066\u4fdd\u5b58\")\r\n        Do\r\n            Set elmMemocho_m3 = elmMemocho.FindFirst(TreeScope_Subtree, iCnd)\r\n            DoEvents\r\n        Loop While elmMemocho_m3 Is Nothing\r\n        \r\n        '\u300c\u540d\u524d\u3092\u4ed8\u3051\u3066\u4fdd\u5b58\u300d\u306e\u30e1\u30cb\u30e5\u30fc\u3092\u62bc\u3059\r\n        \r\n        elmMemocho_m3.SetFocus\r\n        Set InvokePattern = elmMemocho_m3.GetCurrentPattern(UIA_InvokePatternId)\r\n        InvokePattern.Invoke\r\n    \r\n    '---------------------------------------------------------------------------\r\n    '  \u300c\u540d\u524d\u3092\u4ed8\u3051\u3066\u4fdd\u5b58\u300dWindow\u306e\u51e6\u7406\r\n    '  \u3053\u306e\u30a8\u30af\u30bb\u30eb\u30d5\u30a1\u30a4\u30eb\u3068\u540c\u3058\u30d5\u30a9\u30eb\u30c0\u30fc\u306b\"abc.txt\"\u3068\u3044\u3046\u540d\u524d\u3067\u4fdd\u5b58\u3059\u308b\r\n    '\r\n    \r\n        '\u4fdd\u5b58\u3057\u3088\u3046\u3068\u3059\u308b\u30d5\u30a1\u30a4\u30eb\u304c\u5b58\u5728\u3057\u3066\u308b\u3068\u3001\u4e0a\u66f8\u304d\u3092\u805e\u3044\u3066\u304f\u308b\u306e\u3067\r\n        '\u4e88\u3081\u30d5\u30a1\u30a4\u30eb\u304c\u3042\u3063\u305f\u3089\u524a\u9664\u3057\u3066\u304a\u304f\u3002\r\n        xFikename = fso.BuildPath(CurrentDirectory, \"abc.txt\")\r\n        If fso.FileExists(xFikename) Then\r\n            fso.DeleteFile xFikename, True\r\n        End If\r\n    \r\n        '\u300c\u540d\u524d\u3092\u4ed8\u3051\u3066\u4fdd\u5b58\u300dWindow\u306e\u30a8\u30ec\u30e1\u30f3\u30c8\u3092\u63a2\u3059\r\n        Dim elmMemocho_s0 As IUIAutomationElement\r\n        Dim elmMemocho_s1 As IUIAutomationElement\r\n        Dim elmMemocho_s2 As IUIAutomationElement\r\n        Set iCnd = uiAuto.CreatePropertyCondition(UIA_NamePropertyId, \"\u540d\u524d\u3092\u4ed8\u3051\u3066\u4fdd\u5b58\")\r\n        Do\r\n            Set elmMemocho_s0 = elmMemocho.FindFirst(TreeScope_Subtree, iCnd)\r\n            DoEvents\r\n        Loop While elmMemocho_s0 Is Nothing\r\n        \r\n        Set iCnd = uiAuto.CreatePropertyCondition(UIA_ClassNamePropertyId, \"DUIViewWndClassName\")\r\n        Set elmMemocho_s1 = elmMemocho_s0.FindFirst(TreeScope_Subtree, iCnd)\r\n        Set iCnd = uiAuto.CreatePropertyCondition(UIA_ClassNamePropertyId, \"AppControlHost\")\r\n        Set elmMemocho_s2 = elmMemocho_s1.FindFirst(TreeScope_Subtree, iCnd)\r\n        \r\n        Set iValuePattern = elmMemocho_s2.GetCurrentPattern(UIA_ValuePatternId)\r\n        '\u30d5\u30a1\u30a4\u30eb\u540d\u306e\u9001\u4fe1\r\n        iValuePattern.SetValue xFikename\r\n        \r\n        '\u4fdd\u5b58\u30dc\u30bf\u30f3\u3092\u62bc\u3059\r\n        Set iCnd = uiAuto.CreatePropertyCondition(UIA_NamePropertyId, \"\u4fdd\u5b58(S)\")\r\n        Set elmMemocho_s1 = elmMemocho_s0.FindFirst(TreeScope_Subtree, iCnd)\r\n        elmMemocho_s1.SetFocus\r\n        Set InvokePattern = elmMemocho_s1.GetCurrentPattern(UIA_InvokePatternId)\r\n        InvokePattern.Invoke\r\n    \r\n    '---------------------------------------------------------------------------\r\n    '\u30e1\u30e2\u5e33\u306e\u7d42\u4e86\r\n        '\u300c\u30d5\u30a1\u30a4\u30eb\u300d\u306e\u30e1\u30cb\u30e5\u30fc\u3092\u62bc\u3059\r\n        Set InvokePattern = elmMemocho_m2.GetCurrentPattern(UIA_InvokePatternId)\r\n        elmMemocho_m2.SetFocus\r\n        InvokePattern.Invoke\r\n        \r\n        '\u300c\u30d5\u30a1\u30a4\u30eb\u300d\u306e\u30e1\u30cb\u30e5\u30fc\u914d\u4e0b\u306e\u300c\u540d\u524d\u3092\u4ed8\u3051\u3066\u4fdd\u5b58\u300d\u306e\u30a8\u30ec\u30e1\u30f3\u30c8\u3092\u63a2\u3059\r\n    \r\n        Set iCnd = uiAuto.CreatePropertyCondition(UIA_NamePropertyId, \"\u7d42\u4e86\")\r\n        Do\r\n            Set elmMemocho_m3 = elmMemocho.FindFirst(TreeScope_Subtree, iCnd)\r\n            DoEvents\r\n        Loop While elmMemocho_m3 Is Nothing\r\n        \r\n        '\u300c\u7d42\u4e86\u300d\u306e\u30e1\u30cb\u30e5\u30fc\u3092\u62bc\u3059\r\n        elmMemocho_m3.SetFocus\r\n        Set InvokePattern = elmMemocho_m3.GetCurrentPattern(UIA_InvokePatternId)\r\n        InvokePattern.Invoke\r\n\r\nEnd Sub\r\n<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>VBA\u3067Windows\u30a2\u30d7\u30ea\u300c\u30e1\u30e2\u5e33\u300d\u3092\u64cd\u4f5c\u3057\u3066\u307f\u305f\u30fbUI Automation \u3053\u306e\u52d5\u753b\u306e\u30de\u30af\u30ed\u306f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2382,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[34],"tags":[],"class_list":["post-2379","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-vba"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"http:\/\/oreoreki.gotdns.ch\/wp-content\/uploads\/sites\/2\/2022\/05\/mqdefault-5.jpg","jetpack_shortlink":"https:\/\/wp.me\/p9WqRX-Cn","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/oreoreki.gotdns.ch\/index.php?rest_route=\/wp\/v2\/posts\/2379","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/oreoreki.gotdns.ch\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/oreoreki.gotdns.ch\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/oreoreki.gotdns.ch\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/oreoreki.gotdns.ch\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2379"}],"version-history":[{"count":5,"href":"http:\/\/oreoreki.gotdns.ch\/index.php?rest_route=\/wp\/v2\/posts\/2379\/revisions"}],"predecessor-version":[{"id":2401,"href":"http:\/\/oreoreki.gotdns.ch\/index.php?rest_route=\/wp\/v2\/posts\/2379\/revisions\/2401"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/oreoreki.gotdns.ch\/index.php?rest_route=\/wp\/v2\/media\/2382"}],"wp:attachment":[{"href":"http:\/\/oreoreki.gotdns.ch\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/oreoreki.gotdns.ch\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2379"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/oreoreki.gotdns.ch\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}