{"id":3614,"date":"2023-08-24T22:59:32","date_gmt":"2023-08-24T14:59:32","guid":{"rendered":"https:\/\/www.bytetape.com\/?p=3614"},"modified":"2023-09-01T22:44:41","modified_gmt":"2023-09-01T14:44:41","slug":"%e4%bd%bf%e7%94%a8vba%e4%bb%a3%e7%a0%81%e8%bd%bb%e6%9d%be%e5%ae%9a%e4%bd%8d%e7%bd%91%e9%a1%b5%e5%85%83%e7%b4%a0%e7%9a%84%e7%bb%9d%e4%bd%b3%e6%96%b9%e6%b3%95%ef%bc%81","status":"publish","type":"post","link":"https:\/\/www.bytetape.com\/?p=3614","title":{"rendered":"\u4f7f\u7528VBA\u4ee3\u7801\u8f7b\u677e\u5b9a\u4f4d\u7f51\u9875\u5143\u7d20\u7684\u7edd\u4f73\u65b9\u6cd5\uff01"},"content":{"rendered":"\n<p>\u5927\u5bb6\u597d\uff0c\u6b22\u8fce\u6765\u5230\u6211\u7684\u535a\u5ba2\uff01\u4eca\u5929\u6211\u8981\u548c\u5927\u5bb6\u5206\u4eab\u4e00\u4e2a\u5173\u4e8e\u4f7f\u7528VBA\u4ee3\u7801\u5b9a\u4f4d\u7f51\u9875\u5143\u7d20\u7684\u6280\u5de7\uff0c\u8ba9\u4f60\u7684\u7f51\u9875\u81ea\u52a8\u5316\u5904\u7406\u66f4\u52a0\u5f97\u5fc3\u5e94\u624b\u3002\u4e0d\u7ba1\u4f60\u662f\u60f3\u81ea\u52a8\u586b\u5199\u8868\u5355\uff0c\u8fd8\u662f\u6293\u53d6\u7279\u5b9a\u4fe1\u606f\uff0c\u8fd9\u4e9b\u6280\u5de7\u90fd\u4f1a\u8ba9\u4f60\u4e8b\u534a\u529f\u500d\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u80cc\u666f\u77e5\u8bc6<\/h3>\n\n\n\n<p>\u5728\u8fdb\u884cVBA\u81ea\u52a8\u5316\u64cd\u4f5c\u65f6\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u4f7f\u7528Internet Explorer\uff08IE\uff09\u6765\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a\u3002\u8fd9\u7bc7\u6587\u7ae0\u5c06\u7740\u91cd\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528VBA\u4ee3\u7801\u5b9a\u4f4d\u7f51\u9875\u4e0a\u7684\u5143\u7d20\uff0c\u4ee5\u4fbf\u6211\u4eec\u53ef\u4ee5\u4e0e\u4e4b\u4ea4\u4e92\u3002<br>\u5bf9\u4e8e\u521d\u5b66\u8005\u6765\u8bf4\uff0c\u5728\u4e86\u89e3\u4e86DOM\u7684\u7ed3\u6784\u4e4b\u540e\u9996\u5148\u8981\u5b66\u4f1a\u7684\u5c31\u662f\u5982\u4f55\u627e\u5230\u5143\u7d20\uff0c\u4e0b\u9762\u4ee3\u7801\u662f\u5b9a\u4f4d\u5404\u7c7b\u5143\u7d20\u7684\u65b9\u6cd5\u793a\u4f8b\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"visualbasic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\u5b9a\u4f4d\u5143\u7d20\n\u83b7\u53d6\u5143\u7d20options\u6240\u5728\u7684document\nSet oDom = options.document\n\u83b7\u53d6\u5143\u7d20options\u6240\u5728\u7684IE\u5bf9\u8c61\nSet oIE = options.document.parentWindow\n\u83b7\u53d6document\u5185\u5bb9\uff1adocument.body.innerhtml\nSet repList = doc.getElementsByClassName('reportList') 'only works in IE9, not in IE8 \nSet repList = doc.querySelectorAll('.reportList') 'this works in IE8+\n\u5bf9\u4e8e&lt;input name=\"username\" id=\"username_id\" class=\"gray\">\u83b7\u5f97tag\u7684\u51e0\u79cd\u65b9\u6cd5\uff1a\nWebBrowser1.Document.getElementsByName(\"username\")\nWebBrowser1.Document.getElementById(\"username_id\")\nWebBrowser1.Document.getElementsByTagName(\"input\")(0)\nWebBrowser1.Document.getElementsByTagName(\"input\")(\"username\")\nWebBrowser1.Document.getElementsByTagName(\"input\")(\"username_id\")\nWebBrowser1.Document.all(0)\nWebBrowser1.Document.all(\"username\")\nWebBrowser1.Document.all(\"username_id\")\nWebBrowser1.Document.all.item(\"username\")\nWebBrowser1.Document.all.item(\"username_id\")\nWebBrowser1.Document.all.tags(\"input\")(0)\n\nTag\n\u83b7\u5f97\u67d0\u4e00\u7c7btag\u7684\u96c6\u5408\u7684\u65b9\u6cd5\uff1a\nWebBrowser1.Document.all.tags(\"input\")\n\u53e6\u5916\u90e8\u5206tag\u6709\u81ea\u5df1\u72ec\u6709\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982\uff1a\nWebBrowser1.Document.links\nWebBrowser1.Document.frames\nWebBrowser1.Document.forms\nWebBrowser1.Document.images\nWebBrowser1.Document.scripts\nWebBrowser1.Document.all.tags(\"select\").options\n\n\u8868\u683c\n\u884c\u662ftable.rows\uff0c\u5217\u5219\u662ftable.rows(0).cells\n.document.getElementsByclassname(\"x-grid3-row-table \")(3).Rows(0).Cells(4).innertext\n\u83b7\u53d6\u7f51\u9875\uff08\u8868\u683c\uff09\u8d85\u94fe\u63a5\nFor i = 0 To TableX.Rows.Length - 1\n    Set Row = TableX.Rows(i)\n    '\u904d\u5386\u6bcf\u884c\u6240\u6709\u8868\u683c\n    For j = 0 To Row.Cells.Length - 1\n        Rng.Cells(i + 1, j + 1) = Row.Cells(j).innerText\n    Next\nNext\n\n\u5bf9\u8c61\nDOM(Document Object Model)\u4e00\u822c\u5305\u542b\u4ee5\u4e0b\u51e0\u79cd\u5bf9\u8c61\uff1a\nDocument\uff1a\u6587\u6863\u5bf9\u8c61\nElement\uff1a\u5143\u7d20\u5bf9\u8c61\nAttribute\uff1a\u5c5e\u6027\u5bf9\u8c61\nText\uff1a\u6587\u672c\u5bf9\u8c61\nComment:\u6ce8\u91ca\u5bf9\u8c61\nNode\uff1a\u8282\u70b9\u5bf9\u8c61\uff0c\u5176\u4ed65\u4e2a\u7684\u7236\u5bf9\u8c61\n\n\u633a\u597d\u7684\u4f8b\u5b50\uff0cDOM\u65b9\u5f0f\u521b\u5efa\u4e00\u4e2aXML\u6587\u4ef6\nhttps:\/\/www.cnblogs.com\/zzstone\/p\/5562543.html\n\n\u4e00\u822c\u8bf4\u6765\uff0c\u6211\u4eec\u60f3\u8981\u7684\u7f51\u9875\u5185\u5bb9\uff0c\u5c31\u5728WebBrowser1.document.body.innerhtml\u548cWebBrowser1.document.body.innertext\u4e2d\uff0c\u901a\u8fc7\u66ff\u6362\u3001\u5206\u6790\uff0c\u5f97\u5230\u6211\u4eec\u60f3\u8981\u7684\u6570\u636e\u3002\nInnerhtml\u4e2d\u7684\u5185\u5bb9\u5c31\u662f\u7f51\u9875\u6e90\u7801\u3002\ninnertext\u4e2d\u7684\u5185\u5bb9\u5219\u662f\u7f51\u9875\u4e0a\u6240\u663e\u793a\u7684\u5185\u5bb9\u3002\n\n\u4e0b\u62c9\u83dc\u5355\n\u83b7\u53d6\u9009\u4e2d\u9879\u7684\u5185\u5bb9\n'\u627e\u5230\u4e0b\u62c9\u5217\u8868\u5bf9\u8c61 Set selectEle = doc.getElementById(\"UCBody1_ddlproblemgrade\") '\u83b7\u53d6\u9009\u4e2d\u7684\u9009\u9879\u5185\u5bb9 Dim selectedText As String selectedText = selectEle.item(selectEle.selectedIndex).innerText\n\n\u6211\u81ea\u5df1\u7684\u4e0b\u62c9\u83dc\u5355\u7a0b\u5e8f\uff0c\u8bbe\u7f6e\u9009\u4e2d\u67d0\u4e00\u9879\nPrivate Sub selectOption(options As Object, title As String)\n    For Each opt In options\n        If opt.title = title Then\n            opt.Selected = True\n        Else\n            opt.Selected = False\n        End If\n    Next\n    If Not IsNull(options.getAttribute(\"onchange\")) Then options.onchange '\u53ea\u6709\u5177\u6709onchange\u5c5e\u6027\u7684\u4e0b\u62c9\u83dc\u5355\u518d\u8fd0\u884conchange\n    Application.Wait Now + TimeSerial(0, 0, 1)\nEnd Sub\n\u83b7\u5f97\u4e0b\u62c9\u83dc\u5355\u91cc\u7684\u503c\uff1a\n.Document.getElementById(\"selectProductClassify\").All.tags(\"option\")(0).Text\n\u9009\u62e9\u4e0b\u62c9\u83dc\u5355\n.document.All.tags(\"OPTION\")(54).Selected = True\n\n\u5355\u9009\u6309\u94aeradio\n.getElementById(\"UCHead_rbtnMaterial_1\").Checked = True '\u662f\u5426\u6765\u81ea\u9884\u9009\u5e93\uff0c\u9009\u5426\n.getElementById(\"UCHead_rbtnMaterial_1\").Click\n\n\u6846\u67b6\u7ed3\u6784iframe\nset iframe=.document.getelementbyid(\"ueditor_0\")\niframe.contentwindow.document.getelementbyid(\"SELECT1618565954619\").options(2).selected=true\n\u6846\u67b6\u7f51\u9875\u5355\u51fb\u94fe\u63a5:\n.document.frames(0).document.getelementbyid(\"A5\").Click\n\n\u5c5e\u6027\n\u8bbe\u7f6e\u5c5e\u6027\n.getElementById(\"UCHead$txtRemark\").setAttribute \"style\", \"background-color:red;color:white;\"\n\u8c03\u7528js\u5b9e\u73b0\n.document.parentWindow.execScript (\"document.getElementById('UCHead_txtRemark').setAttribute('style','background-color:red;color:white;');\")\n\u83b7\u53d6\u5c5e\u6027\u503c\n\u83b7\u5f97tag\u7684Value\u503c\uff0c\u548c\u4e0a\u9762\u7c7b\u4f3c\u4e5f\u6709\u4e24\u79cd\u65b9\u6cd5\uff1a\nvtag.src\nvtag.getAttribute(\"src\")\nHTMLElement.getAttribute(\"data-elm-id\") = \"field_property_type\"\n\n\u7236\u5b50\u8282\u70b9\n\u589e\u52a0\u548c\u79fb\u9664\u8282\u70b9\nSet nd = Document.appendChild(el)\nDocument.removeChild nd\n\u524d\u8005\u8fd4\u56de\u4e0b\u4e00\u4e2a\u540c\u80de,\u540e\u8005\u8fd4\u56de\u524d\u4e00\u4e2a\u540c\u80de\nnextSibling\/previousSibling\nSet nexNode = oDom.getElementById(\"item1\").NextSibling\nSet preNode = oDom.getElementById(\"item2\").PreviousSibling\n\u83b7\u53d6\u7236\u8282\u70b9\nSet pdlTbl = pdlTbl.getelementbyid(pdlID) \nSet parentParent = pdlTbl.parentElement\nSet parNode = oDom.getElementsByTagName(\"li\")(0).ParentNode\nMsgBox parNode.nodeName\nMsgBox parNode.innerText\nMsgBox parNode.innerHTML\n\u83b7\u53d6\u5b50\u8282\u70b9\nSet parentParent = pdlTbl.childnodes.item(0)\n.getElementById(\"\u6807\u7b7e\u7684\u7236\u5bf9\u8c61\").all.tags(\"\u6807\u7b7e\")\nfirstChild\n\u83b7\u53d6\u8282\u70b9\u4e2d\u7684\u9996\u4e2a\u5b50\u8282\u70b9\uff0c\u5982\u679c\u4e0d\u5177\u6709\u5b50\u8282\u70b9\u5219\u8fd4\u56denull\nlastChild\n\u83b7\u53d6\u8282\u70b9\u4e2d\u7684\u672b\u4e2a\u5b50\u8282\u70b9\uff0c\u5982\u679c\u4e0d\u5177\u6709\u5b50\u8282\u70b9\u5219\u8fd4\u56denull\nnextSibling\n\u83b7\u53d6\u8282\u70b9\u6240\u5728\u7684\u8282\u70b9\u5217\u8868\u4e2d\u4e0b\u4e00\u4e2a\u8282\u70b9\nnodeName\n\u83b7\u53d6\u8282\u70b9\u7684\u540d\u79f0\nnodeType\n\u83b7\u53d6\u8282\u70b9\u7684\u7c7b\u578b\uff0c\uff08\u53c2\u89c1\u4e0b\u8868\uff09\nnodeValue\n\u83b7\u53d6\u6587\u672c\u8282\u70b9\u503c\nparentNode\n\u83b7\u53d6\u8282\u70b9\u7684\u7236\u8282\u70b9\uff0c\u5982\u679c\u4e0d\u5177\u6709\u7236\u8282\u70b9\u5219\u8fd4\u56denull\n\u5176\u4ed6\u8282\u70b9\u53c2\u8003\uff1a https:\/\/blog.csdn.net\/sinat_25419171\/article\/details\/43405317?utm_source=app&amp;app_version=5.5.0\n\n\u56fe\u7247\n\u5bf9\u4e8e\u90e8\u5206tag\u53ef\u4ee5\u901a\u8fc7\u81ea\u5df1\u7684\u4e13\u6709\u96c6\u5408\u65b9\u5f0f\u8bbf\u95ee\uff0c\u4f8b\u5982&lt;img name=\"img1\">\uff1a\nWebBrowser1.Document.images(0)\nWebBrowser1.Document.images(\"img1\")\n\u5f53\u7136\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u65b9\u6cd5\uff1a\nWebBrowser1.Document.tags(\"img\")(0)\n \n\u7126\u70b9\u653e\u5728\u67d0\u4e2a\u5143\u7d20\nobjIE.document.all.search_keywords.focus()\n\n\u8d85\u94fe\u63a5\n\u4e00\u822c\u4f7f\u7528getElementsByTagName\u83b7\u53d6\u8d85\u94fe\u63a5\u5143\u7d20&lt;a>\u7684\u96c6\u5408\u5bf9\u8c61\uff0c\u5982\u4e0b\uff1a\nmytable.all.tags(\"td\")(n - 7).getelementsbytagname(\"a\")(0).href\n\n\u6eda\u52a8\u52a0\u8f7d\uff08\u4eb2\u6d4b\u53ef\u7528\uff09\nDim myDiv As Object\nDim elemRect As Object\nSet myDiv = .document.getElementById(\"item4\")\nSet elemRect = CallByName(myDiv, \"getBoundingClientRect\", VbMethod)\nDo Until elemRect.bottom > 0\n    currPage.parentWindow.scrollBy 0, 0\n    Set elemRect = CallByName(myDiv, \"getBoundingClientRect\", VbMethod)\nLoop\nmyDiv.ScrollIntoView\n\u5f53\u5143\u7d20\u4e0d\u5728\u89c6\u56fe\u4e2d\u65f6\uff0c\u5b83\u4e3a\u96f6\uff0c\u4e00\u65e6\u5b83\u5728\u89c6\u56fe\u4e2d\uff0c\u5b83\u4fbf\u6210\u4e3a\u975e\u96f6\u6570\u5b57\u3002\u6709\u5173\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u89c1here\n\u6700\u540e\uff0c\u4f7f\u7528myDiv.ScrollIntoView\u5c06\u6d4f\u89c8\u5668\u79fb\u81f3\u6700\u5e95\u7aef\u3002<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5c0f\u7ed3<\/h3>\n\n\n\n<p>\u901a\u8fc7\u8fd9\u4e9b\u7b80\u5355\u800c\u53c8\u5f3a\u5927\u7684VBA\u4ee3\u7801\uff0c\u4f60\u53ef\u4ee5\u5728IE\u4e2d\u5b9a\u4f4d\u5e76\u4e0e\u7f51\u9875\u5143\u7d20\u8fdb\u884c\u4ea4\u4e92\u3002\u65e0\u8bba\u662f\u586b\u5199\u8868\u5355\u3001\u70b9\u51fb\u6309\u94ae\u8fd8\u662f\u6293\u53d6\u4fe1\u606f\uff0c\u8fd9\u4e9b\u6280\u5de7\u90fd\u4f1a\u8ba9\u4f60\u4e8b\u534a\u529f\u500d\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u4f60\u5728VBA\u81ea\u52a8\u5316\u9886\u57df\u8fc8\u51fa\u575a\u5b9e\u7684\u4e00\u6b65\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u4f60\u60f3\u4e86\u89e3\u66f4\u591a\u6709\u5173VBA\u3001\u81ea\u52a8\u5316\u4ee5\u53ca\u7f16\u7a0b\u7684\u77e5\u8bc6\uff0c\u8bb0\u5f97\u5e38\u6765\u6211\u7684\u535a\u5ba2\u54e6\uff01\u611f\u8c22\u9605\u8bfb\uff0c\u6211\u4eec\u4e0b\u6b21\u518d\u89c1\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5927\u5bb6\u597d\uff0c\u6b22\u8fce\u6765\u5230\u6211\u7684\u535a\u5ba2\uff01\u4eca\u5929\u6211\u8981\u548c\u5927\u5bb6&#8230; <\/p>\n","protected":false},"author":1,"featured_media":3615,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[40],"tags":[36,54,55,49,53],"class_list":["post-3614","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-vba","tag-vba","tag-54","tag-55","tag-49","tag-53"],"_links":{"self":[{"href":"https:\/\/www.bytetape.com\/index.php?rest_route=\/wp\/v2\/posts\/3614","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bytetape.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bytetape.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bytetape.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bytetape.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3614"}],"version-history":[{"count":1,"href":"https:\/\/www.bytetape.com\/index.php?rest_route=\/wp\/v2\/posts\/3614\/revisions"}],"predecessor-version":[{"id":3616,"href":"https:\/\/www.bytetape.com\/index.php?rest_route=\/wp\/v2\/posts\/3614\/revisions\/3616"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bytetape.com\/index.php?rest_route=\/wp\/v2\/media\/3615"}],"wp:attachment":[{"href":"https:\/\/www.bytetape.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bytetape.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3614"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bytetape.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}