{"id":12113,"date":"2012-01-03T10:52:11","date_gmt":"2012-01-03T10:52:11","guid":{"rendered":"http:\/\/www.learnexcelmacro.com\/?p=1136"},"modified":"2012-01-03T10:52:11","modified_gmt":"2012-01-03T10:52:11","slug":"how-to-login-to-gmail-from-excel-macro","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/excel\/2012\/01\/how-to-login-to-gmail-from-excel-macro\/","title":{"rendered":"How to interact with HTML pages from Excel"},"content":{"rendered":"
In this article you are going to learn how to interact with HTML pages from Excel Macro\/VBA. Though Excel is not much capable and easy enough to interact with HTML pages and its HTML controls. But up to some extend we can interact with HTML pages by Excel Macro.<\/p>\n
To interact with HTML pages and its controls we need to add two references in our Excel VBA.<\/p>\n
1. Microsoft HTML Object Library (mshtml.tlb): <\/strong> This library is required to access all HTML controls which can be present on your HTML page. <\/p>\n 2. Microsoft Internet Controls (ieframe.dll): <\/strong> This reference is required to do operations on Internet Explorer because to open an HTML page we need to access Internet Explorer. 1. Go to VB Editor Screen (Alt+F11) To explain how to open a browser and then a URL and after that recognize some controls on that page and pass some value in it and do some click operation etc, i have taken Gmail Login<\/strong> as an example. In the below example you will see how to login to Gmail from Excel Macro.<\/strong> In this article you are going to learn how to interact with HTML pages from Excel Macro\/VBA. Though Excel is not much capable and easy enough to interact with HTML pages and its HTML controls. But up to some extend we can interact with HTML pages by Excel Macro. To interact with HTML pages and […]<\/p>\n","protected":false},"author":45,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[1246],"tags":[],"class_list":["post-12113","post","type-post","status-publish","format-standard","hentry","category-macro"],"yoast_head":"\n
\n<\/p>\nHow to Add Reference in Excel<\/h3>\n
\n 2. Tools \u2013> References\u2026
\n 3. From the List of Available References Select your Reference Name which you want to add.
\n Note:<\/strong> If you are not able to find the Reference Name in the list then Click on browse and select the dll name whihc is given for those Reference Names.
\n 4. Click OK
\n<\/p>\n
\n
\nNote:<\/strong> Read all the comments gievn in the code, which explains each statement will do what. See if this works for you. If you are facing any issue in this code, do let me know via comment or Mail Me<\/a><\/strong>
\n<\/p>\n\nPrivate Sub CommandButton1_Click()\n\nDim HTMLDoc As HTMLDocument\nDim oBrowser As InternetExplorer\nDim oHTML_Element As IHTMLElement\n\n\nOn Error GoTo Err\n\nSet oBrowser = New InternetExplorer\noBrowser.Height = 1000\noBrowser.Width = 1000\noBrowser.Silent = False\n'oBrowser.tim\noBrowser.navigate \"http:\/\/gmail.com\"\noBrowser.Visible = False\n\nDo\n' Wait till the Browser is loaded\n\nLoop Until oBrowser.readyState = READYSTATE_COMPLETE\nSet HTMLDoc = oBrowser.Document\n\n'Once browser is fully loaded give few seconds\n'this is because sometimes even though the Browser State is\n'Complete but still some of the controls are not\n'ready completely. In such case your script may fail.\n'That's why i have given a waiting time of 3 seconds\n'after the page is loaded completely.\n\nApplication.Wait DateAdd(\"s\", 3, Now)\n\n'Once browser is open with Gmail URL\n'Now we need to pass ID and password at the right\n'field. For this right click on the Gmail page\n'and click on View Code. Here check the \"id\" of\n'User Name and Password Textboxes.\n'For example if ID of the User Name textbox is \"usrname\"\n'then syntax to pass User name in that field would be:\n'HTMLDoc.all.usrname.Value=\"your user name\".\n'Same way i have passed user name and password as below\n\nHTMLDoc.all.Email.Value = \"vishwamitra01\"\nHTMLDoc.all.Passwd.Value = \"*********\"\n\n'after entering email id and password\n'we need to search the button to Sign in gmail.\n'For this also we need to check the ID or name of that\n'button by right clicking and seeing the code.\n'Once you get the Name of that button then use the\n'below code to click on that.\n'Here for loop is necessary because if it is not able\n'to find the control in first time then it will go\n'and look for another button on that page.\n'Whichever button it is finiding with the name as\n'\"signIn\", it will click and for loop will end.\n\n\nFor Each oHTML_Element In HTMLDoc.getElementsByName(\"signIn\")\nIf oHTML_Element.Type = \"submit\" Then oHTML_Element.Click: Exit For\nNext\n\n\nDo\nLoop Until oBrowser.readyState = READYSTATE_COMPLETE\nApplication.Wait DateAdd(\"s\", 1, Now)\n\noBrowser.Visible = True\nExit Sub\nErr:\nMsgBox (\"Error Occured\")\n\nEnd Sub\n\n<\/code><\/pre>\n<\/span>","protected":false},"excerpt":{"rendered":"