I'm new to coding and am trying to navigate a website automatically. I can get the code to run and automatically login successfully, however once it reaches the next page, I am unable to interact with any elements. In the example below, I want the macro to click the 'advanced search link' after it logs in. When I run the code, I get a 'Run time error 91: Object variable or With block variable not set.'
The code:
Private Sub CommandButton7_Click()
Dim ie As SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Set ie = New InternetExplorerMedium
ie.Visible = True
ie.navigate ("website")
While ie.Busy Or ie.readyState <> 4: DoEvents: Wend
Set HTMLDoc = ie.document
HTMLDoc.all.txtUsername.Value = "username"
HTMLDoc.all.txtPassword.Value = "password"
HTMLDoc.all.imgbtnLogin.Click
While ie.Busy Or ie.readyState <> 4: DoEvents: Wend <<<Code works up to here.
HTMLDoc.getElementById("lnkAdvancedSearch").Click <<<This yields the error messsage.
End Sub
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<style>
.link {
font-family: Arial;
font-size: 8pt;
font-weight: normal;
color: blue;
}
.standard {
font-family: Arial;
font-size: 8pt;
font-weight: normal;
color: black;
}
</style>
</head>
<BODY><FORM onkeypress="javascript:return WebForm_FireDefaultButton(event, 'btnSearch')" id=frmMe method=post name=frmMe action=./todoSummary.aspx oldSubmit=" function submit() { [native code] } " submit="function WebForm_SaveScrollPositionSubmit() { if (__nonMSDOMBrowser) { theForm.elements['__SCROLLPOSITIONY'].value = window.pageYOffset; theForm.elements['__SCROLLPOSITIONX'].value = window.pageXOffset; } else { theForm.__SCROLLPOSITIONX.value = WebForm_GetScrollX(); theForm.__SCROLLPOSITIONY.value = WebForm_GetScrollY(); } if ((typeof(this.oldSubmit) != "undefined") && (this.oldSubmit != null)) { return this.oldSubmit(); } return true; }" oldOnSubmit="null" _events="[object Object]">
<DIV class=standard>
<TABLE width="100%">
<TBODY>
<TR>
<TD>
<TABLE>
<TBODY>
<TR>
**<TD style="VERTICAL-ALIGN: top"><A id=lnkAdvancedSearch class=link href="javascript:__doPostBack('lnkAdvancedSearch','')" shape="">Advanced Search:</A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></DIV></FORM></BODY>
</html>
^^This is the element I want to interact with^^**

HTMLDoc.getElementById("lnkAdvancedSearch")is notNothingbefore clicking? You might need toSet HTMLDoc = ie.Documentagain after theWhileloop.ExecScriptas it seems the link is actually calling a javascript so you can execute the javascript directly. see this answer for reference (there's plenty of examples around so you can google it too) @andrewi